Skip to content

Commit c603fe8

Browse files
committed
Add files via upload
1 parent e50b1d1 commit c603fe8

File tree

7 files changed

+346
-140
lines changed

7 files changed

+346
-140
lines changed

README.md

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,30 @@ Beta Version:
1313
pip install --pre -U find-primes
1414
```
1515

16+
**Find all primes below a number**
17+
18+
Example: Find all primes below 100.
19+
```python
20+
from find_primes import all_primes
21+
print(all_primes(100, 'list'))
22+
```
23+
24+
**Check if a number is a prime**
25+
26+
Example: Check if 189765 is a prime.
27+
```python
28+
from find_primes import is_prime
29+
print(is_prime(189765))
30+
```
31+
32+
**Factor a big number**
33+
34+
Example: Factor 2776889953055853600532696901.
35+
```python
36+
from find_primes import factor_mpqs
37+
print(factor_mpqs(2776889953055853600532696901))
38+
```
39+
1640
**The CLI Tool**
1741

1842
Usage:
@@ -301,7 +325,7 @@ The Multiple Polynomial Quadratic Sieve (MPQS) method is a factorization method.
301325
Example: Factor a big number.
302326
```python
303327
from find_primes import factor_mpqs
304-
print(factor_mpqs(2776889953055853600532696901))
328+
print(factor_mpqs(277688995305593400532696901))
305329
```
306330

307331
**[SIQS Method](https://www.rieselprime.de/ziki/Self-initializing_quadratic_sieve)**

build/lib/find_primes/__init__.py

Lines changed: 147 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
from functools import reduce
1010
from operator import mul
1111
from sys import version, version_info
12-
from argparse import ArgumentParser
12+
from itertools import product, chain, permutations
13+
from array import array
1314

1415
DEBUG = False
1516
VERBOSE = False
@@ -44,6 +45,10 @@
4445

4546
if VERBOSE: print()
4647

48+
BRUTE_FORCE = 'brute_force'
49+
MILLER_RABIN = 'miller_rabin'
50+
AKS = 'aks'
51+
4752
LEFT = 'left'
4853
RIGHT = 'right'
4954
BOTH = 'both'
@@ -55,6 +60,9 @@
5560

5661
CAN_RUN_TEST = version_info[0] == 3 and version_info[1] >= 6
5762

63+
class FactorError(Exception):
64+
pass
65+
5866
def _check_num(n):
5967
'''
6068
Internel function to check the input.
@@ -80,22 +88,130 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
8088

8189
return retry + 1
8290

83-
def is_prime(n):
91+
def multi(a, b, n, r):
92+
'''
93+
Internal function.
94+
'''
95+
x = array('d', [])
96+
for i in range(len(a) + len(b) - 1):
97+
x.append(0)
98+
99+
for i in range(len(a)):
100+
for j in range(len(b)):
101+
x[(i + j) % r] += a[i] * b[j]
102+
x[(i + j) % r] %= n
103+
104+
for i in range(r, len(x)):
105+
x = x[:-1]
106+
107+
return x
108+
109+
def is_prime(n, method = MILLER_RABIN):
84110
'''
85111
If n is prime, return True.
112+
Arguments:
113+
method ----- The method to check if n is prime. Choises: AKS (not implemented yet), MILLER_RABIN, BRUTE_FORCE
86114
'''
87115
_check_num(n)
88116
if n in [2, 3, 5, 7]:
89117
return True
90-
91-
if not (n % 10 % 2) or n % 10 not in [1, 3, 7, 9] or n == 1 or not isinstance(n, int):
118+
119+
if n % 10 % 2 == 0 or n % 10 not in [1, 3, 7, 9] or n <= 1 or not isinstance(n, int):
92120
return False
93121

94-
for i in range(2, int(n ** 0.5 + 1)):
95-
if n % i == 0:
96-
return False
122+
if method == BRUTE_FORCE:
123+
for i in range(2, int(n ** 0.5 + 1)):
124+
if n % i == 0:
125+
return False
126+
127+
return True
128+
129+
elif method == AKS:
130+
raise NotImplementedError('This method is not implemented yet.')
131+
'''
132+
#Step 1
133+
for i in range(2, int(sqrt(n)) + 1) :
134+
val = log(n) / log(i)
135+
if int(val) == val:
136+
return False
137+
138+
#Step 2
139+
max_k = log2(n) ** 2
140+
next_r = True
141+
r = 1
142+
while next_r == True:
143+
r += 1
144+
next_r = False
145+
k = 0
146+
while k <= max_k and next_r == False:
147+
k += 1
148+
if pow(n, k, r) in [0, 1]:
149+
next_r = True
150+
151+
#Step 3
152+
for a in range(2, min(r, n)):
153+
if gcd(a, n) > 1:
154+
return False
155+
156+
#Step 4
157+
if n <= r:
158+
return True
159+
160+
#Step 5
161+
x = array('l', [])
162+
euler_phi = lambda r: len([x for x in range(1, r + 1) if gcd(r, i) == 1])
163+
for a in range(1, floor(sqrt(euler_phi(r)) * log2(n))):
164+
base = array('l', [a, 1])
165+
x = array('d', [])
166+
a = base[0]
167+
for i in range(len(base)):
168+
x.append(0)
169+
170+
x[0] = 1
171+
power = n
172+
while power > 0:
173+
if power % 2 == 1:
174+
x = multi(x, base, n, r)
175+
176+
base = multi(base, base, n, r)
177+
power //= 2
178+
179+
x[(0)] = x[(0)] - a
180+
x[n % r] = x[n % r] - 1
181+
if any(x):
182+
return False
183+
184+
#Step 6
185+
return True
186+
'''
187+
188+
elif method == MILLER_RABIN:
189+
oddPartOfNumber = n - 1
190+
timesTwoDividNumber = 0
191+
while oddPartOfNumber % 2 == 0:
192+
oddPartOfNumber //= 2
193+
timesTwoDividNumber += 1
194+
195+
for t in range(3):
196+
while True:
197+
randomNumber = randint(2, n) - 1
198+
if randomNumber not in [0, 1]:
199+
break
97200

98-
return True
201+
randomNumberWithPower = pow(randomNumber, oddPartOfNumber, n)
202+
if randomNumberWithPower not in [1, n - 1]:
203+
iterationNumber = 1
204+
while iterationNumber <= timesTwoDividNumber - 1 and randomNumberWithPower != n - 1:
205+
randomNumberWithPower = pow(randomNumberWithPower, 2, n)
206+
iterationNumber += 1
207+
208+
if (randomNumberWithPower != n - 1):
209+
return False
210+
211+
return True
212+
213+
else:
214+
return False
99215

100216
def all_primes(n, output = 'array'):
101217
'''
@@ -139,42 +255,6 @@ def get_repetend_length(denominator):
139255
pass
140256

141257
return length
142-
'''
143-
output = ''
144-
all_num = []
145-
upload = []
146-
con = 10 // denominator
147-
r = 10 % denominator
148-
upload.append(con)
149-
upload.append(r)
150-
all_num.append(upload)
151-
while True:
152-
con = r * 10 // denominator
153-
r = r * 10 % denominator
154-
upload = []
155-
upload.append(con)
156-
upload.append(r)
157-
index1 = 0
158-
index2 = 0
159-
for x in all_num:
160-
if x == upload:
161-
for a in all_num:
162-
if index1 == index2:
163-
output += '['
164-
165-
output += str(a[0])
166-
index2 += 1
167-
168-
output += ']'
169-
return output.find(']') - output.find('[') - 1
170-
171-
index1 += 1
172-
173-
all_num.append(upload)
174-
'''
175-
176-
class FactorError(Exception):
177-
pass
178258

179259
def find_twins(n):
180260
'''
@@ -456,35 +536,26 @@ def find_center_polygons(n, type):
456536
_check_num(n)
457537
primes = all_primes(n)
458538
center_polygon_primes = []
539+
result = 0
459540
if type == 'triangle':
460-
for i in range(1, n):
461-
result = (3 * (i ** 2) + 3 * i + 2) / 2
462-
if result in primes:
463-
center_polygon_primes.append(int(result))
541+
result = '(3 * (i ** 2) + 3 * i + 2) // 2'
464542

465543
elif type == 'square':
466-
for i in range(1, n):
467-
result = (i ** 2) + (i - 1) ** 2
468-
if result in primes:
469-
center_polygon_primes.append(int(result))
544+
result = '(i ** 2) + (i - 1) ** 2'
470545

471546
elif type == 'pentagon':
472-
for i in range(1, n):
473-
result = (5 * ((i - 1) ** 2) + 5 * (i - 1) + 2) / 2
474-
if result in primes:
475-
center_polygon_primes.append(int(result))
547+
result = '(5 * ((i - 1) ** 2) + 5 * (i - 1) + 2) // 2'
476548

477549
elif type == 'hexagon':
478-
for i in range(1, n):
479-
result = 1 + 3 * i * (i - 1)
480-
if result in primes:
481-
center_polygon_primes.append(int(result))
550+
result = '1 + 3 * i * (i - 1)'
482551

483552
elif type == 'heptagon':
484-
for i in range(1, n):
485-
result = (7 * ((i - 1) ** 2) + 7 * (i - 1) + 2) / 2
486-
if result in primes:
487-
center_polygon_primes.append(int(result))
553+
result = '(7 * ((i - 1) ** 2) + 7 * (i - 1) + 2) // 2'
554+
555+
for i in range(1, n):
556+
value = eval(result.replace('i', str(i)), {})
557+
if value in primes:
558+
center_polygon_primes.append(value)
488559

489560
return center_polygon_primes
490561

@@ -634,7 +705,7 @@ def find_uniques(n):
634705
uniques.append(x)
635706

636707
return uniques
637-
from itertools import product, chain, permutations
708+
638709
def find_friedmans(n):
639710
'''
640711
Return a list that has all friedman primes below n.
@@ -2156,7 +2227,8 @@ def floorsqrt(a):
21562227

21572228
x = x_new
21582229

2159-
class QS(object):
2230+
'''
2231+
class QS():
21602232
def __init__(self, n, sieverange, factorbase):
21612233
self.number = n
21622234
self.sqrt_n = int(sqrt(n))
@@ -2324,6 +2396,7 @@ def run_sieve(self):
23242396
23252397
self.smooth = smooth
23262398
return smooth
2399+
'''
23272400

23282401
class MPQS(object):
23292402
def __init__(self, n, sieverange = 0, factorbase = 0, multiplier = 0):
@@ -2683,6 +2756,7 @@ def gaussian(self):
26832756

26842757
return zero_vector
26852758

2759+
'''
26862760
def qs(n, s, f):
26872761
Q = QS(n, s, f)
26882762
Q.run_sieve()
@@ -2709,6 +2783,7 @@ def qs(n, s, f):
27092783
N_factors.append(factor)
27102784
27112785
N_factors.sort()
2786+
'''
27122787

27132788
def mpqs(n, s = 0, f = 0, m = 0):
27142789
M = MPQS(n, s, f, m)
@@ -2968,7 +3043,7 @@ def add_points(P1, P2, curve):
29683043

29693044
if P2.z == 0:
29703045
return P1
2971-
3046+
29723047
X1 = P1.x
29733048
Y1 = P1.y
29743049
Z1 = P1.z
@@ -3099,7 +3174,7 @@ def factor(n, mode = 1, tries = 10, retry = 1):
30993174
return factors
31003175

31013176
return factor(n, retry = checked)
3102-
3177+
31033178
return factor(n)
31043179

31053180
def factor_pollardpm1(n, retry = 1):
@@ -3212,6 +3287,10 @@ def test():
32123287
'''A test of this module.'''
32133288
if PRIME_TEST:
32143289
start_tm = time()
3290+
n = randint(1, 10000)
3291+
print(f'Is {n} a prime? {is_prime(n)}\n')
3292+
print(f'Factors of {n}: {factor_pollardpm1(n)}\n')
3293+
print(f'All primes: {find_twins(100)}\n')
32153294
print(f'Twin primes: {find_twins(500)}\n')
32163295
print(f'Palindome primes: {find_palindromes(1000)}\n')
32173296
print(f'Palindome primes which was in base 2: {find_palindromes_base_2(8200)}\n')
54.9 KB
Binary file not shown.

dist/find_primes-2.2.0.tar.gz

41.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)