def num_divisors(num): primes = utils.sieve_of_eratosthenes(int(math.ceil(math.sqrt(num)))) factors = [] prime_counter = 0 while num != 1: if prime_counter > len(primes) - 1: if num % 2 != 0 and num > 3 and utils.is_prime(num, 10): factors.append(num) break else: print("fail") break if num % primes[prime_counter] == 0: num /= primes[prime_counter] factors.append(primes[prime_counter]) else: prime_counter += 1 c = Counter(factors) product = 1 for key, value in c.iteritems(): value += 1 product *= value return product
def prime_factors(num): primes = utils.sieve_of_eratosthenes(int(math.ceil(math.sqrt(num)))) factors = [] prime_counter = 0 while num != 1: if num % primes[prime_counter] == 0: num /= primes[prime_counter] factors.append(primes[prime_counter]) else: prime_counter += 1 return factors[len(factors) - 1]
def prime_factors(num): primes = utils.sieve_of_eratosthenes(int(math.ceil(math.sqrt(num))) + 1) factors = set() prime_counter = 0 while num != 1 and prime_counter < len(primes): if num % primes[prime_counter] == 0: num /= primes[prime_counter] factors.add(primes[prime_counter]) else: prime_counter += 1 if num != 1: factors.add(int(num)) return factors
def equal_gap(one, two, three): return two - one == three - two def check_perms(n): perms = [int(''.join(p)) for p in itertools.permutations(str(n), 4)] perms = [perm for perm in perms if utils.is_prime(perm) and perm >= 1000] for i in range(0, len(perms)): for j in range(i + 1, len(perms)): for h in range(j + 1, len(perms)): if (perms[i] != perms[j] != perms[h] and equal_gap(perms[i], perms[j], perms[h])): return (str(perms[i]) + ', ' + str(perms[j]) + ', ' + str(perms[h])) primes = utils.sieve_of_eratosthenes(10000) lower_index = 0 upper_index = len(primes) for i in range(0, len(primes)): if primes[i] > 1000: lower_index = i - 1 break for i in range(lower_index, upper_index): check = check_perms(primes[i]) if check is not None: print(check)
# # Solution to Project Euler Problem 37 # import utils def truncatable(num): num = str(num) temp_num = str(num) for i in range(0, len(num) - 1): temp_num = temp_num[1:] if not utils.is_prime(int(temp_num)): return False for i in range(0, len(num) - 1): num = num[:-1] if not utils.is_prime(int(num)): return False return True primes_so_far = [] for prime in utils.sieve_of_eratosthenes(1000000)[4:]: if truncatable(prime): primes_so_far.append(prime) print(sum(primes_so_far))
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # # Find the sum of all the primes below two million. import time from utils import sieve_of_eratosthenes if __name__ == '__main__': t0 = time.time() results = sieve_of_eratosthenes(2000000) runtime = time.time() - t0 print 'Runtime = {}'.format(runtime) print 'Result = {}'.format(sum(results))
# # Solution to Project Euler Problem 50 # import utils prime_list = [x for x in utils.sieve_of_eratosthenes(1000000) if x < 100000] max_ = 0 for i in range(0, len(prime_list)): for j in range(i + 1, len(prime_list)): if sum(prime_list[i:j]) > 1000000: break if utils.is_prime(sum(prime_list[i:j])) and j - i > max_: max_ = j - i print(str(sum(prime_list[i:j])) + ', ' + str(max_))
# # where |n| is the modulus/absolute value of n # e.g. |11| = 11 and |-4| = 4 # Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number # of primes for consecutive values of n, starting with n = 0. import time from utils import sieve_of_eratosthenes if __name__ == '__main__': t0 = time.time() primes = set(sieve_of_eratosthenes(100000)) max_n = 0 result = (0, 0) for a in xrange(-999, 1000): if a % 200 == 0: print '{}%'.format((100 * (a + 1000)) / 2000.0) for b in xrange(-999, 1000): n = 0 while True: value = (n**2) + a*n + b if value < 2 or value not in primes: break
# n^2 + an + b, where |a| < 1000 and |b| < 1000 # # where |n| is the modulus/absolute value of n # e.g. |11| = 11 and |-4| = 4 # Find the product of the coefficients, a and b, for the quadratic expression that produces the maximum number # of primes for consecutive values of n, starting with n = 0. import time from utils import sieve_of_eratosthenes if __name__ == '__main__': t0 = time.time() primes = set(sieve_of_eratosthenes(100000)) max_n = 0 result = (0, 0) for a in xrange(-999, 1000): if a % 200 == 0: print '{}%'.format((100 * (a + 1000)) / 2000.0) for b in xrange(-999, 1000): n = 0 while True: value = (n**2) + a * n + b if value < 2 or value not in primes: break
# # Solution to Project Euler Problem 7 # # What is the 10001st prime # import utils # Brute force using the sieve_of_eratosthenes if __name__ == "__main__": primes = utils.sieve_of_eratosthenes(200000) print(primes[10000])
def sum_of_primes(): return sum(utils.sieve_of_eratosthenes(2000000))