def __init__(self, p=None, q=None, a=None, b = None, n=None, nb_bits=4096): """Initializes the magic keys. """ if p is None and q is None and a is None: # 1. Generates 'p' and 'q' prime numbers of lengths 'nb_bits' in a # random fashion. if p is None and q is None: p = random.getrandbits(nb_bits) q = random.getrandbits(nb_bits) while not utils.miller_rabin(p): p = random.getrandbits(nb_bits) while not utils.miller_rabin(q): q = random.getrandbits(nb_bits) if a is None and b is None and n is None: # 2. Generates 'a', 'b' and 'n' thanks to 'phi' and 'b'. n = p * q phi = (p - 1) * (q - 1) if b is None: while True: b = random.randint(2, phi - 1) if utils.is_prime_together(b, phi): break a = utils.inv_modulo(b, phi) self.private = a self.public = b self.modulus = n
def dfs(rem, s, prev): if len(rem) == 0 and s == 0: return 1 cnt = 0 for d in rem: next_s = s * 10 + d if next_s > prev and utils.miller_rabin(next_s): cnt += dfs(rem - set([d]), 0, next_s) cnt += dfs(rem - set([d]), next_s, prev) return cnt
def dfs(d, flag, cur, i): if (i == N): if utils.miller_rabin(cur): return cur return 0 if flag[i]: return dfs(d, flag, cur * 10 + d, i + 1) s = 1 if i == 0 else 0 ret = 0 for k in range(s, 10): ret += dfs(d, flag, cur * 10 + k, i + 1) return ret
def count(n, n_list): return sum(utils.miller_rabin(abs(n - e)) for e in n_list)
""" import utils SUP = 100000 def cycle_len(mod): rems = set() n = 1 while n not in rems: rems.add(n) n = (n * 10 + 1) % mod return len(rems) def check(n): while n % 2 == 0: n //= 2 while n % 5 == 0: n //= 5 return n == 1 ans = 0 for i in range(2, SUP): if utils.miller_rabin(i): if i == 2 or i == 5 or not check(cycle_len(i)): ans += i print(ans)
def check(p, q): return utils.miller_rabin(numcat(p, q)) and utils.miller_rabin(numcat(q, p))
# -*- coding: utf-8 -*- """ Created on Wed Apr 18 14:01:45 2018 @author: lamwa """ import utils SUP = 10 ** 6 ans = 0 a = 1 while True: p = (a + 1) ** 3 - a ** 3 if p >= SUP: break if utils.miller_rabin(p): ans += 1 a += 1 print(ans)
# -*- coding: utf-8 -*- """ Created on Wed Mar 21 13:57:20 2018 @author: lamwa """ import utils cur = 0 cnt = 1 n = 1 while True: if utils.miller_rabin(4 * n * n - 2 * n + 1): cur += 1 if utils.miller_rabin(4 * n * n + 1): cur += 1 if utils.miller_rabin(4 * n * n + 2 * n + 1): cur += 1 if utils.miller_rabin(4 * n * n + 4 * n + 1): cur += 1 cnt += 4 if cur / cnt < 0.1: print(n * 2 + 1) break n += 1
# -*- coding: utf-8 -*- """ Created on Mon Apr 25 16:11:11 2018 @author: lamwa """ import utils SUP = 150000000 ans = 0 for n in range(10, SUP, 10): if n % 1000000 == 0: print(n) s = n * n if (s % 3 != 1): continue if (s % 7 != 2 and s % 7 != 3): continue if (s % 9 == 0 or s % 13 == 0 or s % 27 == 0): continue if ( utils.miller_rabin(s + 1) and \ utils.miller_rabin(s + 3) and \ utils.miller_rabin(s + 7) and \ utils.miller_rabin(s + 9) and \ utils.miller_rabin(s + 13) and \ not utils.miller_rabin(s + 19) and \ not utils.miller_rabin(s + 21) and \ utils.miller_rabin(s + 27)): ans += n print(ans)