def gen(self, fixed_public_key=False): self.p = prime.generate_prime(self.l) self.q = prime.generate_prime(self.l) self.N = self.p * self.q phi = (self.p - 1) * (self.q - 1) # φ(N) = (p - 1) * (q - 1) if fixed_public_key: # using often used public key will cut the time of computation sharply e = often_used_public_key self.public_key = e else: # also supports generating a new public key while True: e = randint(2, phi - 1) if gcd(e, phi) == 1: self.public_key = e break d = ext_euclid(e, phi)[0] while d <= 0: d += phi self.secret_key = d
from heapq import heapify, heappop, heappush, heappushpop from itertools import product from prime import generate_prime from time import time def lcm(a, b): return (a * b) / gcd(a, b) cin, cout = stdin.readline, stdout.write # Start time START = time() LIMIT = 5 * (10**7) primes = list(generate_prime(LIMIT=LIMIT)) numbers = set() ## need to optimize it using dp def find_all(primes, size, iteration=0, value=0): if iteration == 3: if value < LIMIT: numbers.add(value) return for i in xrange(size): new_value = value + pow(primes[i], iteration + 2) if new_value < LIMIT: find_all(primes, size, iteration + 1, new_value) else: break
from prime import generate_prime_sieve, generate_prime, is_prime from time import time def lcm(a, b): return (a * b) / gcd(a, b) cin, cout = stdin.readline, stdout.write # Start time START = time() LIMIT = 10**8 sieve = generate_prime_sieve(LIMIT=LIMIT) primes = [[prime] for prime in generate_prime(LIMIT=int(sqrt(LIMIT)))] cout('Done preprocessing\n') cout('Total prime count: %d\n' % (len(list(primes)))) def is_prime_(num): if num >= LIMIT: return is_prime(num) return sieve[num] def check(my_set): my_set_list = list(my_set) size = len(my_set_list) for i in xrange(size): for j in xrange(i + 1, size): a = int(str(my_set_list[i]) + str(my_set_list[j]))
from fractions import Fraction, gcd from sys import stdin, stdout from bisect import bisect, bisect_left, bisect_right from heapq import heapify, heappop, heappush, heappushpop from time import time from prime import generate_prime def lcm(a, b): return (a * b) / gcd(a, b) cin, cout = stdin.readline, stdout.write # Start time START = time() MOD = 500500507 primes = list(generate_prime(LIMIT=10**7)) heapify(primes) iteration, ans = 500500, 1 for i in xrange(iteration): value = heappop(primes) ans = (ans * value) % MOD heappush(primes, value * value) cout("The numbes is: %d\n" % (ans)) # End Time END = time() seconds = END - START print("Total time %d minutes %d seconds" % (seconds / 60, seconds % 60))