Ejemplo n.º 1
0
def main(top=1000000):
    prime_found = None

    # Generate a list with cumalitive sum
    cum_sum_primes = [0]
    cum_sum = 0
    for p in primes_erat():
        cum_sum += p
        cum_sum_primes.append(cum_sum)
        if cum_sum > top:
            break

    num_primes = len(cum_sum_primes)
    num_terms = num_primes
    while True:
        for offset in range(0, 10):
            if offset + num_terms >= num_primes:
                break
            sum_of_primes = cum_sum_primes[offset +
                                           num_terms] - cum_sum_primes[offset]
            if sum_of_primes < top and is_prime(sum_of_primes):
                prime_found = sum_of_primes
                break
        if prime_found:
            break
        num_terms -= 1
    return prime_found
Ejemplo n.º 2
0
def main(smaller_than=None):
    if not smaller_than:
        smaller_than = Fraction(15499, 94744)
    # We know that phi(n)/n is the smallest when n is a primorial,
    # so first look for Phi(priomorial)/primorial which is smaller than our
    # target faction.
    primorial = 1
    prime_list = []
    for prime in primes_erat():
        primorial *= prime
        prime_list.append(prime)
        phi_primorial = reduce(operator.mul, ((p - 1) for p in prime_list))
        ratio = Fraction(phi_primorial, primorial)
        print(primorial, ratio)
        if ratio < smaller_than:
            break
    #return primorial
    # Then search for Phi(n)/n-1 which is smaller than our target fraction.
    found = False
    for n in range(primorial, primorial + 100000):
        ratio = Fraction(phi(n), n - 1)
        if n % 1000 == 0:
            print(n, ratio)
        if ratio < smaller_than:
            found = True
            break
    if found:
        return n
    else:
        return 0
Ejemplo n.º 3
0
def main(max_prime=1000000):
    total = 0
    count = 0
    prime_list = set()
    for n in primes_erat():
        prime_list.add(n)
        if n < 10:
            continue
        n = str(n)
        is_truncatable = True
        # Remove digits from the right
        for l in range(len(n) - 1, 0, -1):
            x = n[:l]
            if not int(x) in prime_list:
                is_truncatable = False
                break
        if not is_truncatable:
            continue
        # Remove digits from the left
        for l in range(1, len(n)):
            x = n[l:]
            if not int(x) in prime_list:
                is_truncatable = False
                break
        if not is_truncatable:
            continue
        # If all above test have passed, it's one of the numbers we're after
        total += int(n)
        count += 1
        if count == 11:
            break
    return total
Ejemplo n.º 4
0
def main(limit=10**7):
    i = 1
    n = 1
    min_ratio = None
    min_n = None
    for p1 in primes_erat():
        for p2 in itertools.takewhile(lambda p: p < p1, primes_erat()):
            n = p1 * p2
            if n > limit:
                break
            phi_n = (p1 - 1) * (p2 - 1)
            ratio = n / phi_n
            if (min_ratio is None or ratio < min_ratio) and is_permutation(
                    n, phi_n):
                min_ratio = ratio
                min_n = n
        if p1 * 2 > limit:
            break
    return min_n
Ejemplo n.º 5
0
def main():
    pandigital = list(range(1, 10))
    largest = None
    for prime in primes_erat():
        if prime < 10**6:
            # numbers below 7 digits are not interesting
            continue
        elif prime > 10**7:
            # pandigital numbers with 8 or 9 digits,
            # are all divisible by 9 (non prime)
            break
        digits = str(prime)
        if sorted(int(d) for d in digits) == pandigital[:len(digits)]:
            largest = prime
    return largest
Ejemplo n.º 6
0
def main():
    results = {}
    for prime in itertools.takewhile(lambda p: p < 9999, primes_erat()):
        if prime >= 1000:
            number_hash = ''.join(sorted(x for x in str(prime)))
            if not results.get(number_hash, False):
                results[number_hash] = []
            results[number_hash].append(prime)

    for number_hash, prime_list in results.items():
        if len(prime_list) < 3:
            continue
        for a, b, c in itertools.combinations(prime_list, 3):
            if (a, b, c) == (1487, 4817, 8147):
                continue
            if b - a == c - b:
                return int(''.join(str(i) for i in (a, b, c)))
Ejemplo n.º 7
0
def main(max_prime=1000000):
    prime_list = set(
        p for p in itertools.takewhile(lambda x: x < max_prime, primes_erat()))
    count = 0
    for n in prime_list:
        is_circular = True
        for x in rotations(str(n)):
            x = int(x)
            if x == n:
                continue
            if x < max_prime and not x in prime_list:
                is_circular = False
                break
            elif x >= max_prime and not is_prime(x):
                is_circular = False
                break
        if is_circular:
            count += 1
    return count
Ejemplo n.º 8
0
def main():
    return sum(itertools.takewhile(lambda x: x < 2000000, primes_erat()))
Ejemplo n.º 9
0
def main():
    def tup_max(a, b):
        if a[1] > b[1]:
            return a
        else:
            return b

    return reduce(tup_max, ((p, period_len_fraction(1, p)) for p in itertools.takewhile(lambda p: p < 1000, primes_erat())))[0]
Ejemplo n.º 10
0
def main():
    return next(
        itertools.islice(
            itertools.takewhile(lambda p: p < 200000, primes_erat()), 10000,
            None))