Ejemplo n.º 1
0
def test():
    print("----- Unit Test ----- ")
    a = randint(1, 100000)
    b = randint(1, 100000)
    assert em.lb(a) == int(math.log(a, 2)), "lb() assert failed!"
    assert em.log(a, 8) == int(math.log(a, 8)), "log() assert failed!"
    assert em.sqrt(a) == int(math.sqrt(a)), "sqrt() assert failed!"
    assert em.gcd(a, b) == math.gcd(a, b), "powmod() assert failed!"
    assert em.mulmod(a, b, 100) == a * b % 100, "mulmod() assert failed!"
    assert em.powmod(a, b, 100) == pow(a, b, 100), "powmod() assert failed!"
    t = em.randrange(min(a, b), max(a, b))
    assert min(a, b) <= t and t < max(a, b), "randrange() assert failed!"
    print("numeric passed!")

    assert em.isprime(primet)
    assert all(prime50[i] == em.primes(50)[i] for i in range(len(prime50)))
    assert all(prime100[i] == em.primes(100)[i] for i in range(len(prime100)))
    em.clearprimes()
    assert all(prime100[i] == em.nprimes(len(prime100))[i] for i in range(len(prime100)))
    plist = list(islice(em.iterprimes(), len(prime100)))
    assert all(prime100[i] == plist[i] for i in range(len(prime100)))
    assert em.isprime(primet)
    em.clearprimes()
    assert em.factors(123456789) == fac123456789
    print("prime passed!")
Ejemplo n.º 2
0
Archivo: 46.py Proyecto: cmpute/CodeJog
def check(number):
    if isprime(number):
        return True  # skip non-composite
    for i in count(1):
        s = number - 2 * i * i
        if s <= 0:
            return False
        elif isprime(s):
            return True
Ejemplo n.º 3
0
def count_primes(ab):
    a, b = ab
    if not isprime(b):
        return 0
    counter = 0
    current = b
    while current > 1 and isprime(current):
        current += counter * 2 + 1 + a
        counter += 1
    return counter
Ejemplo n.º 4
0
Archivo: 37.py Proyecto: cmpute/CodeJog
def check(target):
    # left truncatable
    divider = 10
    while divider <= target:
        if not isprime(target % divider):
            return False
        divider *= 10
    # right truncatable
    target //= 10
    while target > 0:
        if not isprime(target):
            return False
        target //= 10
    return True
Ejemplo n.º 5
0
Archivo: 51.py Proyecto: cmpute/CodeJog
def solve(target = TARGET):
    from em import iterprimes, isprime
    from itertools import product

    families = set()
    digits = [str(d) for d in range(10)]

    for prime in iterprimes():
        if prime < 10000:
            continue
        sprime = str(prime)

        masks = [(digit, '*') for digit in sprime]
        for mask in product(*masks):
            family = ''.join(mask)
            if family == sprime: continue # no replaceable digits
            if len(set(sprime[i] for i, d in enumerate(family) if d == '*')) > 1:
                continue

            if family in families:
                continue
            else:
                # This condition is awkwardly implied by the problem description.
                # Without this condition, the result is 111109
                if mask[0] == '*':
                    replacer = digits[1:] # remove 0
                else:
                    replacer = digits

                total = sum(isprime(int(family.replace('*', d))) for d in replacer)
                if total >= target:
                    return prime
                families.add(family)
Ejemplo n.º 6
0
def solve(thres=THRES):
    total = 1.
    total_prime = 0
    d1, d2, d3, d4 = 3, 5, 7, 9

    for n in count(1):
        total += 4
        total_prime += isprime(d1) + isprime(d2) + isprime(d3) + isprime(d4)
        d1 += 8 * n + 2
        d2 += 8 * n + 4
        d3 += 8 * n + 6
        d4 += 8 * n + 8

        if total_prime / total < thres:
            break

    return 2 * n + 1
Ejemplo n.º 7
0
Archivo: 41.py Proyecto: cmpute/CodeJog
def solve():
    '''
    Brute-force solution
    '''

    digits = [str(d) for d in range(1, 10)]
    for size in reversed(range(10)):
        for number in reversed(sorted(permutations(digits[:size]))):
            number = int(''.join(number))
            if isprime(number):
                return number
Ejemplo n.º 8
0
Archivo: 50.py Proyecto: cmpute/CodeJog
def solve(limit=LIMIT):
    from em import primes, isprime
    from itertools import count

    lprimes = primes(limit)
    maxlen = 21
    psum = 0
    for plen in count(maxlen, 2):
        if sum(lprimes[:plen]) > limit:
            return psum
        for start in range(len(lprimes) - plen):
            current = sum(lprimes[start:start + plen])
            if current > limit:
                break
            if isprime(current) and plen > maxlen:
                psum = current
                maxlen = plen
Ejemplo n.º 9
0
def test2(a, b):
    return isprime(int(str(a) + str(b))) and isprime(int(str(b) + str(a)))