Miller-Rabin primality test-SAGE - sage

I'm trying to program the Miller-Rabin primality test in SAGE. Here is my code:
def miller(n,k):
i=1
s=(n-1).valuation(2)
t=(n-1)/(2**s)
while(i>0 and i<=k):
a=randint(3,n-3)
if gcd(a,n)!=1:
i=0
else:
y=a**t%n
if y!=1 and y!=n-1:
j=1
while(j>0 and j<=s-1 and y!=n-1):
y=y**2%n
if y==1:
j=0
else:
j=j+1
if y!=n-1:
i=0
if i>0:
i=i+1
if i==0:
print "n composite"
else:
print "n probably prime"
It works fine for not-too small numbers, however for n=3847982374893247238947238473289472348923784923748723482374832748923748932478239472389478239479273 I get "exponent must be at most 9223372036854775807". I would apreciate any advice :)

Related

how to find how many times does something appears in a list recursively (python)

imagine if i had a list ([1,2,2,3,2]) and i want to find how many times does the number 2 appear in the list, how would i do it recursively, do i set my counter to 0 in my base case? How do i make the list count my desired item.
def rc_count(L, x):
if len(L)==0:
n=0
return n
else:
rc_count(L[1:], x)
if L[0]==x:
n+=1
return n
it says local variable n referenced before assignment so where should i put my counter
I do not really se the reason for this recursive function. However this is how you would do it:
def rc_count(l,x):
n = 0
if not l:
return n
else:
n = re(l[1:], x)
if l[0] == x:
n+=1
return n
Here rc_count([1,2,2,2,2,3,4,5],2) will return 4

How to optimize this recursion function?

This is really similar to Fibonacci Sequence problem. I understand the DP optimization with Fibonacci function using the for loop, but I'm having hard time to connect to this problem.
The recursion function I want to optimize is:
def cal(n):
if n <= 0:
return 1
else:
return cal(n-25)+cal(n-26)
Something like this may help:
(It's inspired by previous post)
from functools import cache
#cache
def cal(n):
if n <= 0:
return 1
else:
return cal(n-25) + cal(n-26)
print(cal(100))
The idea of a "for loop optimization" is that we calculate cal(0), cal(1), cal(2), ... consecutively. And when we want cal(n), we already have cal(n-25) and cal(n-26) stored in an array.
So, the following solution has linear complexity for non-negative n:
def cal(n):
mem = [1] # cal(0) is 1
for i in range(1, n + 1):
num = 1 if i - 25 < 0 else mem[i - 25]
num += 1 if i - 26 < 0 else mem[i - 26]
mem.append (num)
return mem[-1]
One can further optimize to make all the values cal(1), cal(2), ..., cal(n) globally available after calculating the last of them.

How to make use of Threads optional in a Julia function

I have a function that optionally uses threads for its main loop, doing so when an argument usingthreads is true. At the moment, the code looks like this:
function dosomething(usingthreads::Bool)
n = 1000
if usingthreads
Threads.#threads for i = 1:n
#20 lines of code here
end
else
for i = 1:n
#same 20 lines of code repeated here
end
end
end
Less nasty than the above would be to put the "20 lines" in a separate function. Is there another way?
You could use a macro that changes its behavior depending on the result of Threads.nthreads():
macro maybe_threaded(ex)
if Threads.nthreads() == 1
return esc(ex)
else
return esc(:(Threads.#threads $ex))
end
end
Without threading, this macro will be a no-op:
julia> #macroexpand #maybe_threaded for i in 1:5
print(i)
end
:(for i = 1:5
#= REPL[2]:2 =#
print(i)
end)
But when threading is enabled and e.g. JULIA_NUM_THREADS=4 it will expand to the threaded version:
julia> #maybe_threaded for i in 1:5
print(i)
end
41325
Edit: Upon rereading the question, I realize this doesn't really answer it but it might be useful anyway.
You can use ThreadsX as suggested in this discourse link.
The answer from the thread (all credit to oxinabox):
using ThreadsX
function foo(multi_thread=true)
_foreach = multi_thread ? ThreadsX.foreach : Base.foreach
_foreach(1:10) do ii
#show ii
end
end

Print within Recursion function Python3

the point of my recursion function is to print integers in reverse order.
def rDisp(s):
n=str(s)
if n == "":
return n
else:
return rDisp(n[1:]) + n[0]
def main():
number=(int(input("Enter a number :")))
rDisp(num)
main()
If within the main function I implement print(reverseDisplay(number)), it works, however, for the purpose of this code, I want the reverseDisplay function to do the printing. How would I go about implementing the print function into that block of code.
Thanks!
Untested code:
def reversePrint(s):
if not s:
return
print(s[-1])
reversePrint(s[:-1])
def main():
number=input("Enter a number :")
reversePrint(number)
main()
Just got it
def reverseDisplay(s):
n=str(s)
if n == "":
return n
else:
reverseDisplay(n[1:])
b=n[0]
print(b,end='')

scapy hexdump()

i wonder which hexdump() scapy uses, since i would like to modify it, but i simply cant find anything.
what i DO find is:
def hexdump(self, lfilter=None):
for i in range(len(self.res)):
p = self._elt2pkt(self.res[i])
if lfilter is not None and not lfilter(p):
continue
print "%s %s %s" % (conf.color_theme.id(i,"%04i"),
p.sprintf("%.time%"),
self._elt2sum(self.res[i]))
hexdump(p)
but that simply is an alternative for pkt.hexdump(), which does a pkt.summary() with a following hexdump(pkt)
could anyone tell me where to find the hexdump(pkt) sourcecode?
what i want to have is the hex'ed packet, almost like str(pkt[0]) (where i can check byte by byte via str(pkt[0])[0] ), but with nothing else than hexvalues, just like displayed in hexdump(pkt).
maybe you guys could help me out with this one :)
found it, so, to answer my own question, it is located in utils.py
def hexdump(x):
x=str(x)
l = len(x)
i = 0
while i < l:
print "%04x " % i,
for j in range(16):
if i+j < l:
print "%02X" % ord(x[i+j]),
else:
print " ",
if j%16 == 7:
print "",
print " ",
print sane_color(x[i:i+16])
i += 16

Resources