import sys
from fractions import gcd, Fraction

sys.path.append('./_resources')
import resources

limit = 10**6

primes = [prime for prime in resources.primes(limit)]

def dividing_primes(n):
  limit = n
  for prime in primes:
    if prime > limit:
      return
    if not n%prime:
      limit = n/prime
      yield prime

def totient(n):
  phi = n
  for dividing_prime in dividing_primes(n):
    phi *= (1 - 1.0/dividing_prime)
  return phi

print sum(totient(n) for n in range(2,limit+1))
import sys
sys.path.append('../_resources/')
from resources import primes
from itertools import product

# Extract out primes between 10**5 and 10**6
P = [str(p) for p in primes(10**6) if p>10**5]

# Create a list of permutation patterns
permutations = [''.join([str(x) for x in p]) for p in product(range(2), repeat=6)][1:-1]

# Create an ignore set to reduce redundent calculations 
ignore = set()

for p in P:
  for permutation in permutations:
    count = 0

    # Check if the permutation pattern as been performed before
    pattern = ''.join(['x' if i=='1' else c for c,i in zip(p,permutation)])
    if pattern in ignore: continue
    ignore.add(pattern)
    for n in range(10):
      if ''.join([c if pm=='0' else str(n) for c,pm in zip(p,permutation)]) in P: count+=1
    if count == 8:
      print p
      sys.exit(0)
import numpy as np
from itertools import product

import sys
sys.path.append('./_resources')
import resources

primes = resources.primes(10**6)

n = 1
prime_collection = {}

while(True):
  n += 1

  if np.any(primes == n):
    prime_collection[n] = prime_collection.get(n, set()) | set(((n,),)) 

  for x,y in product(prime_collection.keys(), primes[np.where(primes <= n)]):
    if x+y > n:
      continue
    prime_collection[x+y] = prime_collection.get(x+y, set()) | \
        set((tuple(sorted(i + (y,))) for i in prime_collection[x]))

  if len(prime_collection[n]) >= 5000:
    print n
    break
from operator import mul
import sys
import itertools
import numpy
sys.path.append('./_resources/')
import resources

target = 10**4
primes = resources.primes(100)
solution_space = {}

def gcd(a, b):
    if a > b: a, b = b, a
    while a > 0: a, b = (b % a), a
    return b

lcm = lambda a, b: a * b / gcd(a, b)

def find_pairwise_lcm_counts(number_set, max_target):
  number_set = set(number_set)
  lcms = set()
  for c in itertools.combinations(number_set, 2):
    l = lcm(*c)
    if l < max_target and l > c[0] and l > c[1]:
      lcms.add(l)

  if len(lcms):
    lcms |= find_pairwise_lcm_counts(lcms, max_target)

  return lcms
import sys
sys.path.append('../_resources/')
from resources import primes

P = primes(10**6)
print(len(P))
sol = (0,0)
for p in P:
  t = 0
  i = 0
  for n in P[P.index(p):]:
    if t > P[-1]: break
    t+=n
    i+=1
    if t<sol[1]: continue
    if t not in P: continue
    if i>sol[0]:
      sol=(i,t)
      print sol
print sol

'''
for i in [1]:
  for start in [s for s in P if s<p]:
    i = 0
    t = 0
    while t<p:
      t+=P[P.index(start)+i]
      i+=1
    if t==p and i>sol[0]:
      sol=(i,p)
import sys

sys.path.append("./_resources/")
import resources

target = 50000000

primes = resources.primes(target)
solution = set()


def power_generator(p, max_n):
    i = 0
    while primes[i] ** p < max_n:
        yield primes[i] ** p
        i += 1


for a in power_generator(2, target):
    for b in power_generator(3, target):
        for c in power_generator(4, target):
            if a + b + c >= target:
                break
            solution.add(a + b + c)
        if a + b >= target:
            break
    if a >= target:
        break

print len(solution)
import sys
sys.path.append('../_resources/')
from resources import primes
from itertools import permutations, combinations
P = [p for p in primes(10**4) if p > 10**3]

for p in P:
  S = sorted(list(set([int(''.join(x)) for x in permutations(str(p)) if int(''.join(x)) in P and x[0]!='0'])))
  if 1487 in S: continue
  if len(S) >= 3:
    D = [abs(p-x) for x in S]
    if len(set(D))==len(D): continue 
    R = [p]+sorted(list(set([s for d,s in zip(D,S) if D.count(d)>=2])))
    print ''.join([str(r) for r in sorted(R)])
    break
import sys

sys.path.append('./_resources')

from resources import primes

P = primes(10**9)

def primes_in_layer():
  N = 3
  c = 0
  i = 1
  while i < len(P):
    if P[i] > N**2:
      yield c
      c=0
      N+=2
    if P[i] < N**2 and P[i] in range(N**2, (N-2)**2, -(N-1)): c+=1
    i+=1
    
r = 1.0
n = 1
S = 0.0
p = primes_in_layer()

while r > 0.10:
  n+=2
  S+=p.next()
  r=float(S)/(2*n-1)
print n
import sys, math

sys.path.append('./_resources')
import resources

primes = [str(p) for p in resources.primes(10**8)]
prime_set = set(primes)

sol = {}

target_size = 4

def iterate_sol(num, depth=0, data=sol, solution=[]):

  if depth==target_size:
    print solution+[num]
    sys.exit(0)

  for k,v in data.iteritems():
    if num+k in prime_set and k+num in prime_set:
      iterate_sol(num, depth+1, v, solution+[k])

  data[num] = {}
  
  pass

for prime in primes:
  print prime
  iterate_sol(prime)