def deal(secret, n, k): print secret m = prime.get_primes(secret, 1) for x in prime.get_primes(m[0]*3,n): m.append(x) print 'm: ', m mdash = m[1:k+1] mi = reduce(lambda x,y: x*y, mdash) mdash = m[k+1:n+1] mk = m[0]*reduce(lambda x,y: x*y, mdash) print 'mi: ', mi print 'mk: ', mk if mi>mk: print 'mi > mk: Success' else: print 'mi < mk: Condition failed' M = reduce(lambda x,y: x*y, m) y = secret while(True): a = int(random.random()*100) print 'a: ',a y = secret + a*m[0] print 'y: ',y,' mi:', mi if y < 0 or y > mi: print 'Failure. Trying again!' else: break Y = list() for x in xrange(1,n+1): yi = y % m[x] Y.append(yi) print 'y_'+str(x)+":", yi, 'm_'+str(x)+":", m[x] print 'm0:', m[0]
def generatePrivateKey(self) -> list: # to obtain random seeds random.seed(time.time()) # generate the first number of the series self.sum = random.randint(100, 500) # private key key = [self.sum] # generate the remaining values for private key for i in range(self.n - 1): nextnum = random.randint(self.sum + 1, self.sum + 100) self.sum += nextnum key.append(nextnum) # store the modulo required for public key generation self.modulo = random.randint(self.sum + 130, self.sum + 150) # get the generator function prime_handle = prime.get_primes(self.sum + 100) # get the first prime number : to ensure multiplier and modulo are co prime self.multiplier = prime_handle.send(None) # get the inverse of multiplier self.inverse = gcd.modinv(self.multiplier, self.modulo) self.private_key = key # return the private key return self.private_key
def generate_keys(): # to obtain random seeds random.seed(time.time()) # generate a random number startforp = random.randint(100, 200) # get the generator function prime_handle = prime.get_primes(startforp) # get the first prime number p = prime_handle.send(None) # put a respectable gap between the two startforq = startforp + 100 # get the second prime number q = prime_handle.send(startforq) # obtain n n = p * q # obtain the euler totient of p and q phi = (p - 1) * (q - 1) # choosing standard value of e e = 65537 # Use Extended Euclid's Algorithm to generate the private key # d = multiplicative_inverse(e, phi) d = gcd.modinv(e, phi) # Return public and private keypair # Public key is (e, n) and private key is (d, n) return ((e, n), (d, n))
def set_max(max): global MAX, primes, primes_set MAX = max t1 = time.time() primes = get_primes(MAX) print time.time()-t1 primes_set = set(primes)
def find_smallest(n): """ >>> find_smallest(n) 2520 """ import prime import functools primes = prime.get_primes(n+1) divisors = list(primes) for p in primes: i = 2 while p**i <= n: divisors.append(p) i += 1 return functools.reduce(lambda x, y: x*y, divisors)
import prime primes = prime.get_primes(1000000) primes_set = set(map(str, primes)) onedigit_primes = [1,2,3,5,7] onedigit_primes_set = set(map(str, onedigit_primes)) sum = 0 for num in primes: #for num in [3797]: if num<10: continue num_s = str(num) if num_s[-1] not in onedigit_primes_set or num_s[0] not in onedigit_primes_set: continue for idx in xrange(len(num_s)-1, 0, -1): if num_s[:idx] not in primes_set: # print "2", num break else: for idx in xrange(1, len(num_s)): if num_s[idx:] not in primes_set:
from __future__ import division from math import sqrt import prime MAX=10000000 min_range = int(sqrt(MAX)*0.5) max_range = int(sqrt(MAX)*1.5) print "min_range", min_range print "max_range", max_range primes = [x for x in prime.get_primes(max_range) if x>min_range] print primes def is_perm(a, b): a_s = str(a) b_s = str(b) if len(a_s) != len(b_s): return False if set(a_s) != set(b_s): return False a_digits = [0]*58 b_digits = [0]*58 for digit in a_s:
""" The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ import prime print(sum(prime.get_primes(2000000)))
from prime import get_primes max_val = 999 target_size = 5 primes = get_primes(max_val) prime_set = set(primes) # Returns all valid primes from glob of x (eg 1*3) def glob_primes(val): result = set() if '*' in val: start = 0 if val[0] == '*': start = 1 for i in range(start, 10): x = int(val.replace('*', str(i))) if x in prime_set: result.add(x) else: val = int(val) if val in prime_set: result.add(val) return result tried_globs = set() for prime in primes:
def test_prime_range(self): primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97 ] self.assertEqual(sorted(primes), sorted(get_primes(1, 100)))
from prime import get_primes, prime_factors max_prime = 300000 primes = get_primes(max_prime) i = 0 while i < max_prime-3: if len(prime_factors(i+3, primes)) == 4: if len(prime_factors(i+2, primes)) == 4: if len(prime_factors(i+1, primes)) == 4: if len(prime_factors(i, primes)) == 4: print(i) else: i += 1 else: i += 2 else: i += 3 else: i += 4
def get_indexed_prime(index): primes = get_primes() return next(x for i, x in enumerate(primes) if i == index)
import prime primes = prime.get_primes(2000000) print sum(primes)
from prime import get_primes max_prime = 10000 max_base = 100 primes = set(get_primes(max_prime)) bases = range(1, max_base+1) sqprimes = set() # Prime + 2*square values for prime in primes: for base in bases: sqprimes.add(prime + (2*(base**2))) oddcomps = set() for x in range(9, max(sqprimes)+1, 2): if x not in primes: oddcomps.add(x) print(sorted(oddcomps-sqprimes)[0])
e.g. |11| = 11 and |4| = 4 Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number of primes for consecutive values of n, starting with n = 0. """ import prime lower_n = 0 upper_n = 79 # a must be odd and a in (-1000,1000) # b must prime and b in [3,997] upper_a = 1000 upper_b = 1000 upper_prime = upper_n * upper_n + upper_a * upper_n + upper_b primes = prime.get_primes(upper_prime) primes_set = set(primes) best_a = 1 best_b = 41 most_primes_generated = 40 for b in filter(lambda p : p < upper_b, primes): for a in range(1-upper_a,upper_a,2): primes_generated = 1 while primes_generated*primes_generated + a*primes_generated + b in primes_set: primes_generated += 1 if primes_generated > most_primes_generated: best_a = a best_b = b most_primes_generated = primes_generated
import argparse from sys import argv from prime import get_primes primes = None """ Verify/Confirm a given number (usually from results.json) """ def check(n): global primes total = sum([next(primes) ** 2 for _ in range(n)]) print(n, total % n == 0) if __name__ == '__main__': primes = get_primes() parser = argparse.ArgumentParser(description='Verify a number (from results)') parser.add_argument('n', metavar='N', type=int, default=19, help='Number to verify') args = parser.parse_args(argv[1:]) check(args.n)