Skip to content

Commit ab34af0

Browse files
authored
Add files via upload
1 parent cfd9909 commit ab34af0

39 files changed

+285
-0
lines changed
3.35 KB
Binary file not shown.
3.42 KB
Binary file not shown.

dist/find_primes-1.0.0.post1.tar.gz

2.07 KB
Binary file not shown.

dist/find_primes-1.0.0.tar.gz

2.06 KB
Binary file not shown.
3.29 KB
Binary file not shown.

dist/find_primes-1.0.0b1.tar.gz

1.98 KB
Binary file not shown.
3.39 KB
Binary file not shown.

dist/find_primes-1.0.0rc1.tar.gz

2.07 KB
Binary file not shown.
3.48 KB
Binary file not shown.

dist/find_primes-1.1.0.tar.gz

2.18 KB
Binary file not shown.
3.96 KB
Binary file not shown.
4.03 KB
Binary file not shown.

dist/find_primes-1.1.1.post1.tar.gz

3 KB
Binary file not shown.
4.03 KB
Binary file not shown.

dist/find_primes-1.1.1.post2.tar.gz

3.02 KB
Binary file not shown.
4.04 KB
Binary file not shown.

dist/find_primes-1.1.1.post3.tar.gz

3.03 KB
Binary file not shown.
4.02 KB
Binary file not shown.

dist/find_primes-1.1.1.post4.tar.gz

3.02 KB
Binary file not shown.

dist/find_primes-1.1.1.tar.gz

2.98 KB
Binary file not shown.
3.96 KB
Binary file not shown.

dist/find_primes-1.1.2.tar.gz

3.01 KB
Binary file not shown.
4.12 KB
Binary file not shown.
4.2 KB
Binary file not shown.

dist/find_primes-1.2.0.post1.tar.gz

3.21 KB
Binary file not shown.
4.2 KB
Binary file not shown.

dist/find_primes-1.2.0.post2.tar.gz

3.21 KB
Binary file not shown.
4.19 KB
Binary file not shown.

dist/find_primes-1.2.0.post3.tar.gz

3.2 KB
Binary file not shown.
4.2 KB
Binary file not shown.

dist/find_primes-1.2.0.post4.tar.gz

3.2 KB
Binary file not shown.

dist/find_primes-1.2.0.tar.gz

3.19 KB
Binary file not shown.
4.12 KB
Binary file not shown.

dist/find_primes-1.2.1.tar.gz

3.15 KB
Binary file not shown.

find_primes.egg-info/PKG-INFO

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
Metadata-Version: 2.1
2+
Name: find-primes
3+
Version: 1.2.1
4+
Summary: A module for finding primes.
5+
Home-page: UNKNOWN
6+
Author: JamesJ
7+
Author-email: GGJamesQQ@yeah.net
8+
License: UNKNOWN
9+
Description: Find Primes is a library to find all kinds of primes.
10+
11+
**Install**
12+
```shell
13+
pip install find-primes
14+
```
15+
**[Twin Primes](https://en.wikipedia.org/wiki/Twin_prime)**
16+
17+
A twin prime is a prime number that is either 2 less or 2 more than another prime number.
18+
19+
Example: Find all twin primes below 1000.
20+
```python
21+
from find_primes import find_twins
22+
print(find_twins(1000))
23+
```
24+
25+
**[Palindrome Primes](https://en.wikipedia.org/wiki/Palindromic_prime)**
26+
27+
A palindrome prime is a prime number that is also a palindrome number.
28+
29+
Example: Find all palindrome primes below 1000.
30+
```python
31+
from find_primes import find_palindromes
32+
print(find_palindromes(1000))
33+
```
34+
35+
**[Emirps](https://en.wikipedia.org/wiki/Emirp)**
36+
37+
An emirp is a prime number that results in a different prime when its decimal digits are reversed.
38+
39+
Example: Find all emirps below 1000.
40+
```python
41+
from find_primes import find_reverse
42+
print(find_reverse(1000))
43+
```
44+
45+
**[Primes in Arithmetic Progression](https://en.wikipedia.org/wiki/Primes_in_arithmetic_progression)**
46+
47+
Primes in arithmetic progression are any sequence of at least three prime numbers that are consecutive terms in an arithmetic progression.
48+
49+
Example: Find all primes in arithmetic progression below 1000.
50+
```python
51+
from find_primes import find_arithmetic_prime_progressions
52+
print(find_arithmetic_prime_progressions(1000))
53+
```
54+
55+
**[Mersenne Primes](https://en.wikipedia.org/wiki/Mersenne_prime)**
56+
57+
A mersenne prime is a prime number that is one less than a power of two.
58+
59+
Example: Find all mersenne primes below 600000.
60+
```python
61+
from find_primes import find_mersenne_primes
62+
print(find_mersenne_primes(600000))
63+
```
64+
65+
**[Fermat Pseudoprimes](https://en.wikipedia.org/wiki/Fermat_pseudoprime)**
66+
67+
A fermat pseudoprime is a pseudoprime that satisfies fermat's little theorem.
68+
69+
Example: Find all fermat pseudoprimes below 1000.
70+
```python
71+
from find_primes import find_fermat_pseudoprime
72+
print(find_fermat_pseudoprime(1000))
73+
```
74+
Platform: UNKNOWN
75+
Classifier: Programming Language :: Python :: 3 :: Only
76+
Classifier: License :: OSI Approved :: MIT License
77+
Classifier: Development Status :: 4 - Beta
78+
Requires-Python: >=3.6
79+
Description-Content-Type: text/markdown

find_primes.egg-info/SOURCES.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
README.md
2+
setup.py
3+
find_primes/__init__.py
4+
find_primes.egg-info/PKG-INFO
5+
find_primes.egg-info/SOURCES.txt
6+
find_primes.egg-info/dependency_links.txt
7+
find_primes.egg-info/top_level.txt
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

find_primes.egg-info/top_level.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
find_primes

find_primes/__init__.py

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
'''A module to finds all kinds of primes.'''
2+
from time import time
3+
from math import log
4+
5+
NUMPY_ENABLED = True
6+
try:
7+
from numpy import ones, nonzero, __version__
8+
print(f'Detected numpy version {__version__}')
9+
except ImportError:
10+
NUMPY_ENABLED = False
11+
12+
__version__ = 1.0
13+
14+
def _check_num(n):
15+
if not isinstance(n, int):
16+
raise TypeError(f'Type of argument n should be int, got {type(n).__name__}')
17+
18+
if n < 0:
19+
raise ValueError(f'The number of argument n should be greater than 0, got {n}')
20+
21+
def is_prime(n):
22+
'''
23+
If n is prime, return True.
24+
'''
25+
_check_num(n)
26+
if n in [2, 3, 5, 7]:
27+
return True
28+
29+
if not (n % 10 % 2) or n % 10 not in [1, 3, 7, 9] or n == 1 or not isinstance(n, int):
30+
return False
31+
32+
for i in range(2, int(n ** 0.5 + 1)):
33+
if n % i == 0:
34+
return False
35+
36+
return True
37+
38+
def all_primes(n, output = 'array'):
39+
'''
40+
Return a prime list below n.
41+
42+
Arguments:
43+
output ----- 'array' or 'list' ----- The output type of the function.
44+
'''
45+
_check_num(n)
46+
if NUMPY_ENABLED:
47+
sieve = ones(n + 1, dtype = bool)
48+
else:
49+
sieve = [True] * (n + 1)
50+
51+
for i in range(2, int(n ** 0.5) + 1):
52+
if sieve[i]:
53+
for j in range(i * i, n + 1, i):
54+
sieve[j] = False
55+
56+
if NUMPY_ENABLED:
57+
s = nonzero(sieve)[0]
58+
if output == 'list':
59+
return s.tolist()[2:]
60+
61+
return s[2:]
62+
63+
else:
64+
return [x for x in range(2, n + 1) if sieve[x]]
65+
66+
def find_twins(n):
67+
'''
68+
Return a dict that has all twin primes below n.
69+
'''
70+
_check_num(n)
71+
primes = all_primes(n)
72+
twin_primes = {}
73+
for ix, xp in enumerate(primes):
74+
if ix == len(primes) - 1:
75+
break
76+
77+
if primes[ix + 1] - xp == 2:
78+
twin_primes[xp] = primes[ix + 1]
79+
80+
return twin_primes
81+
82+
def find_palindromes(n):
83+
'''
84+
Return a list that has all palindromes primes below n.
85+
'''
86+
_check_num(n)
87+
primes = all_primes(n)
88+
palin_primes = []
89+
for ix, xp in enumerate(primes):
90+
palin_num = int(str(xp)[::-1])
91+
if is_prime(palin_num) and palin_num == xp and xp > 10:
92+
palin_primes.append(palin_num)
93+
94+
return palin_primes
95+
96+
def find_reverse(n):
97+
'''
98+
Return a dict that has all reverse primes below n.
99+
'''
100+
_check_num(n)
101+
primes = all_primes(n)
102+
reverse_primes = {}
103+
for ix, xp in enumerate(primes):
104+
reverse_num = int(str(xp)[::-1])
105+
if is_prime(reverse_num) and xp > 10:
106+
reverse_primes[xp] = reverse_num
107+
108+
palin_primes = find_palindromes(n)
109+
for x in palin_primes:
110+
if reverse_primes.get(x):
111+
reverse_primes.pop(x)
112+
113+
return reverse_primes
114+
115+
def find_square_palindromes(n):
116+
'''
117+
Return a dict that has all square palindrome primes below n.
118+
'''
119+
_check_num(n)
120+
palin_primes = find_palindromes(n)
121+
square_palin_prime = {}
122+
for x in palin_primes:
123+
if str(x ** 2)[::-1] == str(x ** 2):
124+
square_palin_prime[x] = x ** 2
125+
126+
return square_palin_prime
127+
128+
def find_arithmetic_prime_progressions(n):
129+
'''
130+
Return a list that has all arithmetic prime progression below n.
131+
'''
132+
_check_num(n)
133+
primes = all_primes(n)
134+
time = 0
135+
arithmetic_prime_list = []
136+
for i, xp in enumerate(primes):
137+
for j in range(i + 1, len(primes)):
138+
a0, a1 = primes[i], primes[j]
139+
an = a1 + a1 - a0
140+
k = []
141+
while an < n and an in primes:
142+
k.append(an)
143+
an += a1 - a0
144+
145+
if len([a0, a1] + k) >= 3:
146+
if k and not time:
147+
arithmetic_prime_list = [[a0, a1] + k]
148+
149+
if time:
150+
arithmetic_prime_list += [[a0, a1] + k]
151+
time += 1
152+
153+
return arithmetic_prime_list
154+
155+
def find_mersenne_primes(n):
156+
'''
157+
Return a list that has all mersenne primes below n.
158+
'''
159+
_check_num(n)
160+
primes = set(all_primes(n))
161+
result = []
162+
for i in range(2, int(log(n + 1, 2)) + 1):
163+
result.append(2 ** i - 1)
164+
165+
mersenne_primes = primes.intersection(result)
166+
return sorted(mersenne_primes)
167+
168+
def find_fermat_pseudoprime(n):
169+
'''
170+
Return a list that has all fermat pseudoprimes below n.
171+
'''
172+
_check_num(n)
173+
primes = all_primes(n)
174+
a = 2
175+
fermat_pseudoprimes = []
176+
composites = [x for x in range(3, n + 1, a) if x not in primes]
177+
for x in composites:
178+
if (a ** (x - 1) - 1) % x == 0:
179+
fermat_pseudoprimes.append(x)
180+
181+
return fermat_pseudoprimes
182+
183+
def main():
184+
'''A test of this module.'''
185+
start_tm = time()
186+
print(f'Twin primes: {find_twins(4750)}\n')
187+
print(f'Palindome primes: {find_palindromes(20000)}\n')
188+
print(f'Reverse primes: {find_reverse(3000)}\n')
189+
print(f'Square palindome primes: {find_square_palindromes(500)}\n')
190+
print(f'Arithmetic prime progressions: {find_arithmetic_prime_progressions(125)}\n')
191+
print(f'Mersenne Primes: {find_mersenne_primes(525000)}\n')
192+
print(f'Fermat Pseudoprime: {find_fermat_pseudoprime(1000)}')
193+
end_tm = time()
194+
print(f'Time: {round(end_tm - start_tm, 12)} seconds.')
195+
196+
if __name__ == '__main__':
197+
main()

0 commit comments

Comments
 (0)