码迷,mamicode.com
首页 > 其他好文 > 详细

projecteuler---->problem=35----Circular primes

时间:2014-08-22 17:52:39      阅读:185      评论:0      收藏:0      [点我收藏+]

标签:projecteuler   python   

Problem 35

The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.

There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

How many circular primes are there below one million?



import math
def isPrime(n):
    for i in range(2,int(math.sqrt(n))+1):
        if n%i==0:
            return False
    return True
def isCyclePrime(n):
    num = list()
    while n > 0:
        num.insert(0,n%10)
        n /= 10
    for i in range(len(num)):
        s = ''
        for k in range(i,len(num)):
            s += str(num[k])
        for j in range(i):
            s+=str(num[j])
        if isPrime(int(s)) == False:
            return False
    return True
def main():
    count =0
    for i in range(2,1000000):
        if isCyclePrime(i):
            count += 1
    print count
if __name__ == "__main__":
    main()


projecteuler---->problem=35----Circular primes

标签:projecteuler   python   

原文地址:http://blog.csdn.net/q745401990/article/details/38759389

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!