|
@@ -1,17 +1,18 @@
|
1
|
1
|
import random
|
2
|
|
-from math import floor, log
|
3
|
2
|
|
4
|
3
|
pto100 = [
|
5
|
|
- 2,3,5,7,11,13,17,19,23,29,31,37,41,
|
6
|
|
- 43,47,53,59,61,67,71,73,79,83,89,97
|
|
4
|
+ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
|
|
5
|
+ 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97
|
7
|
6
|
]
|
8
|
7
|
|
|
8
|
+
|
9
|
9
|
def in_100(x):
|
10
|
10
|
"""Check if x is in the primes less than 100 i.e., in the first 25 primes.
|
11
|
11
|
Useful for e.g. the first step in Baillie–PSW.
|
12
|
12
|
"""
|
13
|
13
|
return x in pto100
|
14
|
14
|
|
|
15
|
+
|
15
|
16
|
def hund_div(x):
|
16
|
17
|
"""Check if x is divisible by any of the 25 primes less than 100. Returns 0
|
17
|
18
|
if there is no such divisor, otherwise returns the first divisor.
|
|
@@ -23,6 +24,7 @@ def hund_div(x):
|
23
|
24
|
return v
|
24
|
25
|
return 0
|
25
|
26
|
|
|
27
|
+
|
26
|
28
|
def trd(x):
|
27
|
29
|
"""Express x as 2^r * d. Returns two values, r and d.
|
28
|
30
|
"""
|
|
@@ -33,6 +35,7 @@ def trd(x):
|
33
|
35
|
xc = xc // 2
|
34
|
36
|
return count, xc
|
35
|
37
|
|
|
38
|
+
|
36
|
39
|
def MillerRabinRandom(n, k):
|
37
|
40
|
"""Carry out a Miller-Rabin test on n with k iterations, choosing random
|
38
|
41
|
values for a.
|
|
@@ -53,32 +56,23 @@ def MillerRabinRandom(n, k):
|
53
|
56
|
return True
|
54
|
57
|
return False
|
55
|
58
|
|
|
59
|
+
|
56
|
60
|
def MillerRabin(n, base):
|
57
|
61
|
"""Carry out a Miller-Rabin test on n with a specified base.
|
58
|
62
|
"""
|
59
|
|
- #print(n, base)
|
60
|
63
|
r, d = trd(n-1)
|
61
|
|
- #print(r, d)
|
62
|
64
|
a = base
|
63
|
65
|
x = pow(a, d, n)
|
64
|
|
- #print(x)
|
65
|
66
|
if x == 1 or x == n - 1:
|
66
|
|
- #print("First")
|
67
|
67
|
return False
|
68
|
68
|
for iterb in range(r - 1):
|
69
|
69
|
x = pow(x, 2, n)
|
70
|
70
|
if x == n - 1:
|
71
|
|
- #print("Second,", iterb, x)
|
72
|
71
|
return False
|
73
|
|
- #print("True")
|
74
|
72
|
return True
|
75
|
73
|
|
76
|
|
-#for i in range(2, 100):
|
77
|
|
-# print(i, hund_div(i))
|
78
|
|
-#
|
79
|
|
-#print()
|
80
|
74
|
|
81
|
|
-#Print pseudoprimes greater than 100 for each base, from base 1 to 15
|
|
75
|
+# Print pseudoprimes greater than 100 for each base, from base 1 to 15
|
82
|
76
|
for base in range(2, 16):
|
83
|
77
|
print("{}:".format(base))
|
84
|
78
|
for i in range(2, 10000):
|