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

1000000 이상의 수중에서 소수이며 각자리 숫자의 합 역시 소수인 두개의 숫자를 찾아서 이어서 인증을 하라는 문제이다.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math

# 1이면 소수, 0이면 소수 아님
def isprime(i):
    if i == 1 or i == 2:
        return 1

    for x in range(2, i):
        if i % x == 0:
            return 0
            break

    return 1

i = 1000000
count = 0
while 1:
    sum = 0
    if count == 2:
        break

    elif isprime(i) == 1:
        for x in str(i):
            sum += int(x)
        if isprime(sum) == 1:
            print i
            count += 1
    i += 1

답은 10000331000037

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다