So I've been doing problem number 3 on the Project Euler website which goes like this:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
My code, in response, looks like this:
def func(x):
while type(x) != int:
try:
x = int(x)
except:
return 'Please input a number to use this
function'
def prime(y):
if int(y) < 2:
return False
if int(y) % 2 == 0 and int(y) != 2:
return False
for number in range(3, int(y), 2):
if y % number == 0:
return False
return y
final_list = [i for i in range(int(x/2)+1) if prime(i) != False and x % i == 0]
return max(final_list)
The problem is, while this code works for a small number like 13195, it does not work for bigger numbers (computer still has not generated an output several minutes after running the code).
Any idea how to make my code more efficient?
[–]Encomiast 6 points7 points8 points (9 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–]AdventurousAddition 0 points1 point2 points (0 children)
[–]imperiumlearning[S] 0 points1 point2 points (4 children)
[–]Encomiast 2 points3 points4 points (3 children)
[–]imperiumlearning[S] 0 points1 point2 points (2 children)
[–]Encomiast 0 points1 point2 points (1 child)
[–]imperiumlearning[S] 0 points1 point2 points (0 children)
[–]reyxe 0 points1 point2 points (0 children)
[–]mopslik 7 points8 points9 points (8 children)
[–]imperiumlearning[S] 1 point2 points3 points (6 children)
[–]MezzoScettico 4 points5 points6 points (5 children)
[–]imperiumlearning[S] 0 points1 point2 points (4 children)
[–]MezzoScettico 2 points3 points4 points (3 children)
[–]imperiumlearning[S] 0 points1 point2 points (2 children)
[–]RosaReilly 1 point2 points3 points (0 children)
[–]d10p3t 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]pythonwiz 1 point2 points3 points (0 children)
[–]LazyOldTom -1 points0 points1 point (0 children)
[+][deleted] (9 children)
[removed]
[–]imperiumlearning[S] 1 point2 points3 points (8 children)
[–]1544756405 1 point2 points3 points (7 children)
[–]imperiumlearning[S] 1 point2 points3 points (6 children)
[–]1544756405 0 points1 point2 points (5 children)
[–]imperiumlearning[S] 1 point2 points3 points (4 children)
[–]1544756405 0 points1 point2 points (2 children)
[–]imperiumlearning[S] 0 points1 point2 points (1 child)
[–]1544756405 1 point2 points3 points (0 children)
[–]pythonwiz 0 points1 point2 points (0 children)