|
| 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