# Problem link: http://projecteuler.net/index.php?section=problems&id=35 # use dictionary and prime generator from myMath import genPrime def getDigits(n): L = [] while n != 0: L.append(n % 10) n /= 10 return L n = 1000000 L = genPrime(n); D = {} for prime in L: D[prime] = True res = 0 for prime in L: ok = True digits = getDigits(prime) num = len(digits) for i in range(0, num): value = 0 for j in range(i, i - num, -1): value = value * 10 + digits[j] if not D.has_key(value): ok = False break if ok: res += 1
# Problem link: http://projecteuler.net/index.php?section=problems&id=50 from myMath import genPrime n = 1000000 primeList = genPrime(n) D = {} for prime in primeList: D[prime] = True num = len(primeList) summ = [0] * num for i in range(0, num): summ[i] = primeList[i] if i != 0: summ[i] += summ[i - 1] resCnt = 0 resValue = 0 for i in range(0, num - resCnt): if primeList[i] * resCnt > n: break for j in range(i + resCnt, num): if i == 0: value = summ[j] else: value = summ[j] - summ[i - 1] if value >= n: break cnt = j - i + 1 if D.has_key(value) and cnt > resCnt: resCnt = cnt resValue = value
# Problem link: http://projecteuler.net/index.php?section=problems&id=10 from myMath import genPrime primes = genPrime(2000000) summ = 0 for k in primes: summ += k print summ
if isPrime(num): return num else: return 0 for k in range(n, 0, -1): if not used[k]: digits[kth] = k used[k] = True res = dfs(kth + 1, n, used, digits) if res != 0: return res used[k] = False return 0 def findPandigitalPrime(n): used = [False] * (n + 1) digits = [0] * n return dfs(0, n, used, digits) m = 40000 primes = genPrime(m) n = 9 res = 0 while True: res = findPandigitalPrime(n) if res != 0: break n -= 1 print res
# Problem link: http://projecteuler.net/index.php?section=problems&id=37 from myMath import genPrime n = 1000000 primes = genPrime(n) D = {} for prime in primes: D[prime] = True res = 0 for prime in primes: if prime < 10: continue ok = True mod = 10 while True: if mod > prime: break if not D.has_key(prime % mod) or not D.has_key(prime / mod): ok = False break mod *= 10 if ok: res += prime print res
# Problem link: http://projecteuler.net/index.php?section=problems&id=12 from myMath import genPrime prime = genPrime(10000) def divisorNumber(n): res = 1 for p in prime: if p * p > n: break k = 0 while n % p == 0: k = k + 1 n = n / p res = res * (k + 1) if n != 1: res = res * 2 return res res = 0 n = 1 while True: res = res + n n = n + 1 if divisorNumber(res) >= 500: break print res