45 lines
853 B
Python
45 lines
853 B
Python
|
#!/usr/bin/env python3
|
||
|
|
||
|
from random import randint
|
||
|
|
||
|
DEPTH = 10
|
||
|
|
||
|
def is_prime(n):
|
||
|
if n == 2:
|
||
|
return True
|
||
|
if not n % 2 or n < 2:
|
||
|
return False
|
||
|
for i in range(DEPTH):
|
||
|
a = randint(2, n-1)
|
||
|
if pow(a, n-1, n) != 1:
|
||
|
return False
|
||
|
return True
|
||
|
|
||
|
def harshad_nums(N, n=0, s=0):
|
||
|
r = []
|
||
|
for i in range(n==0, 10):
|
||
|
m = 10*n + i
|
||
|
nws = s + i
|
||
|
if m > N:
|
||
|
break
|
||
|
if m % nws:
|
||
|
continue
|
||
|
r.append((m, nws))
|
||
|
r += harshad_nums(N, m, nws)
|
||
|
return r
|
||
|
|
||
|
def main(N):
|
||
|
for n, s in harshad_nums(N // 10):
|
||
|
if not is_prime(n // s):
|
||
|
continue
|
||
|
for d in range(1, 10):
|
||
|
m = 10*n + d
|
||
|
if m >= N:
|
||
|
break
|
||
|
if is_prime(m):
|
||
|
yield m
|
||
|
|
||
|
N = 10**14
|
||
|
|
||
|
print(sum(main(N)))
|