Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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?
- def isPrime(n):
- '''check if integer n is a prime'''
- # range starts with 2 and only needs to go up the square-root of n
- for x in range(2, int(n**0.5)+1):
- if n % x == 0:
- return False
- return True
- def isCirPrime(n):
- '''check if integer n is a circular prime'''
- if n < 10:
- return True
- nstr = str(n)
- for i in range(len(nstr)-1):
- nstr = nstr[1:]+nstr[0]
- if not isPrime(int(nstr)):
- return False
- return True
- cirPrimes = [2]
- for i in range(3,1000000,2): #Checks only odd numbers for prime
- if isPrime(i) and isCirPrime(i):
- cirPrimes.append(i)
- print len(cirPrimes)
Advertisement
Add Comment
Please, Sign In to add comment