-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproblem-035.py
More file actions
48 lines (37 loc) · 1.34 KB
/
problem-035.py
File metadata and controls
48 lines (37 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
### Problem 35 - Circular Primes
###--------------------------------------------------------------------------------------------------------------------------
### 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?
### Solution
# Function to determine if prime. n:int -> boolean
def isPrime(n):
if n < 2:
return False
elif n == 2:
return True
else:
i = 2
while i ** 2 <= n:
if n % i == 0:
return False
i += 1
return True
# Function to see if all circulations are prime. n:int -> boolean
def isCircular(n):
if not isPrime(n):
return False
num_string = list(str(n))
for i in range(len(num_string) - 1):
temp_index = num_string[0]
for j in range(len(num_string) - 1):
num_string[j] = num_string[j + 1]
num_string[-1] = temp_index
if not isPrime(int("".join(num_string))):
return False
return True
total_primes = 0
for i in range(1, 1000000):
if isCircular(i):
total_primes += 1
print("The total circular primes below 1,000,000 is: " + str(total_primes))