浏览代码

Merge branch 'working' into lucas

Petra Lamborn 5 年前
父节点
当前提交
a4054d1b70
共有 2 个文件被更改,包括 16 次插入19 次删除
  1. 8
    14
      bot.py
  2. 8
    5
      sieve.py

+ 8
- 14
bot.py 查看文件

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

+ 8
- 5
sieve.py 查看文件

3
 from numpy import array, zeros, int8
3
 from numpy import array, zeros, int8
4
 from time import process_time
4
 from time import process_time
5
 
5
 
6
+
6
 def sieve(to):
7
 def sieve(to):
7
     """Construct a sieve of Eratosthenes up to the value of 'to'; return all
8
     """Construct a sieve of Eratosthenes up to the value of 'to'; return all
8
     primes below this number.
9
     primes below this number.
17
     avals = array(range(to))
18
     avals = array(range(to))
18
     return avals[zarr[range(to)] == 0]
19
     return avals[zarr[range(to)] == 0]
19
 
20
 
21
+
20
 def sieval(to):
22
 def sieval(to):
21
     """Run sieve algorithm up to 'to'; return process time, number of primes,
23
     """Run sieve algorithm up to 'to'; return process time, number of primes,
22
     and last prime calculated (in that order).
24
     and last prime calculated (in that order).
29
     ls = len(sh)
31
     ls = len(sh)
30
     return te, ls, sh[ls - 1]
32
     return te, ls, sh[ls - 1]
31
 
33
 
34
+
32
 def sievemsg(to):
35
 def sievemsg(to):
33
     """Format sieval output for sieve up to 'to'
36
     """Format sieval output for sieve up to 'to'
34
 
37
 
35
     'to' must be greater than 2.
38
     'to' must be greater than 2.
36
     """
39
     """
37
-    t, n, l = sieval(to)
38
-    rs = ("There are {1} prime numbers smaller than {3}; the largest is {2}. This "
39
-        "calculation took {0:.2f} seconds via the sieve of "
40
-        "Eratosthenes.".format(t, n, l, to))
40
+    t, n, largest = sieval(to)
41
+    rs = ("There are {1} prime numbers smaller than {3}; "
42
+          "the largest is {2}. This "
43
+          "calculation took {0:.2f} seconds via the sieve of "
44
+          "Eratosthenes.".format(t, n, largest, to))
41
     return rs
45
     return rs
42
-