I have this pseudocode for the Miller-Rabin primitive tester:
function isPrime(n, k=5)
if n < 2 then return False
for p in [2,3,5,7,11,13,17,19,23,29]
if n % p == 0 then return n == p
s, d = 0, n-1
while d % 2 == 0
s, d = s+1, d/2
for i from 0 to k
x = powerMod(randint(2, n-1), d, n)
if x == 1 or x == n-1 then next i
for r from 1 to s
x = (x * x) % n
if x == 1 then return False
if x == n-1 then next i
return False
return True
But translating this into Python is difficult due to the statement next iin the inner loop for, which should break the two loops. In Python, no goto. Other experts who asked this question in Stack Overflow were told to use a local function with a condition returnor try/exceptor an additional logical flag, but these solutions either do not apply here or they strongly guess this beautiful pseudo-code.
What is Pythonic's approach to this problem?
source
share