Advertisement
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?
- '''
- from math import ceil, pow
- # First, I create a list with all the primes
- LIMIT = list(int(pow(10, i)) for i in range(1, 5))
- def f1(LIMIT):
- primes = [2]
- for i in range(3, LIMIT):
- isIPrime = True
- for prime in primes:
- if i % prime == 0:
- isIPrime = False
- break
- if isIPrime == True:
- primes.append(i)
- return primes
- def f2(LIMIT):
- primes = [2]
- for i in range(3, LIMIT):
- isIPrime = True
- for j in range(2, ceil(i/2+1)):
- if i % j == 0:
- isIPrime = False
- if isIPrime == True:
- primes.append(i)
- return primes
- print(LIMIT)
- from timeit import default_timer as timer
- times1 = []
- times2 = []
- for limit in LIMIT:
- start = timer()
- f1(limit)
- end = timer()
- times1.append((end - start) * 1000)
- for limit in LIMIT:
- start = timer()
- f2(limit)
- end = timer()
- times2.append((end - start) * 1000)
- print("Limit 1st Function 2nd Function")
- for i in range(len(LIMIT)):
- print(str(LIMIT[i]) + " " + str(times1[i]) + " " + str(times2[i]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement