def main(): top = 1000 #Routine for all n less than the top value longest = 0 val = 0 t = time.time() for num in range(2, top + 1): r = findRecurrence(num) if r > longest: longest = r val = num t = time.time() - t print('Including non-primes:') print(val) print('It took {0} seconds'.format(t)) #Routine for all primes less than the top value longest = 0 val = 0 primes = pemaths.Eratosthenes(top) t = time.time() for num in primes: r = findRecurrence(num) if r > longest: longest = r val = num t = time.time() - t print('Using only primes:') print(val) print('It took {0} seconds'.format(t))
def elegant(interval): t = time.time() primes = pemaths.Eratosthenes(interval) powers = [0] * (interval + 1) for i in range(2, interval + 1): pfacts = pemaths.primeFactorize(i, primes) for prime, power in pfacts: if power > powers[prime]: powers[prime] = power value = 1 for v in range(interval + 1): if powers[v]: value = value * (v ** powers[v]) print(value) print('The elegant method took {0} seconds'.format(time.time() - t))
#28: 1,2,4,7,14,28 #We can see that 28 is the first triangle number to have over five divisors. # #What is the value of the first triangle number to have over five hundred #divisors? # #Calculating triangle numbers will be simple, checking the number of divisors #will be less so. If we work with prime factorization, this will be faster. import time import os.path, sys # An OS-independent hack for importing from the parent directory sys.path.append(os.path.abspath(os.path.split(os.getcwd())[0])) import pemaths primes = pemaths.Eratosthenes(10000) #Generate a list of primes t = time.time() tnum = 1 # Triangle number n = 1 c = 0 while c <= 500: # While the count of divisors is <= 500 c = 1 n += 1 tnum += n # New generated triangle number test = tnum for prime in primes: # Check prime divisors if prime > test ** 0.5: c = 2 * c break e = 0
#Problem 7 from Project Euler #Solution by Paul Barton # #Here is the text of the problem: #By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see #that the 6th prime is 13. # #What is the 10001st prime number? # #This is a relatively simple question. It is also easily within the scope of a #prime sieve solution. import time import os.path, sys # An OS-independent hack for importing from the parent directory sys.path.append(os.path.abspath(os.path.split(os.getcwd())[0])) import pemaths t = time.time() myprimes = pemaths.Eratosthenes(200000) print('The 100001st prime is {0}'.format(myprimes[10000])) print('This took {0} seconds'.format(time.time() - t))
for p in primes: if val == p: return True elif val < p: return False raise ValueError('Primes list was too short! Test value exceeded highest \ prime value.') t = time.time() # Start the timer CONST = 1000 #The largest potential prime is determined by the highest n, a, and b #values of b and a are constrained, n is unknown, so we guess an upper limit and #checkPrime() will let us know if it is too small. The function is also written #to remove the penalty of this value being too large. primes = pemaths.Eratosthenes(20000) #b must be prime, a requirement of the case n = 0 bvals = pemaths.Eratosthenes(CONST) a = (CONST - 1) * (-1) max_n = 0 max_a_b = (0, 0) while a < CONST: for b in bvals: n = 0 while checkPrime(qForm(n, a, b), primes): n += 1 if n > max_n: max_n = n max_a_b = a, b #a must be odd: consider the quadratic formula as n(n + a) + b = Prime(odd)