require("bigint") isprime( BigInt( strcat( repeat( "1808010808", 1560 ), "1" ) ) )

I assume it is flawed because I get a segmentation fault. In fact, even with prime numbers as “small” as 953467954114363 I get segfaults often, though when the it works, it takes over a minute.

]]>`say ("1808010808" x 1560 ~ "1").Int.is-prime;`

On Rakudo Perl 6 it runs in 4 hours. ]]>

def modpower(b, e, n):

'''Iterative method for Fermat's Test, O(n^3) instead of exponential.

From Computations in Number Theory Using Python: A Brief Introduction,

Jim Carlson, March 2003'''

result = 1

s = b

q = e

while q > 0:

#r = q % 2

r = q & 1

if r == 1:

result = result * s % n

# print q, r, s, result

s = s * s % n

#q = q/2

q = q >> 1

return result

`def is_prime(n):`

return modpower(2, n-1, n) == 1

1. It tests if the target number is a pseudoprime *after* running the mathematical test. I would think this would be a relatively fast operation so maybe it could be done first. This isn’t related to this issue, though.

2. It doesn’t use any threading. Tests for different bases could be run simultaneously.

3. The actual test function does some of the exact same calculations multiple times for each base, even though the calculations in question are independent of the base.

4. If I am reading it correctly, the test function could skip the tests as long as b**2 is lower than n-1, since that case b**2%n will never equal n-1.

]]>http://mathworld.wolfram.com/Rabin-MillerStrongPseudoprimeTest.html

It appears they use similar versions of one algorithm (although not identical), while mathematica also uses another algorithm.

]]>