Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
GUO Joanna committed Jan 2, 2022
1 parent dac6544 commit 8426950
Show file tree
Hide file tree
Showing 10 changed files with 88 additions and 60 deletions.
26 changes: 15 additions & 11 deletions bin/find_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
print('Detected numpy version {__version__}'.format(**locals()))

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

print()

def _check_num(n):
'''
Internel function to check the input.
Expand All @@ -36,10 +39,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
return 0

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

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

return retry + 1

Expand Down Expand Up @@ -2453,7 +2456,7 @@ def multiply_point(P, k, curve):

return P2

def lenstra(n, mode = 1, tries = 10):
def factor(n, mode = 1, tries = 10, retry = 1):
factors = []
for i in (2, 3):
while n % i == 0:
Expand Down Expand Up @@ -2485,10 +2488,7 @@ def lenstra(n, mode = 1, tries = 10):
i = 1
while True:
i += 1
if mode == 0:
P2 = add_points(P, P2, curve)

elif mode == 1:
if mode == 1:
P2 = multiply_point(P2, i, curve)

elif mode == 2:
Expand Down Expand Up @@ -2531,10 +2531,14 @@ def lenstra(n, mode = 1, tries = 10):
return factors

factors.append(n)
factors.sort()
return factors
checked = _check_factors(factors, n, retry)
if checked == 0:
factors.sort()
return factors

return factor(n, retry = checked)

return lenstra(n)
return factor(n)

def factor_pollardpm1(n, retry = 1):
'''
Expand Down
43 changes: 25 additions & 18 deletions build/lib/find_primes/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
'''
A module to finds all kinds of primes.
Python version: 3.6+ (If runs test)
Python version: 3.6+
'''

from time import time
from math import log, factorial, sqrt, log2, ceil, floor, gcd
from random import randint, randrange
from functools import reduce
from operator import mul
from sys import version_info
from sys import version, version_info
from argparse import ArgumentParser

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

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

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

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

print()

LEFT = 'left'
RIGHT = 'right'
BOTH = 'both'
Expand All @@ -37,6 +41,8 @@
HEXAGON = 'hexagon'
HEPTAGON = 'heptagon'

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

def _check_num(n):
'''
Internel function to check the input.
Expand All @@ -55,10 +61,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
return 0

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

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

return retry + 1

Expand Down Expand Up @@ -150,7 +156,7 @@ def find_palindromes_base_2(n):
base2_palin_primes = []
for ix, xp in enumerate(primes):
palin_num = xp[::-1]
result = eval(f'0b{palin_num}')
result = eval('0b{palin_num}'.format(**locals()))
if is_prime(result) and palin_num == xp:
base2_palin_primes.append(result)

Expand Down Expand Up @@ -181,12 +187,12 @@ def find_square_palindromes(n):
'''
_check_num(n)
palin_primes = find_palindromes(n)
square_palin_prime = {}
square_palin_primes = {}
for x in palin_primes:
if str(x ** 2)[::-1] == str(x ** 2):
square_palin_prime[x] = x ** 2
square_palin_primes[x] = x ** 2

return square_palin_prime
return square_palin_primes

def find_arithmetic_prime_progressions(n):
'''
Expand Down Expand Up @@ -2909,7 +2915,7 @@ def multiply_point(P, k, curve):

return P2

def factor(n, mode = 1, tries = 10):
def factor(n, mode = 1, tries = 10, retry = 1):
factors = []
for i in (2, 3):
while n % i == 0:
Expand Down Expand Up @@ -2941,10 +2947,7 @@ def factor(n, mode = 1, tries = 10):
i = 1
while True:
i += 1
if mode == 0:
P2 = add_points(P, P2, curve)

elif mode == 1:
if mode == 1:
P2 = multiply_point(P2, i, curve)

elif mode == 2:
Expand Down Expand Up @@ -2987,8 +2990,12 @@ def factor(n, mode = 1, tries = 10):
return factors

factors.append(n)
factors.sort()
return factors
checked = _check_factors(factors, n, retry)
if checked == 0:
factors.sort()
return factors

return factor(n, retry = checked)

return factor(n)

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

if __name__ == '__main__':
if version_info[0] == 3 and version_info[1] >= 6:
if CAN_RUN_TEST:
test()

else:
print('The test method can\'t run in your python version.')
print('The test method can\'t run in your python version ' + version.split(' (')[0] + '.')
26 changes: 15 additions & 11 deletions build/scripts-3.10/find_primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
print('Detected numpy version {__version__}'.format(**locals()))

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

print()

def _check_num(n):
'''
Internel function to check the input.
Expand All @@ -36,10 +39,10 @@ def _check_factors(ans, n, retry = 1, max_retries = 3):
return 0

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

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

return retry + 1

Expand Down Expand Up @@ -2453,7 +2456,7 @@ def multiply_point(P, k, curve):

return P2

def lenstra(n, mode = 1, tries = 10):
def factor(n, mode = 1, tries = 10, retry = 1):
factors = []
for i in (2, 3):
while n % i == 0:
Expand Down Expand Up @@ -2485,10 +2488,7 @@ def lenstra(n, mode = 1, tries = 10):
i = 1
while True:
i += 1
if mode == 0:
P2 = add_points(P, P2, curve)

elif mode == 1:
if mode == 1:
P2 = multiply_point(P2, i, curve)

elif mode == 2:
Expand Down Expand Up @@ -2531,10 +2531,14 @@ def lenstra(n, mode = 1, tries = 10):
return factors

factors.append(n)
factors.sort()
return factors
checked = _check_factors(factors, n, retry)
if checked == 0:
factors.sort()
return factors

return factor(n, retry = checked)

return lenstra(n)
return factor(n)

def factor_pollardpm1(n, retry = 1):
'''
Expand Down
Binary file added dist/find_primes-2.1.6-py3-none-any.whl
Binary file not shown.
Binary file added dist/find_primes-2.1.6.tar.gz
Binary file not shown.
3 changes: 2 additions & 1 deletion find_primes.egg-info/PKG-INFO
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Metadata-Version: 2.1
Name: find-primes
Version: 2.1.5
Version: 2.1.6
Summary: A module for finding primes and finding factors of big numbers.
Home-page: https://github.com/git4robot/pypi_find_primes
Author: JamesJ
Expand All @@ -11,6 +11,7 @@ Classifier: Programming Language :: Python
Classifier: License :: OSI Approved :: MIT License
Classifier: Development Status :: 4 - Beta
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.6.0
Description-Content-Type: text/markdown
License-File: LICENSE

Expand Down
1 change: 1 addition & 0 deletions find_primes.egg-info/SOURCES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ find_primes/__init__.py
find_primes.egg-info/PKG-INFO
find_primes.egg-info/SOURCES.txt
find_primes.egg-info/dependency_links.txt
find_primes.egg-info/requires.txt
find_primes.egg-info/top_level.txt
2 changes: 2 additions & 0 deletions find_primes.egg-info/requires.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
numpy
rsa
Loading

0 comments on commit 8426950

Please sign in to comment.