Python/Project Euler/Problem 03/sol1.py

40 lines
894 B
Python
Raw Normal View History

2017-10-24 15:41:19 +00:00
'''
Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N?
e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
'''
from __future__ import print_function, division
2017-10-24 15:41:19 +00:00
import math
def isprime(no):
if(no==2):
return True
elif (no%2==0):
return False
sq = int(math.sqrt(no))+1
for i in range(3,sq,2):
if(no%i==0):
return False
return True
maxNumber = 0
n=int(raw_input())
2017-10-24 15:41:19 +00:00
if(isprime(n)):
print(n)
2017-10-24 15:41:19 +00:00
else:
while (n%2==0):
n=n/2
if(isprime(n)):
print(n)
2017-10-24 15:41:19 +00:00
else:
n1 = int(math.sqrt(n))+1
for i in range(3,n1,2):
if(n%i==0):
if(isprime(n/i)):
maxNumber = n/i
2017-10-24 15:41:19 +00:00
break
elif(isprime(i)):
maxNumber = i
print(maxNumber)