def main(): start = default_timer() N = 10000 primes = sieve(N) found = 0 i = 1489 # Starting from i=1489 (bigger than the first number in the sequence given in the problem), # check odd numbers. If they're prime, loop on even numbers j (odd+even=odd, odd+odd=even and # we need odd numbers because we're looking for primes) up to 4254 (1489+2*4256=10001 which has # 5 digits. while i < N: if primes[i] == 1: for j in range(1, 4255): # If i, i+j and i+2*j are all primes and they have # all the same digits, the result has been found. if i + 2 * j < N and primes[i+j] == 1 and primes[i+2*j] == 1 and\ ''.join(sorted(str(i))) == ''.join(sorted(str(i+j))) and\ ''.join(sorted(str(i))) == ''.join(sorted(str(i+2*j))): found = 1 break if (found): break i = i + 2 end = default_timer() print('Project Euler, Problem 49') print('Answer: {}'.format(str(i) + str(i + j) + str(i + 2 * j))) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() N = 10000000 n = -1 min_ = float('inf') semi_p = False primes = sieve(N) for i in range(2, N): # When n is prime, phi(n)=(n-1), so to minimize n/phi(n) we should # use n prime. But n-1 can't be a permutation of n. The second best # bet is to use semiprimes. For a semiprime n=p*q, phi(n)=(p-1)(q-1). # So we check if a number is semiprime, if yes calculate phi, finally # check if phi(n) is a permutation of n and update the minimum if it's # smaller. semi_p, a, b = is_semiprime(i, primes) if semi_p == True: p = phi_semiprime(i, a, b) if ''.join(sorted(str(p))) == ''.join(sorted( str(i))) and i / p < min_: n = i min_ = i / p end = default_timer() print('Project Euler, Problem 70') print('Answer: {}'.format(n)) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() global primes N = 1000000 # N set to 1000000 as a reasonable limit, which turns out to be enough. primes = sieve(N) # Checking only odd numbers with at least 4 digits. i = 1001 while i < N: # The family of numbers needs to have at least one of 0, 1 or 2 as # repeated digits, otherwise we can't get a 8 number family (there # are only 7 other digits). Also, te number of repeated digits must # be 3, otherwise at least 3 resulting numbers will be divisible by 3. # So the smallest number of this family must have three 0s, three 1s or # three 2s. if count_digit(i, 0) >= 3 or count_digit(i, 1) >= 3 or\ count_digit(i, 2) >= 3: if primes[i] == 1 and replace(i) == 8: break i = i + 2 end = default_timer() print('Project Euler, Problem 51') print('Answer: {}'.format(i)) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() N = 1000000 primes = sieve(N) max_ = 0 max_p = 0 # Starting from a prime i, add consecutive primes until the # sum exceeds the limit, every time the sum is also a prime # save the value and the count if the count is larger than the # current maximum. Repeat for all primes below N. # A separate loop is used for i=2, so later only odd numbers are # checked for primality. i = 2 j = i + 1 count = 1 sum_ = i while j < N and sum_ < N: if primes[j] == 1: sum_ = sum_ + j count = count + 1 if sum_ < N and primes[sum_] == 1 and count > max_: max_ = count max_p = sum j = j + 1 for i in range(3, N, 2): if primes[i] == 1: count = 1 sum_ = i j = i + 2 while j < N and sum_ < N: if primes[j] == 1: sum_ = sum_ + j count = count + 1 if sum_ < N and primes[sum_] == 1 and count > max_: max_ = count max_p = sum_ j = j + 2 end = default_timer() print('Project Euler, Problem 50') print('Answer: {}'.format(max_p)) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() N = 1000001 count = 0 primes = sieve(N) # For any denominator d, the number of reduced proper fractions is # the number of fractions n/d where gcd(n, d)=1, which is the definition # of Euler's Totient Function phi. It's sufficient to calculate phi for each # denominator and sum the value. for i in range(2, N): count = count + phi(i, primes) end = default_timer() print('Project Euler, Problem 72') print('Answer: {}'.format(count)) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() N = 2000000 # Use the function in projecteuler.py implementing the # Sieve of Eratosthenes algorithm to generate primes. primes = sieve(N) sum_ = 0 # Sum all the primes for i in range(N): if primes[i] == 1: sum_ = sum_ + i end = default_timer() print('Project Euler, Problem 10') print('Answer: {}'.format(sum_)) print('Elapsed time: {:.9f} seconds'.format(end - start))
def main(): start = default_timer() global primes N = 1000000 # Calculate all primes below one million, then check if they're circular. primes = sieve(N) count = 13 # Calculate all primes below one million, then check if they're circular. for i in range(101, N, 2): if is_circular_prime(i): count = count + 1 end = default_timer() print('Project Euler, Problem 35') print('Answer: {}'.format(count)) print('Elapsed time: {:.9f} seconds'.format(end - start))
#Find the sum of all the primes below two million. import math import time from projecteuler import sieve p = sieve(2000000) sum = 0 for x in p: sum += x print(sum)
def main(): start = default_timer() N = 10000 primes = sieve(N) found = 0 p1 = 3 # Straightforward brute force approach while p1 < N and not found: # If p1 is not prime, go to the next number. if primes[p1] == 0: p1 = p1 + 2 continue p2 = p1 + 2 while p2 < N and not found: # If p2 is not prime, or at least one of the possible concatenations of # p1 and p2 is not prime, go to the next number. if primes[p2] == 0 or not is_prime( int(str(p1) + str(p2))) or not is_prime( int(str(p2) + str(p1))): p2 = p2 + 2 continue p3 = p2 + 2 while p3 < N and not found: # If p3 is not prime, or at least one of the possible concatenations of # p1, p2 and p3 is not prime, got to the next number. if primes[p3] == 0 or not is_prime(int(str(p1)+str(p3))) or not is_prime(int(str(p3)+str(p1))) or\ not is_prime(int(str(p2)+str(p3))) or not is_prime(int(str(p3)+str(p2))): p3 = p3 + 2 continue p4 = p3 + 2 while p4 < N and not found: # If p4 is not prime, or at least one of the possible concatenations of # p1, p2, p3 and p4 is not prime, go to the next number. if primes[p4] == 0 or not is_prime(int(str(p1)+str(p4))) or not is_prime(int(str(p4)+str(p1))) or\ not is_prime(int(str(p2)+str(p4))) or not is_prime(int(str(p4)+str(p2))) or\ not is_prime(int(str(p3)+str(p4))) or not is_prime(int(str(p4)+str(p3))): p4 = p4 + 2 continue p5 = p4 + 2 while p5 < N and not found: # If p5 is not prime, or at least one of the possible concatenations of # p1, p2, p3, p4 and p5 is not prime, go to the next number if primes[p5] == 0 or not is_prime(int(str(p1)+str(p5))) or not is_prime(int(str(p5)+str(p1))) or\ not is_prime(int(str(p2)+str(p5))) or not is_prime(int(str(p5)+str(p2))) or\ not is_prime(int(str(p3)+str(p5))) or not is_prime(int(str(p5)+str(p3))) or\ not is_prime(int(str(p4)+str(p5))) or not is_prime(int(str(p5)+str(p4))): p5 = p5 + 2 continue # If it gets here, the five values have been found. n = p1 + p2 + p3 + p4 + p5 found = 1 p4 = p4 + 2 p3 = p3 + 2 p2 = p2 + 2 p1 = p1 + 2 end = default_timer() print('Project Euler, Problem 60') print('Answer: {}'.format(n)) print('Elapsed time: {:.9f} seconds'.format(end - start))