#!/usr/bin/env python3 def divisors(divs): r = [] for p in divs: r += [p*d for d in r] + [p] return r def add_divisors(n, st=2): for i in range(st*n, len(sieve), st*n): sieve[i].append(n) pw = 2 while n**pw < len(sieve): m = n**pw for i in range(st*m, len(sieve), st*m): sieve[i][-1] *= n pw += 1 target = 25 count = 0 s = 0 lim = 10**5 sieve = [x for i in range(lim//2) for x in ([], True)] add_divisors(2, st=1) for p in range(3, len(sieve)): if not p%2 and not sieve[p+1]: for d in divisors(sieve[p]): if pow(10, d, 9*p+9) == 1: break else: continue count += 1 s += p + 1 #print(count, p+1) if count >= target: break if not p%2 or not sieve[p]: continue for i in range(p**2, len(sieve), 2*p): sieve[i] = False add_divisors(p) if count < target: print("Limit reached. Count = {}/{}".format(count,target)) print(s)