コード例 #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!")
コード例 #2
0
def solve(limit = LIMIT):
    from em import primes
    pset = set(primes(limit))

    # be careful for that '71993' is a rotation of `930719`
    result = [prime for prime in pset if all(
                int(str(prime)[i:] + str(prime)[:i]) in pset 
                for i in range(1, len(str(prime)))
            )]
    return len(result)
コード例 #3
0
ファイル: 49.py プロジェクト: cmpute/CodeJog
def solve():
    lprimes = [p for p in primes(10000) if p > 1000]
    for idx, p1 in enumerate(lprimes):
        for p2 in lprimes[:idx]:
            if not isperm(p1, p2):
                continue

            p3 = 2 * p2 - p1
            if p3 in lprimes and isperm(p2, p3):
                if p3 != 1487:  # given by problem
                    return str(p3) + str(p2) + str(p1)
コード例 #4
0
ファイル: 47.py プロジェクト: cmpute/CodeJog
def pfactor(target):
    flist = set()
    for prime in primes(sqrt(target) + 1):
        if target % prime == 0:
            flist.add(prime)
            target //= prime
            while target % prime == 0:
                target //= prime

        if target == 1 or prime > target:
            break
    if target != 1:
        flist.add(target)

    return flist
コード例 #5
0
def solve():
    # In this order we prevent duplicate computation
    for a in iterprimes():
        for plist in cache[4]:
            if testn(a, plist):
                return sum([a] + plist)
        for plist in cache[3]:
            if testn(a, plist):
                cache[4].append([a] + plist)
        for plist in cache[2]:
            if testn(a, plist):
                cache[3].append([a] + plist)
        for p in primes(a):
            if test2(a, p):
                cache[2].append([a, p])
コード例 #6
0
ファイル: 50.py プロジェクト: 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
コード例 #7
0
from em import iterprimes, primes, isprime
from functools import lru_cache

# primes that fulfill the property is far more less, so store true values
cache = {2: list(), 3: list(), 4: list()}
primes(1e6, True)


@lru_cache(maxsize=None)
def test2(a, b):
    return isprime(int(str(a) + str(b))) and isprime(int(str(b) + str(a)))


def testn(a, l):
    for p in l:
        if not test2(a, p):
            return False
    return True


def solve():
    # In this order we prevent duplicate computation
    for a in iterprimes():
        for plist in cache[4]:
            if testn(a, plist):
                return sum([a] + plist)
        for plist in cache[3]:
            if testn(a, plist):
                cache[4].append([a] + plist)
        for plist in cache[2]:
            if testn(a, plist):
コード例 #8
0
ファイル: 10.py プロジェクト: cmpute/CodeJog
def solve(limit=LIMIT):
    from em import primes
    return sum(primes(limit))