def isTruncPrime(n): s = str(n) pre = ['1', '9'] if s[0] in pre or s[-1] in pre: return False for i in range(1, len(s)): if not isPrime(int(s[:i])) or not isPrime(int(s[i:])): return False return True
def isCircularPrime(n): if '5' in str(n): return False if not isPrime(n): return False nums = Rotation(str(n)) for n in nums[1:]: if not isPrime(int(n)): return False return True
def run(n): t = 2 arr = [] while not common.isPrime(n): if t == 2 and n % t != 0: t += 1 elif n % t != 0: t += 2 if not common.isPrime(t): t += 2 if n % t == 0: n //= t arr.append(t) arr.append(n) return max(arr)
def isTruncatablePrime(n, leftToRight): s = str(n) while len(s) > 0: if not isPrime(int(s)): return False s = s[1:] if leftToRight else s[:-1] return True
def isCircularPrime(n): s = str(n) for i in range(len(s)): if not isPrime(int(s)): return False s = s[1:] + s[0] return True
def prob10(): maxNumber = 2000000 primeList = set() for i in range(2,maxNumber+1): if isPrime(i): primeList.add(i) return sum(primeList)
def prob7(): primeList = set() i = 2 while len(primeList) <= 10000: if isPrime(i): primeList.add(i) i = i+1 return max(primeList)
def prob3(): number = 600851475143 primeList = set() primeFactors = set() for i in range(2,int(ceil(sqrt(number)))+1): if i in primeList or isPrime(i): primeList.add(i) if number % i == 0: primeFactors.add(i) return max(primeFactors)
def quadratic(a, b): """ If n^2 + a*n + b is a quadratic formula then return the number of prime it produces, else return 0 """ n = 1 while True: fn = n * (n + a) + b if not isPrime(fn): break else: n += 1 return n - 1
# The prime factors of 13195 are 5, 7, 13 and 29. # What is the largest prime factor of the number 600851475143? from math import sqrt from common import isPrime val = 600851475143 if isPrime(val): print(val) exit() for n in range(int(sqrt(val))+1, 0, -1): if val % n == 0 and isPrime(n): print(n) break
If n^2 + a*n + b is a quadratic formula then return the number of prime it produces, else return 0 """ n = 1 while True: fn = n * (n + a) + b if not isPrime(fn): break else: n += 1 return n - 1 if __name__ == "__main__": nums, product = -1, 0 primes = [n for n in range(2, 1000) if isPrime(n)] # derivation of f(n)=n^2+a*n+b is f'=2*n+a, so f(n) get its minimum value when # n=-a/2, and f(-a/2)=b-(a^2)/4 must be greater than 2(the smallest prime) # when a is lesser than zero. # For b is up to 999,so we get 4*999-8 >= a^2, then a >= -63 when a is # negative, so a starts at -63 for a in range(-63, 1000): # b must prime because f(n) = b (when n=0) for b in primes: if a == 0 or (a < 0 and 4 * b - a ** 2 - 8 < 0): continue tmp = quadratic(a, b) if nums < tmp: nums = tmp product = a * b
# The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. # Find the sum of all the primes below two million. from common import isPrime print(2 + sum([n for n in range(3, 2000000+1, 2) if isPrime(n)]))
return s.find(substring) def getAllPrimes(u): pdict = dict() allps = [] for i in range(2, u): n, ps, flag = i, [], True while n not in pset: if n in pdict: ps += pdict[n] flag = False break else: for p in primes: if not n % p: ps.append(p) n /= p break if flag: ps.append(n) allps.append(ps) pdict[i] = ps return allps primes = [i for i in range(2, 1000000) if isPrime(i)] pset = Set(primes) distincts = [len(Set(ps)) for ps in getAllPrimes(1000000)] print findFirsNumber(distincts, '4444') + 2
#! /usr/bin/python2.7 # -*- coding: utf-8 -*- from common import isPrime import itertools # sum(range(1,10)) = 45, which can be divided by 3, so 9-bit number is impossible # so does 8-bit numbers, while 7-bit numbers might be fit # starting from the largest 7-bit number, so it doesn't need to check the # maximum nums = itertools.permutations(range(7, 0, -1)) for i in nums: cur = int(''.join([str(item) for item in i])) if isPrime(cur): print cur break
from common import isPrime cnt = 0 i = 1 while cnt < 10001: i += 1 if isPrime(i): cnt += 1 print(i)
#! /usr/bin/python2.7 # -*- coding: utf-8 -*- from common import isPrime def isTruncPrime(n): s = str(n) pre = ['1', '9'] if s[0] in pre or s[-1] in pre: return False for i in range(1, len(s)): if not isPrime(int(s[:i])) or not isPrime(int(s[i:])): return False return True def isPrimeWithOdd(n): even = ['0', '4', '6', '8'] for i in even: if i in str(n): return False return True primes = [i for i in range(11, pow(10, 6)) if isPrime(i) and isPrimeWithOdd(i)] if __name__ == "__main__": res = [p for p in primes if isTruncPrime(p)] print sum(res)
def isPrime(n): if n < 0: return False elif n < len(p): return p[n] return common.isPrime(n)
#! /usr/bin/python2.7 # -*- coding: utf-8 -*- from common import isPrime from sets import Set def isConjecture(n): root = 1 while 2 * pow(root, 2) <= n - 2: if n - 2 * pow(root, 2) in primes: return True root += 1 return False primes = Set([i for i in range(2, 10000) if isPrime(i)]) odds = Set(range(3, 10000, 2)) oddcomps = odds.difference(primes) for n in oddcomps: if not isConjecture(n): print n 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 sys import maxsize from common import isPrime primesToFind = 10001 primesFound = 0 lastPrime = 0 for n in range(2, maxsize): if isPrime(n): lastPrime = n primesFound += 1 if primesFound == primesToFind: break print(lastPrime)
#! /usr/bin/python2.7 # -*- coding: utf-8 -*- from common import isPrime # let a, b, c be the increasing sequence, then a + c = 2 * b # a, b, c all end with the same number like 1, 3, 7, 9 primes = [i for i in range(1000, 9999) if isPrime(i)] pdict = dict() pdict['1'], pdict['3'], pdict['7'], pdict['9'] = [], [], [], [] for p in primes: pdict[str(p)[-1]].append(p) def findPP(arr): l = len(arr) for i in range(l): for j in range(i + 1, l): mid = (arr[i] + arr[j]) / 2 if mid in arr and sorted(str(arr[i])) == sorted(str(mid)) == sorted(str(arr[j])): print str(arr[i]) + str(mid) + str(arr[j]) for k in pdict.keys(): findPP(pdict[k])