def consecutive_prime_sum(): sieve = eulerlib.prime_sieve(1000000) # takes ~0.5 sec to load primes = [i for i, v in enumerate(sieve) if v] max_s = 0 max_len = 0 max_i = 0 # 551 = number of primes needed when the sum >1 M # 551 = len([i for i, v in enumerate(eulerlib.prime_sieve(1000000//50//5)) if v]) number_of_primes_till_sum_1m = 551 for i in range(0, number_of_primes_till_sum_1m): for k in range(i, number_of_primes_till_sum_1m, 2): if k * primes[i] > 1000000: break s = sum(primes[i:k + 1]) if s > 1000000: break if sieve[s] and k - i > max_len: max_s = s max_len = k - i max_i = i print('Number of terms: {}'.format(max_len)) print('Sum starts at n: {}'.format(max_i)) print('Sum starts at P: {}'.format(primes[max_i])) return max_s
def find_contradiction(): N = 10000 primes = eulerlib.prime_sieve(N) # For all odd composite numbers for i in range(3, N, 2): if primes[i]: continue # Factor into a prime plus a square p, a = factor_goldbach(i, primes) # Not factorized? if not p: # Return solution return i print('{} = {} + 2^{}'.format(i, p, a))
def circular_primes(): sieve = eulerlib.prime_sieve(1000000) primes = [i for i, v in enumerate(sieve) if v] result = 0 for p in primes: is_circular_prime = True prime_c = eulerlib.digits(p) for _ in range(len(prime_c) - 1): prime_c = eulerlib.shift(prime_c) if not sieve[eulerlib.digits_to_int(prime_c)]: is_circular_prime = False break if is_circular_prime: print(p) result += 1 return result
def prime_permutations(): N = 10000 S = eulerlib.prime_sieve(N) P = [p for p in eulerlib.sieve_to_list(S) if 1000 < p < 9999] print('p1 p2 p3 d') print('------------------------') for p1 in P: d = 1 while p1 + 2 * d < N: p2 = p1 + d p3 = p2 + d if S[p1] and S[p2] and S[p3] and is_permutation3( eulerlib.digits(p1), eulerlib.digits(p2), eulerlib.digits(p3)): print(p1, p2, p3, d) break d += 1
def reciprocal_cycles_primes(): denominator_with_largest_cycle = 0 largest_cycle = 0 longest_decimal = "" primes = [i for i, v in enumerate(eulerlib.prime_sieve(1000)) if v] for d in primes: decimal, is_cyclic, cycle_length = fraction_to_decimal(1, d) if is_cyclic and cycle_length > largest_cycle: denominator_with_largest_cycle = d largest_cycle = cycle_length longest_decimal = decimal print('Denominator with largest cycle: {}'.format( denominator_with_largest_cycle)) print('Cycle length: {}'.format(largest_cycle)) print('Decimal as string: {}'.format(longest_decimal)) print('Decimal as float: {}'.format(1 / denominator_with_largest_cycle)) return denominator_with_largest_cycle
def prime_pair_sets(): sieve = euler.prime_sieve(10000000) primes = euler.sieve_to_list(sieve) for prime in primes: n = len(str(prime)) if 4 <= n <= 5: groups_that_are_prime = 0 sub_primes = [] for i in range(1, n): left = int(str(prime)[:i]) right = int(str(prime)[i:]) if sieve[left]: groups_that_are_prime += 1 sub_primes.append(left) if sieve[right]: groups_that_are_prime += 1 sub_primes.append(right) sub_primes = list(set(sub_primes)) m = len(sub_primes) if m >= n: all_orders_are_prime = True print(sub_primes) for i in range(m): for j in range(m): if i == j: continue test_prime = int( str(sub_primes[i]) + str(sub_primes[j])) if test_prime >= 1000000 and not euler.is_prime( test_prime, 32 ) or test_prime < 1000000 and not sieve[test_prime]: all_orders_are_prime = False print(' {} is not prime.'.format(test_prime)) break if not all_orders_are_prime: break if all_orders_are_prime: print(sub_primes) return sum(sub_primes) """
import eulerlib LIMIT = 10**8 primes = eulerlib.prime_sieve(LIMIT + 1) print("Primes generated") total = 0 for num in range(LIMIT + 1): # PGI (prime generating integer) will always be 1 less than a prime number if primes[num + 1] == False: continue # if num is divisible by 4, it is not a PGI if num % 4 == 0: continue isPGI = True for d in range(2, int(num**0.5) + 1): # if d is a divisor if num % d == 0: # p is the prime number we are looking for p = d + int(num / d) if primes[p] == False: isPGI = False break if isPGI == True:
def test_prime_sieve_997(self): assert eulerlib.prime_sieve(1000)[997]
def test_prime_sieve_12345(self): assert eulerlib.prime_sieve(5) == [ False, False, True, True, False, True ]
import eulerlib as lib sieve = lib.prime_sieve(1000000) def perm(k, n, res = [], memo = {}): if n == 1: return for i in range(len(k)): if k[i] == '*': continue temp = k[i] k[i] = '*' pstr = ''.join(map(str, k)) if pstr not in memo.keys(): res.append(pstr) memo[pstr] = True perm(k, n-1, res, memo) k[i] = temp if len(k) == n: return res def pdr(max_n): result = {} memo = {} for i in range(13, max_n): if not sieve[i] and i % 11 != 0: continue print('Testing: {}'.format(i)) istr = str(i)