#https://projecteuler.net/problem=10 import Eulerlib #Start timer to time algorithm start = Eulerlib.getTime() #We can just call our prime list function to list all primes less #than 2000000 and then sum the list primes = Eulerlib.primeList(1999999) Eulerlib.answer = sum(primes) #Stop timer to time algorithm end = Eulerlib.getTime() #The answer should be 142913828922 print("The answer was found in %f seconds" % (end - start)) print("And the answer is: %d" % Eulerlib.answer)
#https://projecteuler.net/problem=9 import Eulerlib #Start timer to time algorithm start = Eulerlib.getTime() #Since a < b < c the biggest A can be for this sum of these number to be 1000 is 332 #and the biggest b can be is 498(a could be 1, b could be 499, and c could be 500). #We can also save CPU time by starting b's range from one number bigger than a. for a in range(1, 332): for b in range(a + 1, 499): c = (a**2 + b**2)**.5 if a + b + c == 1000: Eulerlib.answer = a * b * c #Stop timer to time algorithm end = Eulerlib.getTime() #The answer should be 31875000 print("The answer was found in %f seconds" % (end - start)) print("And the answer is: %d" % Eulerlib.answer)
#https://projecteuler.net/problem=3 import Eulerlib #This is the number we are trying to find the greatest common prime factor of z = 600851475143 #Start timer to time algorithm start = Eulerlib.getTime() #We can save CPU time by just checking if the numbers less than the square root of z #are factors of z. We can then check if each factor is prime to find the greatest #common prime factor for i in range(3, int(z**.5)): if z % i == 0: if Eulerlib.primeCheck(i): Eulerlib.answer = i #Stop timer to time algorithm end = Eulerlib.getTime() #The answer should be 6857 print("The answer was found in %f seconds" % (end - start)) print("And the answer is: %d" % Eulerlib.answer)
#https://projecteuler.net/problem=7 import Eulerlib #Start timer to time algorithm start = Eulerlib.getTime() #We can just generate the first 10001 and primes and then read the last prime in the list primes = Eulerlib.primeGen(10001) Eulerlib.answer = primes[len(primes)-1] #Stop timer to time algorithm end = Eulerlib.getTime() #The answer should be 104743 print ("The answer was found in %f seconds" % (end - start)) print ("And the answer is: %d" % Eulerlib.answer)