#!/usr/bin/env python from math_tools import is_prime """https://projecteuler.net/problem=3""" if __name__ == '__main__': cmp = 600851475143 print( next(div for div in xrange(int(cmp**0.5), 1, -1) if cmp % div == 0 and is_prime(div)))
#!/usr/bin/env python from math_tools import rotations, primes, is_prime """https://projecteuler.net/problem=35""" if __name__ == '__main__': result = 0 for prime in primes(1000000): result += all(is_prime(int("".join(rot))) for rot in rotations(list(str(prime)))) print(result)
#!/usr/bin/env python from math_tools import is_prime """https://projecteuler.net/problem=58""" if __name__ == '__main__': side = 1 corner = 1 primes = 0 while True: side += 2 for i in range(4): corner += side - 1 if is_prime(corner): primes += 1 if (float(primes) / ((side - 1) * 2 + 1)) < 0.1: print(side) break
# -*- coding: utf-8 -*- """ Created on 2014-09-23 Last edited on 2014-09-23 @author: Ew3n """ import math_tools range_min = 10000 range_max = 10050 my_list = [] for i in range(range_min, range_max+1): if math_tools.is_prime(i): my_list.append(i) print(*my_list, sep=", ")
#!/usr/bin/env python from math_tools import is_prime """https://projecteuler.net/problem=58""" if __name__ == "__main__": side = 1 corner = 1 primes = 0 while True: side += 2 for i in range(4): corner += side - 1 if is_prime(corner): primes += 1 if (float(primes) / ((side - 1) * 2 + 1)) < 0.1: print(side) break
#!/usr/bin/env python from math_tools import rotations, primes, is_prime """https://projecteuler.net/problem=35""" if __name__ == '__main__': result = 0 for prime in primes(1000000): result += all( is_prime(int("".join(rot))) for rot in rotations(list(str(prime)))) print(result)
#!/usr/bin/env python from math_tools import primes, is_prime """https://projecteuler.net/problem=49""" if __name__ == '__main__': for prime in primes(): prime2 = prime + 3330 prime3 = prime2 + 3330 if ( prime != 1487 and len(str(prime)) == 4 and sorted(str(prime)) == sorted(str(prime2)) == sorted(str(prime3)) and is_prime(prime2) and is_prime(prime3) ): print str(prime) + str(prime + 3330) + str(prime + 6660) exit()
#!/usr/bin/env python from math_tools import is_prime, primes """https://projecteuler.net/problem=27""" if __name__ == '__main__': generator_product = None max_primes_generated = None for b in primes(1000): for a in range(-999, 1000, 2): n = 0 primes_generated = 0 while True: if not is_prime((n ** 2) + (a * n) + b): break primes_generated += 1 n += 1 if primes_generated > max_primes_generated: max_primes_generated = primes_generated generator_product = a * b print(generator_product)
We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly once. For example, 2143 is a 4-digit pandigital and is also prime. What is the largest n-digit pandigital prime that exists? """ # Pandigital numbers with 1,2,5,8,9 digits cannot be prime because for the case of 2,5,8,9 - the digits will sum up to a multiple of 3 making the result divisible by 3. # Also such numbers can not end in 2,4,5,6,8 or it will be composite. # This leaves only us with pandigital numbers with 3,4,6,7 digits to check. from itertools import permutations from math_tools import is_prime pandigital = set() pandigital.update(permutations([x for x in range(1,1+3)])) pandigital.update(permutations([x for x in range(1,1+4)])) pandigital.update(permutations([x for x in range(1,1+6)])) pandigital.update(permutations([x for x in range(1,1+7)])) def is_composite(t): if t[-1] == 2: return True if t[-1] == 4: return True if t[-1] == 5: return True if t[-1] == 6: return True if t[-1] == 8: return True return False not_composite = filter(lambda x : not is_composite(x), pandigital) nums = map(lambda t : int(''.join(map(str,t))), not_composite) primes = filter(lambda x : is_prime(x), nums) print max(primes)
#!/usr/bin/env python from math_tools import primes, is_prime """https://projecteuler.net/problem=49""" if __name__ == "__main__": for prime in primes(): prime2 = prime + 3330 prime3 = prime2 + 3330 if ( prime != 1487 and len(str(prime)) == 4 and sorted(str(prime)) == sorted(str(prime2)) == sorted(str(prime3)) and is_prime(prime2) and is_prime(prime3) ): print str(prime) + str(prime + 3330) + str(prime + 6660) exit()
#!/usr/bin/env python from math_tools import is_prime """https://projecteuler.net/problem=3""" if __name__ == '__main__': cmp = 600851475143 print(next(div for div in xrange(int(cmp ** 0.5), 1, -1) if cmp % div == 0 and is_prime(div)))
""" The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of all the primes below two million. """ from math_tools import is_prime primes = [2] for x in range(3, int(2e6), 2): if is_prime(x) == True: primes.append(x) ans = sum(primes) print ans
# -*- coding: utf-8 -*- """ Created on 2014-09-23 Last edited on 2014-09-23 @author: Ew3n """ import math_tools n = 100000000 while not(math_tools.is_prime(n)): n += 1 print(n)