WeChall - Prime Factory

Your task is to find the first two primes above 1,000,000 whose digit sum is also prime. 你的任务是找到大于1,000,000的前两个质数,其各位数字之和也是质数。

Challenge

Find the first two primes greater than 1,000,000 where the sum of their digits is also prime. Concatenate them as the answer.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
def is_prime(n):
return n > 1 and all(n % i != 0 for i in range(2, int(n**0.5) + 1))

p = 1000000
found = []
while len(found) < 2:
p += 1
if is_prime(p) and is_prime(sum(int(d) for d in str(p))):
found.append(p)

print(f'{found[0]}{found[1]}')
# 10000331000037

The first two primes are 1000033 and 1000037. Their digit sums are 1+0+0+0+0+3+3 = 7 (prime) and 1+0+0+0+0+3+7 = 11 (prime). Concatenated: 10000331000037.