def generate_safe_prime(bits): while True: print 'trying prime' p_ = millerrabin.generateLargePrime(bits-1) if type(p_) is str: print 'failed to find prime, trying again' continue p = 2*p_ + 1 if millerrabin.is_probable_prime(p): return p else: 'not a safe prime, trying again'
def generate_safe_prime(bits): while True: print 'trying prime' p_ = millerrabin.generateLargePrime(bits - 1) if type(p_) is str: print 'failed to find prime, trying again' continue p = 2 * p_ + 1 if millerrabin.is_probable_prime(p): return p else: 'not a safe prime, trying again'
def totient(n): """ >>> totient(20) 8 >>> totient(97) 96 >>> totient(56) 24 """ if is_probable_prime(n): return n - 1 f = factors(n) # use (p - 1) / p instead of 1 - 1./p to avoid floating point error return n * reduce(mul, ((p - 1) for p in f)) / reduce(mul, f)
def totient(n): """ >>> totient(20) 8 >>> totient(97) 96 >>> totient(56) 24 """ if is_probable_prime(n): return n - 1 f = factors(n) # use (p - 1) / p instead of 1 - 1./p to avoid floating point error return n * reduce(mul, ( (p-1) for p in f )) / reduce(mul, f)
def factors(number): """ >>> factors(3) [3] >>> factors(4) [2] >>> factors(6) [2, 3] >>> factors(12) [2, 3] """ if is_probable_prime(number): return [number] for p in generator(): if number % p == 0: while number % p == 0: number = number / p if number == 1: return [p] else: return [p] + factors(number)
from millerrabin import is_probable_prime # .8s instead of 130s using is_prime n = 1 # circle counter total = 1 # 1 at the center belongs to all diagonal prime = 0 while True: for i in (0, 2, 4, 6): # testing diagonal values for primality if is_probable_prime((2 * n + 1)**2 - i * n): prime += 1 total += 1 # testing ratio if prime * 100 / total < 10: # the side lenght is ... print 2 * n + 1 break # next circle n += 1
from millerrabin import is_probable_prime # .8s instead of 130s using is_prime n = 1 # circle counter total = 1 # 1 at the center belongs to all diagonal prime = 0 while True: for i in (0, 2, 4, 6): # testing diagonal values for primality if is_probable_prime( (2 * n + 1) ** 2 - i * n ): prime += 1 total += 1 # testing ratio if prime * 100 / total < 10: # the side lenght is ... print 2 * n + 1 break # next circle n += 1
def valid(a, b): s1 = str(a)+str(b) s2 = str(b)+str(a) if is_probable_prime(int(s1)) and is_probable_prime(int(s2)): return True return False