def __init__(self): self.dudes = [] self.voices = [] self.voters = { 'Arkady': False, 'Kirill': False, 'Random Babka': False, 'Ded': False, 'Grisha': False } self.P, self.Q = gen_prime(1024), gen_prime(1024) self.sub = Subscriber(self.P, self.Q) self.N = self.sub.N self.c = self.sub.c self.d = self.sub.d
def gen_pnr(size, m): while True: # 1 шаг p = prime.gen_prime(size) assert p % 4 == 1 assert sympy.isprime(p) # 2 шаг (страница 168, алгоритм 7.8.1) a, b = get_zi_factors(p, 1) assert a * a + b * b == p # 3 шаг t_set = [2 * a, -2 * a, 2 * b, -2 * b] for t in t_set: n = p + 1 + t rs = [n // 2, n // 4] for r in rs: if prime.isprime(r): assert sympy.isprime(r) # шаг 4 if p != r: for i in range(1, m + 1): if pow(p, i, r) == 1: break else: return p, n, r
def solution2(): primes = gen_prime(10000) for i in range(1,len(primes)): lst = [] for j in range(i+1, len(primes)): if prime_concat(primes[i], primes[j]): lst.append([primes[i],primes[j]]) solve(lst)
def gen_params(bit_count): p = gen_prime(bit_count) q = gen_prime(bit_count) n = p * q phi = (p - 1) * (q - 1) e = randint(2, phi - 1) while euclid(e, phi) > 1: e = randint(2, phi - 1) d = get_inverse(e, phi) write('pq.txt', '{}\n{}'.format(p, q)) write('phi.txt', str(phi)) write('open.txt', '{}\n{}'.format(n, e)) write('secret.txt', str(d))
def main(): primes = gen_prime(1000) target = 2 while True: result = solve_prime_plus(target, [k for k in primes if k <= target]) if result > 5000: break target += 1 print target
def __init__(self): self.p, self.q = prime.gen_prime() #step 1 get prime numbers #step 2 cal n self.n = self.p * self.q #step 3 cal totient of n self.phi = (self.p - 1) * (self.q - 1) #step 4 cal public key e self.e = calPublicKey.cal(self.phi, self.n) #step 5 cal private key d self.d = calPrivateKey.cal(self.e, self.phi)
def main(): upperbound = 1000000 for prime in gen_prime(upperbound): for number_family in number_families(prime): prime_family = [n for n in number_family if is_prime(n) and len(str(n))==len(str(prime))] if len(prime_family)==8 and prime in prime_family: print '\n',prime return sys.stdout.write("Testing number %d\r"%(prime)) sys.stdout.flush()
def main(): ub = 10**7 pairs = combinations(gen_prime(2*int(sqrt(ub))), 2) result = (87109./79180, 87109) # Becasue primes produce most relative primes, but single prime doesn't # meet the "permutation" criterion. Hence, directly looking up to the # product of two primes for product, phi in ((a*b, (a-1)*(b-1)) for a,b in pairs if a*b<ub): if is_permutation(phi, product): r = float(product)/phi if r < result[0]: result = (r, product) print result
def main(): limit = 50000000 primes = prime.gen_prime(int(limit**0.5)) result = set() for i in primes: for j in primes: sum = i**2+j**3 if sum >= limit: break for k in primes: sum2 = sum+k**4 if sum2 < limit: result.add(sum2) print "Q087 = ", len(result)
def main(): # initial set of prime primes = set([2]) ub = 1000000 primes = gen_prime(ub) print "The length of the primes is ", len(primes) result = [] for i in primes: if is_circular(i, primes): result.append(i) sys.stdout.write("Complete %d\r" % (i)) sys.stdout.flush() print result print len(result)
def main(): ub = 1000000 startpoint = 10 primes = gen_prime(ub) result = [] for start in range(startpoint): seq = primes[start:] count = 0 total = 0 for prime in seq: total += prime if total > ub: break count += 1 if total in primes: if count > len(result): result = seq[:count] print sum(result)
def main(): primes = gen_prime(10000) pool = [k for k in primes if k>1000] result = [] for num in pool: if num in result: continue perms = next_permutation([k for k in str(num)]) count = 0 temp = [] for perm in perms: if int(''.join(perm)) in pool: count += 1 temp.append(int(''.join(perm))) if count >= 3: temp.sort() if temp[2]-temp[1]==temp[1]-temp[0]: result += temp print result
def main(): primes = gen_prime(10000) # any number concatenate 2 would generate even number. for first in range(1,len(primes)): for second in range(first+1,len(primes)): if not prime_concat(primes[first], primes[second]): continue temp = [primes[first], primes[second]] for third in range(second+1, len(primes)): if not all([prime_concat(k, primes[third]) for k in temp]): continue temp.append(primes[third]) for fourth in range(third+1, len(primes)): if not all([prime_concat(primes[fourth], k) for k in temp]): continue temp.append(primes[fourth]) for fifth in range(fourth+1, len(primes)): if all([prime_concat(primes[fifth], k) for k in temp]): temp.append(primes[fifth]) print temp print sum(temp) return 0 print "You need to enlarge the searching scope."
def gen_parts(p_size, msg, n, k): if n < k: print('ОШИБКА: число долей={} больше k={}'.format(n, k)) return with open(msg, 'rb') as f: msg = bytearray(f.read()) part_size = ceil(len(msg) / k) q = [ int.from_bytes(msg[i * part_size:(i + 1) * part_size], byteorder='big') for i in range(k) ] p_size_max = max(qi.bit_length() for qi in q) + 1 if p_size_max > p_size: p_size = p_size_max + 1 print('ЛОГ: длина модуля p изменена на {}'.format(p_size)) p = gen_prime(p_size) mat = gen_mat(p, n, k, q) write('p.txt', p) for idx, part in enumerate(mat): write('part_{}.txt'.format(idx + 1), part)
import sys from prime import gen_prime def cal_formula(a, b): return lambda n: n**2+a*n+b # initial set of prime ub = 10000 primes = set(gen_prime(ub)) # initial state for the searching ar, br = 1, 41 nprime = 40 for a in range(-999, 1000): for b in range(2, 1000): formula = cal_formula(a,b) n = 0 while True: result = formula(n) if not result in primes: break else: n +=1 if n > nprime: nprime = n ar = a br = b sys.stdout.write("Complete a = %d\r"%(a)) sys.stdout.flush()
def test_returns_correct_ouput_for_small_values(self): n=prime.gen_prime(10) self.assertEqual(n,[2,3,5,7],"The return value is expected to be correct for input 10")
def test_returns_a_message_for_wrong_input(self): msg=prime.gen_prime([]) self.assertEqual(msg,"Only an integer is allowed","The function should return a message telling user about wrong input")
def test_negative(self): self.assertEqual(gen_prime(-10),"only positive numbers allowed")
def test_returns_correct_ouput_for_large_values(self): n=prime.gen_prime(100) self.assertEqual(n,[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],"The return value is expected to be correct for input 100")
def test_integer(self): self.assertEquals(gen_prime("20"), 'Input must be an integer')
def test_it_works(self): self.assertEquals(gen_prime(7), [2, 3, 5, 7])
def test_result_not_none(self): self.assertNotEqual( gen_prime(10), None)
def test_negative_input(self): self.assertEquals(gen_prime(-5), 'Input cannot be negative')
def test_for_string(self): self.assertEqual(gen_prime("100"),"Invalid Input!!Only numbers allowed")
def test_for_input_greater_than_2(self): self.assertEqual(gen_prime(1),"Input must be greater than 2")
def test_returns_empty_list_if_no_primes(self): n=prime.gen_prime(1) self.assertEqual(n,[],"The return value is expected to be correct if no primes are found")
def test_empty_inputs(self): self.assertEquals(gen_prime(""), 'Input cannot be empty')
def test_returns_a_list(self): a=prime.gen_prime(0) self.assertTrue(isinstance(a,list),"The returned output should be a list")
#! /usr/bin/env python from prime import gen_prime from mod.point import Point primes = gen_prime() p10 = [] for i in range(10): p10.append(next(primes)) print(', '.join(map(str, p10))) # 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 a = Point(2, 4) b = Point(-3, 6) print(a - b) a += b print(a) b -= a print(-b)
def test_result_not_string(self): self.assertNotEqual( gen_prime(10), "")
def gen_params(pq_size, k): p = prime.gen_prime(pq_size) q = prime.gen_prime(pq_size) n = p * q write('n.txt', n) write('k.txt', k)
def test_gen_prime(self): self.assertEqual(gen_prime(10),[2,3,5,7])
def test_result_not_a_dictionary(self): self.assertNotEqual( gen_prime(10), {})
def test_output(self): self.assertIsInstance(gen_prime(20), list, 'The output must be a list')
import sys, os sys.path.append(os.path.abspath('..')) import prime print sum(prime.gen_prime(2000000))
def test_for_large_no(self): self.assertEqual(gen_prime(100),[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])