target=10**6+1 d=[[] if i%2 else [2] for i in range(target)] s,n=1,3 while n < target: if d[n]: m=n for k in d[n]: m=m*(k-1)/k s+=m else: for k in range(n,target,n): d[k].append(n) s=s+n-1 n+=1 print s