Skip to content

Commit 8426950

Browse files
author
GUO Joanna
committed
Add files via upload
1 parent dac6544 commit 8426950

File tree

10 files changed

+88
-60
lines changed

10 files changed

+88
-60
lines changed

bin/find_primes.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
print('Detected numpy version {__version__}'.format(**locals()))
1717

1818
except ImportError:
19+
print('Numpy is not found! Finding primes will be slower!')
1920
NUMPY_ENABLED = False
2021

22+
print()
23+
2124
def _check_num(n):
2225
'''
2326
Internel function to check the input.
@@ -36,10 +39,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
3639
return 0
3740

3841
if retry == max_retries + 1:
39-
print(f'Factor Error. The multiplication of {ans} is not {n}.')
40-
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
42+
print('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
43+
raise FactorError('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
4144

42-
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
45+
print('Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.'.format(**locals()))
4346

4447
return retry + 1
4548

@@ -2453,7 +2456,7 @@ def multiply_point(P, k, curve):
24532456

24542457
return P2
24552458

2456-
def lenstra(n, mode = 1, tries = 10):
2459+
def factor(n, mode = 1, tries = 10, retry = 1):
24572460
factors = []
24582461
for i in (2, 3):
24592462
while n % i == 0:
@@ -2485,10 +2488,7 @@ def lenstra(n, mode = 1, tries = 10):
24852488
i = 1
24862489
while True:
24872490
i += 1
2488-
if mode == 0:
2489-
P2 = add_points(P, P2, curve)
2490-
2491-
elif mode == 1:
2491+
if mode == 1:
24922492
P2 = multiply_point(P2, i, curve)
24932493

24942494
elif mode == 2:
@@ -2531,10 +2531,14 @@ def lenstra(n, mode = 1, tries = 10):
25312531
return factors
25322532

25332533
factors.append(n)
2534-
factors.sort()
2535-
return factors
2534+
checked = _check_factors(factors, n, retry)
2535+
if checked == 0:
2536+
factors.sort()
2537+
return factors
2538+
2539+
return factor(n, retry = checked)
25362540

2537-
return lenstra(n)
2541+
return factor(n)
25382542

25392543
def factor_pollardpm1(n, retry = 1):
25402544
'''

build/lib/find_primes/__init__.py

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
'''
22
A module to finds all kinds of primes.
3-
Python version: 3.6+ (If runs test)
3+
Python version: 3.6+
44
'''
55

66
from time import time
77
from math import log, factorial, sqrt, log2, ceil, floor, gcd
88
from random import randint, randrange
99
from functools import reduce
1010
from operator import mul
11-
from sys import version_info
11+
from sys import version, version_info
1212
from argparse import ArgumentParser
1313

1414
NUMPY_ENABLED = True
@@ -17,6 +17,7 @@
1717
print('Detected numpy version {__version__}'.format(**locals()))
1818

1919
except ImportError:
20+
print('Numpy is not found! Finding primes will be slower!')
2021
NUMPY_ENABLED = False
2122

2223
PRIME_TEST = True
@@ -26,8 +27,11 @@
2627
print('Detected rsa version {__version__}'.format(**locals()))
2728

2829
except ImportError:
30+
print('Rsa is not found! Factor Test will be disabled!')
2931
FACTOR_TEST = False
3032

33+
print()
34+
3135
LEFT = 'left'
3236
RIGHT = 'right'
3337
BOTH = 'both'
@@ -37,6 +41,8 @@
3741
HEXAGON = 'hexagon'
3842
HEPTAGON = 'heptagon'
3943

44+
CAN_RUN_TEST = version_info[0] == 3 and version_info[1] >= 6
45+
4046
def _check_num(n):
4147
'''
4248
Internel function to check the input.
@@ -55,10 +61,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
5561
return 0
5662

5763
if retry == max_retries + 1:
58-
print(f'Factor Error. The multiplication of {ans} is not {n}.')
59-
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
64+
print('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
65+
raise FactorError('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
6066

61-
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
67+
print('Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.'.format(**locals()))
6268

6369
return retry + 1
6470

@@ -150,7 +156,7 @@ def find_palindromes_base_2(n):
150156
base2_palin_primes = []
151157
for ix, xp in enumerate(primes):
152158
palin_num = xp[::-1]
153-
result = eval(f'0b{palin_num}')
159+
result = eval('0b{palin_num}'.format(**locals()))
154160
if is_prime(result) and palin_num == xp:
155161
base2_palin_primes.append(result)
156162

@@ -181,12 +187,12 @@ def find_square_palindromes(n):
181187
'''
182188
_check_num(n)
183189
palin_primes = find_palindromes(n)
184-
square_palin_prime = {}
190+
square_palin_primes = {}
185191
for x in palin_primes:
186192
if str(x ** 2)[::-1] == str(x ** 2):
187-
square_palin_prime[x] = x ** 2
193+
square_palin_primes[x] = x ** 2
188194

189-
return square_palin_prime
195+
return square_palin_primes
190196

