this is my first question here.
I have been solving eulerproject.net questions and after getting a correct answer for the given input I wanted to try different ones to check whether my code was working properly. Seems like it doesn't.
Largest prime factor should be 1137193159 but I get 79. Of course because of the upper limit I have put here. However, if I increase the upper limit even a little my code gets super slow.
This is the function I have used for finding the largest prime factor
def largest_prime_factor(n) :
upper_limit = math.trunc((n**0.5))
while(1) :
for num in range(upper_limit, 2, -1) :
if n % num == 0 :
if(isPrime(num)) :
return num
else :
pass
if(isPrime(n)) :
return n
Here is the isprime method
## taken from : https://stackoverflow.com/questions/15285534/isprime-function-for-python-language
def isPrime(n):
if n==2 or n==3: return True
if n%2==0 or n<2: return False
for i in range(3, int(n**0.5)+1, 2): # only odd numbers
if n%i==0:
return False
return True
Thanks in advance