def search(goal): """ >>> search(6) 13 >>> search(7) 56003 """ already_searched = set() for prime in primes(): if prime in already_searched: continue positions_list = list(position_combinations(prime)) for positions in positions_list: digits = list(digits_of(prime)) family = set() for digit in range(10): if 0 in positions and digit == 0: continue for pos in positions: digits[pos] = digit number = from_digits(digits) if is_prime(number): already_searched.add(number) family.add(number) if len(family) >= goal: return min(family)
def odd_composites(): """ >>> list(up_to(33, odd_composites())) [9, 15, 21, 25, 27, 33] """ for odd in islice(count(3), None, None, 2): if not is_prime(odd): yield odd
def prime_sqrt(n): """ >>> prime_sqrt(4) (2, 2) >>> prime_sqrt(1000) (31, 37) """ if n < 4: raise ValueError(n) s = math.sqrt(n) lps = math.floor(s) while not is_prime(lps): lps -= 1 ups = math.ceil(s) while not is_prime(ups): ups += 1 return (lps, ups)
def prime_sequence_length(coefficients): """ Find the length of the prime sequence generated by a polynomial. >>> prime_sequence_length((1, 1, 41)) 40 >>> prime_sequence_length((1, -79, 1601)) 80 """ for n in itertools.count(): if not is_prime(eval_polynomial(coefficients, n)): return n
def prime_sequence_sums(limit): prime_list = [] for p in primes(): prime_list.append(p) if sum(prime_list) >= limit: return for i in range(len(prime_list)): sequence = prime_list[i:] if len(sequence) <= 1: continue s = sum(sequence) if is_prime(s): yield sequence, s
def RSAKeyGeneration(socket, option): while(True): p = secrets.randbits(256) if(util.is_prime(p)): break while(True): q = secrets.randbits(256) if(util.is_prime(q) and p!=q): break N = p*q carmichael = util.lcm(p-1,q-1) e = 65537 d = float(1+carmichael)/e k = 1 while(not d.is_integer()): d = float(1+k*carmichael)/e k += 1 d = int(d) socket.send(str(e).encode()) e = int(socket.recv(1024).decode()) socket.send(str(N).encode()) n = int(socket.recv(1024).decode()) return d, n, e, N
from utility import is_prime n = abs(int(input('Enter a number: '))) print('The number is prime!' if is_prime(n) else 'The number is not prime!')
import itertools from utility import digits_of, from_digits, is_prime # We only need to search 4 and 7 digit pandigital numbers for primality. For # other sizes of numbers, their digital roots show that they are always # divisible by 3 (http://en.wikipedia.org/wiki/Digital_root). # 1+2+3+4+5+6+7+8+9 = 45, 4+5 = 9 # 1+2+3+4+5+6+7+8 = 36, 3+6 = 9 # etc. def digit_permutations(n): for digits in itertools.permutations(digits_of(n)): yield from_digits(digits) print(max(p for p in digit_permutations(1234567) if is_prime(p)))
41 20 7 8 9 10 27 42 21 22 23 24 25 26 43 44 45 46 47 48 49 >>> import itertools >>> list(itertools.islice(spiral_corners(), 4)) [(1,), (3, 5, 7, 9), (13, 17, 21, 25), (31, 37, 43, 49)] """ size = 0 n = 1 yield (n,) while True: size += 2 nums = [] for _ in range(4): n += size nums.append(n) yield tuple(nums) if __name__ == '__main__': total = 0 prime_count = 0 for i, nums in enumerate(spiral_corners()): for n in nums: total += 1 if is_prime(n): prime_count += 1 if i > 0 and prime_count * 10 < total: print(2 * i + 1) break