191197
def find_arithmetic_prime_progressions(n):
192198
'''
@@ -2909,7 +2915,7 @@ def multiply_point(P, k, curve):
29092915

29102916
return P2
29112917

2912-
def factor(n, mode = 1, tries = 10):
2918+
def factor(n, mode = 1, tries = 10, retry = 1):
29132919
factors = []
29142920
for i in (2, 3):
29152921
while n % i == 0:
@@ -2941,10 +2947,7 @@ def factor(n, mode = 1, tries = 10):
29412947
i = 1
29422948
while True:
29432949
i += 1
2944-
if mode == 0:
2945-
P2 = add_points(P, P2, curve)
2946-
2947-
elif mode == 1:
2950+
if mode == 1:
29482951
P2 = multiply_point(P2, i, curve)
29492952

29502953
elif mode == 2:
@@ -2987,8 +2990,12 @@ def factor(n, mode = 1, tries = 10):
29872990
return factors
29882991

29892992
factors.append(n)
2990-
factors.sort()
2991-
return factors
2993+
checked = _check_factors(factors, n, retry)
2994+
if checked == 0:
2995+
factors.sort()
2996+
return factors
2997+
2998+
return factor(n, retry = checked)
29922999

29933000
return factor(n)
29943001

@@ -3218,8 +3225,8 @@ def test():
32183225
print(f'Factor Test Time: {round(end_all - start_all, 12)} seconds.')
32193226

32203227
if __name__ == '__main__':
3221-
if version_info[0] == 3 and version_info[1] >= 6:
3228+
if CAN_RUN_TEST:
32223229
test()
32233230

32243231
else:
3225-
print('The test method can\'t run in your python version.')
3232+
print('The test method can\'t run in your python version ' + version.split(' (')[0] + '.')

build/scripts-3.10/find_primes.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
print('Detected numpy version {__version__}'.format(**locals()))
1717

1818
except ImportError:
19+
print('Numpy is not found! Finding primes will be slower!')
1920
NUMPY_ENABLED = False
2021

22+
print()
23+
2124
def _check_num(n):
2225
'''
2326
Internel function to check the input.
@@ -36,10 +39,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
3639
return 0
3740

3841
if retry == max_retries + 1:
39-
print(f'Factor Error. The multiplication of {ans} is not {n}.')
40-
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
42+
print('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
43+
raise FactorError('Factor Error. The multiplication of {ans} is not {n}.'.format(**locals()))
4144

42-
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
45+
print('Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.'.format(**locals()))
4346

4447
return retry + 1
4548

@@ -2453,7 +2456,7 @@ def multiply_point(P, k, curve):
24532456

24542457
return P2
24552458

2456-
def lenstra(n, mode = 1, tries = 10):
2459+
def factor(n, mode = 1, tries = 10, retry = 1):
24572460
factors = []
24582461
for i in (2, 3):
24592462
while n % i == 0:
@@ -2485,10 +2488,7 @@ def lenstra(n, mode = 1, tries = 10):
24852488
i = 1
24862489
while True:
24872490
i += 1
2488-
if mode == 0:
2489-
P2 = add_points(P, P2, curve)
2490-
2491-
elif mode == 1:
2491+
if mode == 1:
24922492
P2 = multiply_point(P2, i, curve)
24932493

24942494
elif mode == 2:
@@ -2531,10 +2531,14 @@ def lenstra(n, mode = 1, tries = 10):
25312531
return factors
25322532

25332533
factors.append(n)
2534-
factors.sort()
2535-
return factors
2534+
checked = _check_factors(factors, n, retry)
2535+
if checked == 0:
2536+
factors.sort()
2537+
return factors
2538+
2539+
return factor(n, retry = checked)
25362540

2537-
return lenstra(n)
2541+
return factor(n)
25382542

25392543
def factor_pollardpm1(n, retry = 1):
25402544
'''
53.2 KB
Binary file not shown.

dist/find_primes-2.1.6.tar.gz

39.7 KB
Binary file not shown.

find_primes.egg-info/PKG-INFO

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Metadata-Version: 2.1
22
Name: find-primes
3-
Version: 2.1.5
3+
Version: 2.1.6
44
Summary: A module for finding primes and finding factors of big numbers.
55
Home-page: https://github.com/git4robot/pypi_find_primes
66
Author: JamesJ
@@ -11,6 +11,7 @@ Classifier: Programming Language :: Python
1111
Classifier: License :: OSI Approved :: MIT License
1212
Classifier: Development Status :: 4 - Beta
1313
Classifier: Topic :: Scientific/Engineering :: Mathematics
14+
Requires-Python: >=3.6.0
1415
Description-Content-Type: text/markdown
1516
License-File: LICENSE
1617

find_primes.egg-info/SOURCES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ find_primes/__init__.py
66
find_primes.egg-info/PKG-INFO
77
find_primes.egg-info/SOURCES.txt
88
find_primes.egg-info/dependency_links.txt
9+
find_primes.egg-info/requires.txt
910
find_primes.egg-info/top_level.txt

find_primes.egg-info/requires.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
numpy
2+
rsa

0 commit comments

Comments
 (0)