Browse Source

PEP 8 code in bot.py, sieve.py

Petra Lamborn 5 years ago
parent
commit
c03bd67719
2 changed files with 16 additions and 19 deletions
  1. 8
    14
      bot.py
  2. 8
    5
      sieve.py

+ 8
- 14
bot.py View File

@@ -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):

+ 8
- 5
sieve.py View File

@@ -3,6 +3,7 @@
3 3
 from numpy import array, zeros, int8
4 4
 from time import process_time
5 5
 
6
+
6 7
 def sieve(to):
7 8
     """Construct a sieve of Eratosthenes up to the value of 'to'; return all
8 9
     primes below this number.
@@ -17,6 +18,7 @@ def sieve(to):
17 18
     avals = array(range(to))
18 19
     return avals[zarr[range(to)] == 0]
19 20
 
21
+
20 22
 def sieval(to):
21 23
     """Run sieve algorithm up to 'to'; return process time, number of primes,
22 24
     and last prime calculated (in that order).
@@ -29,14 +31,15 @@ def sieval(to):
29 31
     ls = len(sh)
30 32
     return te, ls, sh[ls - 1]
31 33
 
34
+
32 35
 def sievemsg(to):
33 36
     """Format sieval output for sieve up to 'to'
34 37
 
35 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 45
     return rs
42
-