Beispiel #1
0
def istruncatable(n):
  old = 0
  v = 1
  while n:
    (n, r) = divmod(n, 10)
    old = r * v + old
    if (n and not isprime(n)) or (old and not isprime(old)):
      return False
    v *= 10
  return True
Beispiel #2
0
def _prime(num, direction):
    key = (num, direction)
    if key in memo:
        return memo[key]

    recurse = num[1:] if direction == 1 else num[:-1]
    if _prime(recurse, direction):
        result = isprime(int(num))
    else:
        result = False

    memo[key] = result
    return result
Beispiel #3
0
def _prime(num, direction):
    key = (num, direction)
    if key in memo:
        return memo[key]

    recurse = num[1:] if direction == 1 else num[:-1]
    if _prime(recurse, direction):
        result = isprime(int(num))
    else: 
        result = False

    memo[key] = result
    return result
def computing_square_roots(a, b, p, x):
    """
    Handbook of Applied cryptography by Menezes -- Algorithm 3.36
    y^2 = x^3 + ax + b (mod p) let equ_right = x^3 + ax + b (mod p)
    OUTPUT: the two square roots of equ_right modulo p, provided equ_right is a quadratic residue modulo p.
    params:
        -- a, integer
        -- b, integer
        -- p, integer: an odd prime 
        -- x, integer: horizontal coordinate 
    return the square roots
    """
    assert p % 2 != 0 and isprime(p), "%d is not a odd prime number!"% p
    equ_right = elliptic_curve_equation(a, b, p, x)
    symbol = legendre_symbol(equ_right, p)
    if symbol == -1:
        print(f"{equ_right} does not have a square root modulo {p}")
        return
    # step2: get b
    b = random.randint(1, p-1)
    while legendre_symbol(b, p) != -1:
        b = random.randint(1, p-1)
    # step3: get s, t
    s, t = decompose_integer_by_two(p - 1)
    # step4: Compute a^-1 mod p
    inver_a = get_multiplicative_inverse(equ_right, p)
    # step5: 
    c = b ** t % p
    r = get_modular_exponentiation_from_left(equ_right, (t+1)//2, p)
    # step6:
    for i in range(1, s):
        d = pow((r ** 2 * inver_a), pow(2, s - i - 1)) % p
        if d % p == -1:
            r = r * c % p
            c = c ** 2 % p
    r = abs(r)
    return (r, p-r)
Beispiel #5
0
from itertools import permutations, combinations, combinations_with_replacement as cwr
import sys

from util import isprime

# iterate over each combination of 4 numbers
for digits in cwr('0123456789', 4):

    # all permutations of digits, in numerical order
    perms = [int(''.join(p)) for p in permutations(digits)]
    perms = sorted(perms)

    # each increasting pair
    for t1, t2 in combinations(perms, 2):
        if t1 < 1000: continue  # must be 4 digits
        if t2 == t1: continue  # must be increasing
        if t1 == 1487: continue
        t3 = t2 + (t2 - t1)
        if not isprime(t1) or not isprime(t2): continue
        if not isprime(t3) or t3 not in perms: continue
        print "%4.4i%4.4i%4.4i" % (t1, t2, t3)
        sys.exit(1)
Beispiel #6
0
from util import isprime

num = 1
x = 3
prime = 2
stop = 10001
while (num < stop):
    if isprime(x):
        num += 1
        prime = x
    x += 2

print prime
Beispiel #7
0
def test(a, b):
    if not isprime(int(str(a) + str(b))): return False
    if not isprime(int(str(b) + str(a))): return False
    return True
Beispiel #8
0
def is_circular_prime(num): # num is a string
    for i in range(len(num)):
        shift = int(num[i:] + num[:i])
        if not isprime(shift): 
            return False
    return True
Beispiel #9
0
import sys
from util import isprime

for i in xrange(3, 10000000, 2):
    if isprime(i): continue
    solved = True
    for n in xrange(1, i):
        k = i - 2 * n**2
        if k < 0:
            break
        if isprime(k):
            solved = False
            break
    if solved:
        print i
        sys.exit()
Beispiel #10
0
from util import isprime

sum = 2
for i in range(3, 2000000, 2):
    if isprime(i): sum += i

print sum
Beispiel #11
0
from util import isprime
from itertools import permutations

#can exclude following ns:
#1-3 (example gives a 4-digit pandigital)
#5,6,8,9 (divisible by 3)
#thus, look at 7 then 4
#that leaves 4! + 7! = 5064 permutations to search through
#order permutations in reverse order to speed up search

for p in permutations('7654321'):
    num = int(''.join(p))
    if isprime(num):
        break
print num
Beispiel #12
0
from util import isprime


num = 1
x = 3
prime = 2
stop = 10001
while(num < stop):
    if isprime(x): 
        num += 1
        prime = x
    x += 2

print prime

Beispiel #13
0
#only ~4Million combinations. Just check

from util import isprime

best = (0, 0, 0)
#the n=0 term is always b. Only consider prime bs
bs = filter(isprime, range(-999, 1000))

for a in range(-999, 1000):
    for b in bs:
        #count prime sequence
        n = 0
        while isprime(n**2 + a * n + b):
            n += 1
        if n > best[0]:
            best = (n, a, b)

print best[0], best[1] * best[2]
Beispiel #14
0
def is_circular_prime(num):  # num is a string
    for i in range(len(num)):
        shift = int(num[i:] + num[:i])
        if not isprime(shift):
            return False
    return True
Beispiel #15
0
def test(a, b):
    if not isprime(int(str(a) + str(b))): return False
    if not isprime(int(str(b) + str(a))): return False
    return True
Beispiel #16
0
import sys
from util import isprime

for i in xrange(3, 10000000, 2):
    if isprime(i): continue
    solved = True
    for n in xrange(1, i):
        k = i - 2 * n ** 2
        if k < 0: 
            break
        if isprime(k): 
            solved = False
            break
    if solved:
        print i
        sys.exit()
Beispiel #17
0
from itertools import permutations, combinations, combinations_with_replacement as cwr
import sys

from util import isprime

# iterate over each combination of 4 numbers
for digits in cwr("0123456789", 4):

    # all permutations of digits, in numerical order
    perms = [int("".join(p)) for p in permutations(digits)]
    perms = sorted(perms)

    # each increasting pair
    for t1, t2 in combinations(perms, 2):
        if t1 < 1000:
            continue  # must be 4 digits
        if t2 == t1:
            continue  # must be increasing
        if t1 == 1487:
            continue
        t3 = t2 + (t2 - t1)
        if not isprime(t1) or not isprime(t2):
            continue
        if not isprime(t3) or t3 not in perms:
            continue
        print "%4.4i%4.4i%4.4i" % (t1, t2, t3)
        sys.exit(1)
Beispiel #18
0
#only ~4Million combinations. Just check

from util import isprime

best = (0, 0, 0)
#the n=0 term is always b. Only consider prime bs
bs = filter(isprime, range(-999, 1000))

for a in range(-999, 1000):
    for b in bs:
        #count prime sequence
        n = 0        
        while isprime(n**2 + a * n + b):
            n += 1
        if n > best[0]:
            best = (n, a, b)

print best[0], best[1] * best[2]
            
Beispiel #19
0
from util import isprime


sum = 2
for i in range(3, 2000000, 2):
    if isprime(i):
        sum += i

print sum