예제 #1
0
파일: p152.py 프로젝트: liuliqiu/study
def count_inverse_squares(number):
    prime_list = list(reversed(list(primes(number / 2 + 1))))
    factories = dict(zip(count(3), factorize_to(number)[2:]))

    data = defaultdict(list)
    for n, fact in factories.items():
        data[max(fact.keys())].append((Fraction(1, n * n), (n, )))

    last_choices = [(0, tuple())]
    all_choices = [(0, tuple())]
    for p in prime_list:
        choices = data[p]
        for values, lc in product(power_set(choices, with_empty=False), last_choices):
            values = values + [lc]
            value = sum(v for v, l in values)
            lst = tuple(chain(*[l for v, l in values]))
            if p == 2:
                if value == Fraction(1, 4):
                    yield sorted(lst)
                    print sorted(lst)
            else:
                if value.denominator % p != 0 and value <= Fraction(1, 4):
                    all_choices.append((value, lst))
        last_choices = all_choices
        all_choices = [(0, tuple())]
예제 #2
0
def bruteIt():
    limit = 10000
    s = sieve(limit)
    # Only odds numbers
    for candidate in range(3, limit, 2):
        # Only composite
        if not s[candidate]:
            # Brute : test all the primes smaller than candidate and check the
            # property of the substraction
            isGoldbach = False
            for p in primes(candidate):
                delta = candidate - p
                if (int(math.sqrt(delta/2)) == math.sqrt(delta/2)):
                    isGoldbach = True
                    break

            if not isGoldbach:
                print(candidate)
                break
예제 #3
0
def main():
    ps = primes(10**6)
    biggest = 1
    for start in range(len(ps)):
        acc = 0
        nbTerms = 0

        if len(ps[start:]) < biggest:
            continue

        for p in ps[start:]:
            nbTerms += 1
            acc += p

            if acc > 10**6:
                continue

            if isPrime(acc) and nbTerms > biggest:
                biggest = nbTerms
                print("prime ", acc, " with ", nbTerms, " terms")
예제 #4
0
def main():
    ps = dropwhile(lambda x: x < 1000, primes(10000))
    for prime in ps:
        perms = list(filter(isPrime,
                            map(int,
                                set(
                                    map("".join,
                                        permutations(str(prime)))))))

        # look for a serie
        for first in perms:
            for second in perms:
                if first >= second:
                    continue

                delta = second - first
                # looks for a potential third
                if second + delta in perms:
                    print(first, second, second + delta)

    return
예제 #5
0
def main():
    ps = primes(10**6)

    for p in dropwhile(lambda x: x <= 56003, ps):
        if test(p):
            return