def get_prime(n): return primesieve.get_primes(n)[-1]
''' The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ? ''' from primesieve import get_primes if __name__ == '__main__': num = 600851475143 # Largest distinct factor is <= to SQRT max = round(pow(600851475143, .5)) primes = get_primes(max) # Since we're looking for the largest, start at the end primes.reverse() for p in primes: if num % p == 0: print p break
""" 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? """ from primesieve import get_primes if __name__ == "__main__": index = 10001 primes = get_primes(1000000) print primes[index - 1]
''' 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20? ''' from primesieve import get_primes import math if __name__ == '__main__': max = 20 numbers = range(2,max+1,1) primes = get_primes(numbers[-1]) result = 1 # Per the fundamental theorem of arithmetic: # http://en.wikipedia.org/wiki/Fundamental_theorem_of_arithmetic # Every number can be factored as a multiple of primes in the form: # p1 ^ i1 * p2 ^ i2 * p3 ^ i3 ... pi ^ in # Therefore, the only numbers we need to multiply are the primes, but # the tricky part is to figure out the specific exponents of each prime # To do that we would need to determine the largest exponent of our prime # that would "fit" in the number for p in primes: # We need to find the smallest integer that satisfies this # p^x <= max <= p^(x+1) # x * log(p) = log(max) # x = log(max) / log(p) # x = floor(log(max)/log(p)
def test_primes(self): self.assertEquals(get_primes(0), []) self.assertEquals(get_primes(1), []) self.assertEquals(get_primes(2), [2]) self.assertEquals(get_primes(3), [2,3]) self.assertEquals(get_primes(20), [2,3,5,7,11,13,17,19])
def __init__(self, max): # Store all the primes up to the max number to speed up calls for p in get_primes(max): self.primes[p] = True
''' The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. ''' from primesieve import get_primes if __name__ == '__main__': print sum(get_primes(2000000))