Example #1
0
def get_public_params(len_p):
    assert len_p % 64 == 0
    assert 512 <= len_p <= 1024

    while True:
        q = utils.get_big_prime(2**(160 - 1), 2**160)
        p = q * (2**(len_p - 160))
        # print('p:', len(bin(p)[2:]), 'bit')
        high = 2**len_p
        # print('high:', len(bin(high)[2:]), 'bit')
        while p < high:
            p += q
            if not isprime(p + 1):
                continue
            p += 1
            if (p - 1) % q == 0:
                break
        if isprime(p):
            break
    # print(p)
    # print('p:', len(bin(p)[2:]), 'bit')
    h = 2
    while not pow(h, (p - 1) // q, p) > 1:
        h += 1
    g = pow(h, (p - 1) // q, p)

    return p, q, g
Example #2
0
File: p60.py Project: icot/euler
def checkGroup(nums):
    pairs = combinations(nums, 2)
    try:
        while 1:
            pair = pairs.next()
            n1 = catnums(pair[0], pair[1])
            if isprime(n1):
                n2 = catnums(pair[1], pair[0])
                if not isprime(n2):
                    return False
            else:
                return False
    except StopIteration:
        return True
Example #3
0
def is_valid_n(combo, n):
    valid = False
    v = f(combo, n)
    if v > 2:
        if utils.isprime(v):
            valid = True
    return valid
Example #4
0
def hadamard_s(n):
    if isprime(n) == 0:
        print('error! n must be a prime\n')
        circ_S = 0
        inv_circ_S = 0
        return False
    if (n - 3) % 4 != 0:
        print('error! n must be 4m + 3\n')
        circ_S = 0
        inv_circ_S = 0
        return False

    A_k = np.array(range(1, (n - 1) // 2 + 1))**2
    A_k = A_k % n

    S_1 = np.zeros([1, n])
    S_1[0, 0] = 1
    S_1[0, A_k] = 1

    circ_S = np.zeros([n, n])
    circ_S[0, :] = S_1

    for i in range(2, n + 1):
        circ_S[i - 1, :] = circshift_r(circ_S[i - 2, :], -1)

    pos_measure = circ_S > 0
    pos_measure_non = circ_S == 0
    pos_measure_non_matr = True - circ_S
    pos_measure_matr = circ_S
    pos_measure_matr[pos_measure_non] = 0

    inv_circ_S = (pos_measure_matr - pos_measure_non_matr) / (n + 1) * 2

    return circ_S, inv_circ_S
Example #5
0
def count(n, cur):
    if not digits:
        return [cur + [n]] if isprime(n) and n > max(cur) else []
    res = []

    if isprime(n) and n > max(cur + [0]):
        res += count(0, cur + [n])

    for d in digits:
        digits.remove(d)
        rest = count(n * 10 + d, cur)
        res += rest

        digits.add(d)

    return res
Example #6
0
def sieve(n):
    prime_list = [2]
    for x in range(3, n + 1):
        if isprime(x) == True:
            prime_list.append(x)

    return prime_list
Example #7
0
def get_full_size(block_number):
    sz = DATASET_BYTES_INIT + DATASET_BYTES_GROWTH * (block_number //
                                                      EPOCH_LENGTH)
    sz -= MIX_BYTES
    while not isprime(sz / MIX_BYTES):
        sz -= 2 * MIX_BYTES
    return sz
Example #8
0
def generator_elliptic_curve(l, m):
    while True:
        while True:
            p = get_prime(l)
            if p % 4 == 1:
                break

        a, b = complex_decomposition(1, p)
        assert a * a + b * b == p
        T = [-2 * a, -2 * b, 2 * a, 2 * b]
        for t in T:
            N = p + 1 + t
            if isprime(N // 2):
                r = N // 2
                break
            if isprime(N // 4):
                r = N // 4
                break
        else:
            continue

        good = True
        for i in range(1, m):
            if (p**i) % r == 1:
                good = False
        if p == r or not good:
            continue

        while True:
            e = EllipticPoint(p)
            A = ((e.y**2 - e.x**3) * inverse(e.x, p)) % p
            good = False
            if N == r * 2:
                if legendre_symbol(-A, p) == -1:
                    good = True
            if N == r * 4:
                if legendre_symbol(-A, p) == 1:
                    good = True
            if not good:
                continue

            m = EllipticPoint.mul(e, N, A, p)
            if m == EllipticPoint(p, -1, -1):
                break
        Q = EllipticPoint.mul(e, N // r, A, p)
        return p, A, Q, r
Example #9
0
def main():
    i, count = 2, 0
    while True:
        if isprime(i):
            count += 1
            if count == 10001:
                return i
        i += 1
def prime_factors(number):
    prime_list =[]
    for x in xrange(2, number+1):
        while number % x == 0:
            if isprime(x):
                prime_list.append(x)
                number = number / x

    return prime_list
Example #11
0
def sieve(n):
    prime_list = []
    for x in range(2, n+1):
        if x == 2:
            prime_list.append(x)
        elif isprime(x) == True:
            prime_list.append(x)

    return prime_list
Example #12
0
File: p122.py Project: icot/euler
def M(k):
    if k <= 1:
        return 0
    else:
        if isprime(k):
            return (M(k-1) + 1)
        else:
            ds = factors(k)
            ks = map(lambda x: m[x], ds)
            # print k, ds, ks
            return sum(ks)
Example #13
0
def main():
    digits = "987654321"

    pandigitals = []
    for i in reversed(range(1,10)):
        choices = digits[-i:]
        pandigitals.append(map("".join, itertools.permutations(choices, i)))
        
    for i in range(0, 9):
        for p in pandigitals[i]:
            if utils.isprime(int(p)):
                return p
Example #14
0
def is_valid(combo):
    valid = False
    for n in range(40):
        v = f(combo, n)
        if v > 2:
            if utils.isprime(v):
                if n == 39:
                    valid = True
            else:
                break
        else:
            break
    return valid
Example #15
0
def main():
    prime_numbers = utils.primes(10000)
    odd_composites = [n for n in range(2, 10000) if (utils.isprime(n) == False) & (n % 2 != 0)]

    for odd_composite in odd_composites:
        number_found = False
        for prime in prime_numbers:
            difference = odd_composite - prime
            if difference > 0:
                if np.sqrt(difference / 2) == int(np.sqrt(difference/2)):
                    number_found = True
        if number_found == False:
            return odd_composite
Example #16
0
 def __init__(self,
              a: int,
              b: int,
              p: int,
              order: int = None,
              verify: bool = True):
     self.p = p
     self.a = a % p
     self.b = b % p
     a4 = 4 * (self.a**3)
     b27 = 27 * (self.b**2)
     a4b27 = a4 + b27
     self.discriminant = -16 * a4b27
     if verify:
         assert isprime(
             p), f"{p} is not prime and verify=True in curve params"
         assert self.discriminant != 0, f"discriminant {self.discriminant} != 0 and verify=True in curve params"
     self.j_invariant = 1728 + (a4 * modinv(a4b27, self.p))
     self.card = order
Example #17
0
from utils import primes, is_prime as isprime
from collections import defaultdict

ps = primes(10**4)[1:]

concatenable = {p: set() for p in ps}

n = len(ps)

for p in ps:
    for q in ps:
        if isprime(int(str(p) + str(q))):
            concatenable[p].add(q)

for p in ps:
    for q in concatenable[p]:
        if q <= p: continue
        if p not in concatenable[q]: continue
        for r in concatenable[p] & concatenable[q]:
            if r <= q: continue
            if p not in concatenable[r] or q not in concatenable[r]: continue
            for s in concatenable[p] & concatenable[q] & concatenable[r]:
                if s <= r: continue
                if p not in concatenable[s] or q not in concatenable[
                        s] or r not in concatenable[s]:
                    continue
                for t in concatenable[p] & concatenable[q] & concatenable[
                        r] & concatenable[s]:
                    if t <= s: continue
                    if set([p, q, r, s]) <= concatenable[t]:
                        print p, q, r, s, t, p + q + r + s + t
Example #18
0
            if (j + off) % p == 0:
                break
        else:
            res.append(i)
    return res


limit = 15 * 10**7
res = 0

mods = {p: calc_mods(p) for p in primes(500)}

for n in xrange(0, limit + 1, 210):
    for off in (10, 80, 130, 200):
        k = n + off
        possib = True
        for p in mods:
            if k * k > p and k % p not in mods[p]:
                possib = False
                break
        if not possib: continue
        k = (n + off)**2
        ps = []
        for j in range(k + 1, k + 28, 2):
            if isprime(j):
                ps.append(j)
        if ps == [k + 1, k + 3, k + 7, k + 9, k + 13, k + 27]:
            res += n + off

print res
Example #19
0
    koek_odd_even = []
    for l in koek.splitlines():
        koek_odd_even.append(' '.join(
            ['A' if int(c) % 2 == 0 else 'B' for c in l.split()]))
    return '\n'.join(koek_odd_even)


koek_odd_even = []
for l in koek.splitlines():
    koek_odd_even.append(['A' if int(c) % 2 == 0 else 'B' for c in l.split()])
print(koek_odd_even)

koek_prime = []
for l in koek.splitlines():
    koek_prime.append(
        ['B' if utils.isprime(int(c)) else 'A' for c in l.split()])

print('Converted to odd/even')
for koek_msg in [koek_odd_even, koek_prime]:

    print('Reading from left to right, horizontally, top to bottom')

    print(''.join(koek_msg))

    msg = ''.join([''.join(l) for l in koek_msg])
    print(msg)
    print('version1, noswap: {}'.format(
        cipher.bacon_decrypt(msg, version=1, swapAB=False)))
    print('version1,   swap: {}'.format(
        cipher.bacon_decrypt(msg, version=1, swapAB=True)))
    print('version2, noswap: {}'.format(
Example #20
0
def next_prime(n):
    n += 1
    while not isprime(n):
        n += 1
    return n
Example #21
0
def check_number():
    if not number[0]: return 0
    n = int("".join(map(str, number)))
    return n if isprime(n) else 0
Example #22
0
from utils import is_prime as isprime
limit = 2000
count = 1
n = 1

while True:
    if isprime(6 * n - 1) and isprime(6 * n + 1) and isprime(12 * n + 5):
        count += 1
        if count == limit:
            print 3 * n * n - 3 * n + 2
            break
    if n > 1 and isprime(6 * n - 1) and isprime(6 * n + 5) and isprime(12 * n -
                                                                       7):
        count += 1
        if count == limit:
            print 3 * n * n + 3 * n + 1
            break
    n += 1
Example #23
0
els = set([1])

while q:
    el = heappop(q)
    if el in els: continue
    els.add(el)
    if el * 2 <= limit:
        heappush(q, el * 2)
        if el * 3 <= limit:
            heappush(q, el * 3)
            if el * 5 <= limit:
                heappush(q, el * 5)
primes = []
for el in els:
    val = el + 1
    if 6 <= val <= limit and isprime(val):
        primes.append(val)

primes = sorted(primes)
choices = [1]
final = []
for prime in primes:
    newchoices = []
    for choice in choices:
        if choice * prime <= limit:
            newchoices.append(choice)
            newchoices.append(choice * prime)
        else:
            final.append(choice)
    choices = newchoices
Example #24
0
File: p156.py Project: icot/euler
#!/usr/bin/python2.7

from utils import isprime

def p(n, c):
    return n*n + c

if __name__ == "__main__":
    limit = 1000000
    cs = (3, 7, 9, 13, 27)
    print "Generating groups"
    groups = [[n, p(n, 1)] for n in xrange(limit) if isprime(p(n,1))]
    for c in cs:
        groups = [item + [p(item[0], c)] for item in groups]
        groups = filter(lambda x: isprime(x[-1]), groups)
        print "Filter: ", len(groups)
    print "Suma: ", sum(cs)

Example #25
0
"""
Project Euler Problem 7
=======================

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see
that the 6th prime is 13.

What is the 10001st prime number?
"""

from utils import isprime


INDEX = 10001
nprimes = 0
i = 1
while True:
    if isprime(i):
        nprimes += 1
    if nprimes == INDEX + 1:
        break
    i += 1

print i

def prime_(x, y):
    return utils.isprime(x) or utils.isprime(y)
Example #27
0
def get_cache_size(block_number):
    sz = CACHE_BYTES_INIT + CACHE_BYTES_GROWTH * (block_number // EPOCH_LENGTH)
    sz -= HASH_BYTES
    while not isprime(sz / HASH_BYTES):
        sz -= 2 * HASH_BYTES
    return sz