Example #1
0
def generate_key_pair(bits_size):
    # find primes
    p, q = get_primes(bits_size)

    # evaluate totiente
    phi_n = (p - 1) * (q - 1)

    # evaluate modulus
    n = p * q

    print("\n")

    # evaluate modular inverse of e
    founded_inverse = True
    e, gcd, a, b = 0, 0, 0, 0
    while founded_inverse:
        e = generate_e(phi_n)
        gcd, a, b = extended_euclidean(e, phi_n)
        if (gcd == 1 and (gcd == (a * e) + (phi_n * b))):
            if (a * e % phi_n == 1 and 0 <= a <= n):
                founded_inverse = False

    # d mod (p - 1).
    exp1 = a % (p - 1)
    # d mod (q - 1).
    exp2 = a % (q - 1)
    # q^(-1) mod p.
    coef = (q ^ -1) % p

    public_key = generate_public_key(e, n)
    private_key = generate_private_key(e, n, a, p, q, exp1, exp2, coef)

    print("\n")
    print("-----BEGIN RSA PUBLIC KEY-----")
    encoded_public_key = base64.b64encode(public_key)
    print(encoded_public_key.decode())
    print("-----END RSA PUBLIC KEY-----")
    print("\n\n")

    print("-----BEGIN RSA PRIVATE KEY-----")
    encoded_private_key = base64.b64encode(private_key)
    print(encoded_private_key.decode())
    print("-----END RSA PRIVATE KEY-----")
#!/usr/bin/python
from utils.primes import get_primes

MAX = 100

primes_dict = get_primes(MAX)
primes = [i for i in xrange(2, MAX) if primes_dict[i]]
cache = {}
def update_answer(n, max_element=None):
  assert n >= 0, "n must be non-negative %d" % n
  if n == 0:
    return 1
  if n == 1:
    return 0
  max_element = min(n, max_element or n)
  if n in cache and max_element in cache[n]:
    return cache[n][max_element]
  count = 0
  i = 0
  while i < len(primes) and primes[i] <= max_element:
    p = primes[i]
    count += update_answer(n - p, min(p, max_element))
    i = i + 1
  cache_n = cache.get(n, {})
  cache_n[max_element] = count
  cache[n] = cache_n
  return count

for i in xrange(MAX):
  res = update_answer(i)
  if (res > 5000):
def test(group, p, primes, candidates):
  for g in group:
    if not (isprime(g*(10**int(math.log(p,10) + 1)) + p, primes, candidates) and
      isprime(p*(10**int(math.log(g,10) + 1)) + g, primes, candidates)):
      return False
  return True


def extend(source, candidates, primes):
  result = []
  for g in source:
    m = max(g)
    for c in (i for i in candidates if i > m):
      if test(g, c, primes, candidates):
        result.append(g + [c])
  return result

def find(primes, candidates, size, found=None):
  if found == None:
    return find(primes, candidates, size, [[i] for i in candidates])
  while len(found) > 0 and len(found[0]) < size:
    found = extend(found, candidates, primes)
  return found

MAX = 9000
SIZE = 5
primes = get_primes(MAX_P)
candidates = [k for k,v in primes.iteritems() if k <= MAX and v]
print sum(find(primes, candidates, SIZE)[0])

#!/usr/bin/python
from utils.primes import get_primes

import math

MAX = 50000000

max_prime = int(math.sqrt(MAX))
primes = get_primes(max_prime)
primes = [p for p in primes if primes[p]]

found = set()
for p1 in primes:
  v1 = p1 ** 4
  if v1 >= MAX:
    break
  for p2 in primes:
    v2 = p2 ** 3
    if v1 + v2 >= MAX:
      break
    for p3 in primes:
      v3 = p3 ** 2
      v = v1 + v2 + v3
      if v >= MAX:
        break
      found.add(v)
print len(found)

Example #5
0
import math

def check(num):
	if '200' not in str(num):
		return False
	for i in xrange(len(str(num))):
		for j in range(0, 10):
			new_num = int(str(num)[:i] + str(j) + str(num)[i + 1:])
			if is_prime(new_num):
				return False
	return True


if __name__ == "__main__":
	L = 10 ** 12
	primes = get_primes(int(math.sqrt(L)))
	q_upperbound = int((L / 4) ** (1.0 / 3))
	ans = []
	for q in primes:
		if q > q_upperbound:
			break
		p_upperbound = int((L / (q ** 3)) ** (1.0 / 2))
		for p in primes:
			if p > p_upperbound:
				break
			if p == q:
				continue
			num = (p ** 2) * (q ** 3)
			if check(num):
				ans.append(num)
	print sorted(ans)[199]
import math
MAX = 10**7
HIGH_BOUND = int(math.sqrt(MAX) * 2)

def permutations(n1, n2):
  l1, l2 = list(str(n1)), list(str(n2))
  if len(l1) != len(l2):
    return False
  for c in l1:
    if not c in l2:
      return False
    l2.remove(c)
  return len(l2) == 0

primes = get_primes(HIGH_BOUND)
primes = [i for i in xrange(2, HIGH_BOUND) if primes[i]]

result = 0
max_found = 0
for p1 in primes:
  for p2 in primes:
    if p1 == p2 or p1 * p2 >= MAX:
      continue
    n, phi_n = p1 * p2, (p1 - 1) * (p2 - 1)
    if not permutations(n, phi_n):
      continue
    if float(phi_n) / n > max_found:
      max_found = float(phi_n) / n
      result = n
print result