您的当前位置:首页正文

We Chall之Prime Factory题解

来源:花图问答

题目:

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,567and the second is 8,765,432,your solution is 12345678765432

程序(python):

def isPrime(n):
    for i in range(2,n):
        if i*i <= n:
            if n%i == 0:
                return False
    return True

def sumA(n):
    #return n-((n-1)/9*9)
    sum=0
    while(n>0):
        sum+=n%10
        n/=10
    return sum
def main():
    i=1000001
    a=[]
    k=0
    while True:
        if len(a)==2:
            break
        if isPrime(i):
            if isPrime(sumA(i)):
                a.append(str(i))
        i=i+1
    print(a[0]+a[1])

if __name__ == '__main__':
    main()

结果:

10000331000037