コード例 #1
0
ファイル: p046.py プロジェクト: wilsonify/euler
def test_goldbach(n):
    if n % 2 == 0 or eulerlib.is_prime(n):
        return True
    for i in itertools.count(1):
        k = n - 2 * i * i
        if k <= 0:
            return False
        elif eulerlib.is_prime(k):
            return True
コード例 #2
0
ファイル: p387.py プロジェクト: wilsonify/euler
 def find_harshad_primes(n, digitsum, isstrong):
     # Shift left by 1 digit, and try all 10 possibilities for the rightmost digit
     m = n * 10
     s = digitsum
     for iteration in range(10):
         if m >= limit:
             break
         if isstrong and eulerlib.is_prime(m):
             ans[0] += m
         if m % s == 0:
             find_harshad_primes(m, s, eulerlib.is_prime(m // s))
         m += 1
         s += 1
コード例 #3
0
def is_truncatable_prime(n):
    # Test if left-truncatable
    i = 10
    while i <= n:
        if not eulerlib.is_prime(n % i):
            return False
        i *= 10

    # Test if right-truncatable
    while n > 0:
        if not eulerlib.is_prime(n):
            return False
        n //= 10
    return True
コード例 #4
0
ファイル: p058.py プロジェクト: wilsonify/euler
def problem058():
    """


    Starting with 1 and spiralling anticlockwise in the following way, a
       square spiral with side length 7 is formed.

                                  37 36 35 34 33 32 31
                                  38 17 16 15 14 13 30
                                  39 18  5  4  3 12 29
                                  40 19  6  1  2 11 28
                                  41 20  7  8  9 10 27
                                  42 21 22 23 24 25 26
                                  43 44 45 46 47 48 49

       It is interesting to note that the odd squares lie along the bottom right
       diagonal, but what is more interesting is that 8 out of the 13 numbers
       lying along both diagonals are prime; that is, a ratio of 8/13 ≈ 62%.

       If one complete new layer is wrapped around the spiral above, a square
       spiral with side length 9 will be formed. If this process is continued,
       what is the side length of the square spiral for which the ratio of primes
       along both diagonals first falls below 10%?
    """
    target = fractions.Fraction(1, 10)
    numprimes = 0
    for n in itertools.count(1, 2):
        for i in range(4):
            if eulerlib.is_prime(n * n - i * (n - 1)):
                numprimes += 1
        if n > 1 and fractions.Fraction(numprimes, n * 2 - 1) < target:
            return n
コード例 #5
0
def problem243():
    TARGET = fractions.Fraction(15499, 94744)
    totient = 1
    denominator = 1
    p = 2
    while True:
        totient *= p - 1
        denominator *= p
        # Note: At this point in the code, denominator is the product of one copy of each
        # prime number up to and including p, totient is equal to totient(denominator),
        # and totient/denominator = R'(2 * 3 * ... * p) (the pseudo-resilience).

        # Advance to the next prime
        while True:
            p += 1
            if eulerlib.is_prime(p):
                break

        # If the lower bound is below the target, there might be a suitable solution d such that
        # d's factorization only contains prime factors strictly below the current (advanced) value of p
        if fractions.Fraction(totient, denominator) < TARGET:
            # Try to find the lowest factor i such that R(i*d) < TARGET, if any.
            # Note that over this range of i, we have R'(d) = R'(i*d) < R(i*d).
            for i in range(1, p):
                numer = i * totient
                denom = i * denominator
                if fractions.Fraction(numer, denom - 1) < TARGET:
                    return denom
コード例 #6
0
ファイル: p077.py プロジェクト: wilsonify/euler
def num_prime_sum_ways(n):
    for i in range(primes[-1] + 1, n + 1):
        if eulerlib.is_prime(i):
            primes.append(i)

    ways = [1] + [0] * n
    for p in primes:
        for i in range(n + 1 - p):
            ways[i + p] += ways[i]
    return ways[n]
コード例 #7
0
ファイル: p027.py プロジェクト: wilsonify/euler
def is_prime(input_n):
    """
    cached version of eulerlib.is_prime
    :param input_n:
    :return:
    """
    if input_n < 0:
        return False
    if input_n < len(IS_PRIME_CACHE):
        return IS_PRIME_CACHE[input_n]
    return eulerlib.is_prime(input_n)
コード例 #8
0
def problem041():
    """


    We shall say that an n-digit number is pandigital if it makes use of all
       the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital
       and is also prime.

       What is the largest n-digit pandigital prime that exists?
    """

    # Note: The only 1-digit pandigital number is 1, which is not prime. Thus we require n >= 2.
    for n in reversed(range(2, 10)):
        arr = list(reversed(range(1, n + 1)))
        while True:
            if arr[-1] not in NONPRIME_LAST_DIGITS:
                n = int("".join(str(x) for x in arr))
                if eulerlib.is_prime(n):
                    return n
            if not prev_permutation(arr):
                break
    raise AssertionError()
コード例 #9
0
 def is_prime(n):
     if n < len(isprime):
         return isprime[n]
     else:
         return eulerlib.is_prime(n)
コード例 #10
0
ファイル: p130.py プロジェクト: wilsonify/euler
def problem130():
    cond = (lambda i: (i % 5 != 0) and (not eulerlib.is_prime(i)) and (
        (i - 1) % find_least_divisible_repunit(i) == 0))
    ans = sum(itertools.islice(filter(cond, itertools.count(7, 2)), 25))
    return ans
コード例 #11
0
def problem132():
    # Among the integers starting from 2, take the sum of
    # the first 40 integers satisfying the filter condition
    cond = lambda i: eulerlib.is_prime(i) and repunit_mod(10**9, i) == 0
    ans = sum(itertools.islice(filter(cond, itertools.count(2)), 40))
    return ans
コード例 #12
0
ファイル: test_utils.py プロジェクト: wilsonify/euler
def test_is_prime(input_x, expected_output):
    output = eulerlib.is_prime(input_x)
    print(output)
    assert output == expected_output