def phi(x, lis): out = 1 for p in lis: if x % p == 0: k = 0 while x % p == 0: k += 1 x /= p out *= p ** (k - 1) * (p - 1) if x <= p ** 2: if x == 1: return out return out * (x - 1) def makep(x): p = [2] P = [2] n = 3 while n < x: for i in p: if n % i == 0: break else: P.append(n) n += 2 while n > p[-1] ** 2: p.append(P[len(p)]) return P prime = makep(1010) total = 0 for i in xrange(2, 1000001): total += phi(i, prime) print total