题意
Your task is simple:
Find the first two primes above 1 million, whose separate digit sums are also prime.
As example take 23, which is a prime whose digit sum, 5, is also prime.
The solution is the concatination of the two numbers,
Example: If the first number is 1,234,567
and the second is 8,765,432,
your solution is 12345678765432
解读
就如题意所说的,任务很简单,找到两个在1,000,000以上的第一和第二个素数,再将其拼起来然后上交flag。本身很简单,用两下素数判断就好,再不济,手算也能硬跑出来。不过,这个问题还可以延申一下,比如各位数字之和仍是多位数等等。
结果
这里我用python枚举
import math
def isPrime(n): # 判断素数
if n <= 1:
return False
for i in range(2, int(math.sqrt(n)) + 1):
if n % i == 0:
return False
return True
count = 0
for i in range(1000000, 10000000):
if isPrime(i):
digit_sum = 0 # 各位数字和
tmp = i # 判定数的拷贝
while tmp > 0:
digit_sum += tmp % 10
tmp = int(tmp / 10)
print("digit_sum:", digit_sum)
if isPrime(digit_sum):
print(i)
count += 1 # 素数数量
if 2 == count:
break
然后这两个数字拼起来就好了(不需要逗号)