Skip to content

Instantly share code, notes, and snippets.

@anivarth
Last active May 28, 2016 07:18
Project Euler Problem 41 Solution With Python
# http://radiusofcircle.blogspot.com
# time module for calculating time
import time
#permutation method
from itertools import permutations
#time at the start of program execution
start = time.time()
def is_prime(n):
"""function to check if the given
number is prime"""
for i in xrange(2, int(n**0.5)+1):
if n % i == 0:
return False
return True
# starting with 1-9 digits
a = '123456789'
# flag to stop while loop when prime is found
flag = True
# while loop iterator
j = 9
# while loop
while flag:
p = permutations(a[:j])
p = list(p)[::-1]
for i in p:
if int(i[j-1]) % 2 != 0:
number = int(''.join(i))
if (number+1) % 6 == 0 or (number-1) % 6 == 0:
if is_prime(number):
print number
flag = False
break
j -= 1
# time at the end of program execution
end = time.time()
# total execution time
print end - start
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment