Skip to content

Commit a86fa4d

Browse files
author
GUO Joanna
committed
Add files via upload
1 parent 43e0b89 commit a86fa4d

File tree

11 files changed

+72
-26
lines changed

11 files changed

+72
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ pip install --pre -U find-primes
1414
```
1515

1616
**The CLI Tool**
17+
1718
Usage:
1819
```shell
1920
find_primes.py --help

bin/find_primes.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def all_primes(n, output = 'array'):
7474
else:
7575
return [x for x in range(2, n + 1) if sieve[x]]
7676

77+
class FactorError(Exception):
78+
pass
79+
7780
def factor_siqs(n):
7881
'''
7982
Return a list that has all factors of n.
@@ -2304,7 +2307,7 @@ def mpqsfind(n, s = 0, f = 0, m = 0):
23042307
if 1 < divisor < N:
23052308
return divisor
23062309

2307-
def mpqs(n):
2310+
def mpqs(n, retry = 1, max_retries = 3):
23082311
num = n
23092312
ans = []
23102313
if is_prime(n):
@@ -2333,8 +2336,15 @@ def mpqs(n):
23332336
ans = [x for x in _factor(num)[1]]
23342337
break
23352338

2336-
ans.sort()
2337-
return ans
2339+
if reduce(mul, ans) == n:
2340+
ans.sort()
2341+
return ans
2342+
2343+
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
2344+
if retry == max_retries:
2345+
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
2346+
2347+
return mpqs(n, retry + 1)
23382348

23392349
return mpqs(n)
23402350

@@ -2620,18 +2630,20 @@ def add_args():
26202630
parser.add_argument('--all_primes', metavar = 'all_primes', const = all_primes, nargs = '?', help = all_primes.__doc__)
26212631
parser.add_argument('--factor', metavar = 'factor', const = factor_mpqs, nargs = '?', help = factor_mpqs.__doc__)
26222632
args = parser.parse_args()
2623-
print_help = False
2633+
print_help_is_prime = False
2634+
print_help_all_primes = False
2635+
print_help_factor = False
26242636
if args.is_prime:
26252637
print(is_prime(args.n))
26262638

26272639
else:
2628-
print_help = True
2640+
print_help_is_prime = True
26292641

26302642
if args.all_primes:
26312643
print(all_primes(args.n))
26322644

26332645
else:
2634-
print_help = print_help and True
2646+
print_help_all_primes = True
26352647

26362648
if args.factor:
26372649
if args.method == 'siqs':
@@ -2650,9 +2662,9 @@ def add_args():
26502662
print(factor_williamspp1(args.n))
26512663

26522664
else:
2653-
print_help = print_help and True
2665+
print_help_factor = True
26542666

2655-
if print_help:
2667+
if print_help_is_prime and print_help_all_primes and print_help_factor:
26562668
print('''usage: find_primes.py [-h] [-n num] [-method method] [--is_prime [is_prime]] [--all_primes [all_primes]]
26572669
[--factor [factor]]
26582670

build/lib/find_primes/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def all_primes(n, output = 'array'):
9090
else:
9191
return [x for x in range(2, n + 1) if sieve[x]]
9292

93+
class FactorError(Exception):
94+
pass
95+
9396
def find_twins(n):
9497
'''
9598
Return a dict that has all twin primes below n.
@@ -2757,7 +2760,7 @@ def mpqsfind(n, s = 0, f = 0, m = 0):
27572760
if 1 < divisor < N:
27582761
return divisor
27592762

2760-
def mpqs(n):
2763+
def mpqs(n, retry = 1, max_retries = 3):
27612764
num = n
27622765
ans = []
27632766
if is_prime(n):
@@ -2786,11 +2789,18 @@ def mpqs(n):
27862789
ans = [x for x in _factor(num)[1]]
27872790
break
27882791

2789-
ans.sort()
2790-
return ans
2792+
if reduce(mul, ans) == n:
2793+
ans.sort()
2794+
return ans
2795+
2796+
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
2797+
if retry == max_retries:
2798+
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
2799+
2800+
return mpqs(n, retry + 1)
27912801

27922802
return mpqs(n)
2793-
2803+
27942804
def factor_lenstra(n):
27952805
'''
27962806
Return a list that has all factors of n.

build/scripts-3.10/find_primes.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ def all_primes(n, output = 'array'):
7474
else:
7575
return [x for x in range(2, n + 1) if sieve[x]]
7676

77+
class FactorError(Exception):
78+
pass
79+
7780
def factor_siqs(n):
7881
'''
7982
Return a list that has all factors of n.
@@ -2304,7 +2307,7 @@ def mpqsfind(n, s = 0, f = 0, m = 0):
23042307
if 1 < divisor < N:
23052308
return divisor
23062309

2307-
def mpqs(n):
2310+
def mpqs(n, retry = 1, max_retries = 3):
23082311
num = n
23092312
ans = []
23102313
if is_prime(n):
@@ -2333,8 +2336,15 @@ def mpqs(n):
23332336
ans = [x for x in _factor(num)[1]]
23342337
break
23352338

2336-
ans.sort()
2337-
return ans
2339+
if reduce(mul, ans) == n:
2340+
ans.sort()
2341+
return ans
2342+
2343+
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
2344+
if retry == max_retries:
2345+
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
2346+
2347+
return mpqs(n, retry + 1)
23382348

23392349
return mpqs(n)
23402350

@@ -2620,18 +2630,20 @@ def add_args():
26202630
parser.add_argument('--all_primes', metavar = 'all_primes', const = all_primes, nargs = '?', help = all_primes.__doc__)
26212631
parser.add_argument('--factor', metavar = 'factor', const = factor_mpqs, nargs = '?', help = factor_mpqs.__doc__)
26222632
args = parser.parse_args()
2623-
print_help = False
2633+
print_help_is_prime = False
2634+
print_help_all_primes = False
2635+
print_help_factor = False
26242636
if args.is_prime:
26252637
print(is_prime(args.n))
26262638

26272639
else:
2628-
print_help = True
2640+
print_help_is_prime = True
26292641

26302642
if args.all_primes:
26312643
print(all_primes(args.n))
26322644

26332645
else:
2634-
print_help = print_help and True
2646+
print_help_all_primes = True
26352647

26362648
if args.factor:
26372649
if args.method == 'siqs':
@@ -2650,9 +2662,9 @@ def add_args():
26502662
print(factor_williamspp1(args.n))
26512663

26522664
else:
2653-
print_help = print_help and True
2665+
print_help_factor = True
26542666

2655-
if print_help:
2667+
if print_help_is_prime and print_help_all_primes and print_help_factor:
26562668
print('''usage: find_primes.py [-h] [-n num] [-method method] [--is_prime [is_prime]] [--all_primes [all_primes]]
26572669
[--factor [factor]]
26582670
0 Bytes
Binary file not shown.

dist/find_primes-2.1.2.tar.gz

2 Bytes
Binary file not shown.
53.2 KB
Binary file not shown.

dist/find_primes-2.1.3.tar.gz

39.6 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.2
3+
Version: 2.1.3
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
@@ -30,6 +30,7 @@ pip install --pre -U find-primes
3030
```
3131

3232
**The CLI Tool**
33+
3334
Usage:
3435
```shell
3536
find_primes.py --help

find_primes/__init__.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ def all_primes(n, output = 'array'):
9090
else:
9191
return [x for x in range(2, n + 1) if sieve[x]]
9292

93+
class FactorError(Exception):
94+
pass
95+
9396
def find_twins(n):
9497
'''
9598
Return a dict that has all twin primes below n.
@@ -2757,7 +2760,7 @@ def mpqsfind(n, s = 0, f = 0, m = 0):
27572760
if 1 < divisor < N:
27582761
return divisor
27592762

2760-
def mpqs(n):
2763+
def mpqs(n, retry = 1, max_retries = 3):
27612764
num = n
27622765
ans = []
27632766
if is_prime(n):
@@ -2786,11 +2789,18 @@ def mpqs(n):
27862789
ans = [x for x in _factor(num)[1]]
27872790
break
27882791

2789-
ans.sort()
2790-
return ans
2792+
if reduce(mul, ans) == n:
2793+
ans.sort()
2794+
return ans
2795+
2796+
print(f'Factor Error. The multiplication of {ans} is not {n}. Retry {retry}.')
2797+
if retry == max_retries:
2798+
raise FactorError(f'Factor Error. The multiplication of {ans} is not {n}.')
2799+
2800+
return mpqs(n, retry + 1)
27912801

27922802
return mpqs(n)
2793-
2803+
27942804
def factor_lenstra(n):
27952805
'''
27962806
Return a list that has all factors of n.

0 commit comments

Comments
 (0)