Python,C,C++ and JAVA programs for CBSE, ISC, B.Tech and I.T Computer Science and MCA students

The Programming Project: Largest prime factor: Python Code : Problem 3

Thursday, August 28, 2014

Largest prime factor: Python Code : Problem 3

Largest prime factor : Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
http://projecteuler.net/problem=3 

Answer:  6857

Python Code

import math
def largestPrimeFactor():
    numb = input("Enter the number:")
    pf = 1
    def checkPrime(numb):
        prime = True
        for i in range(int(math.sqrt(numb))):
            i = i+2
            if numb%i == 0:
                prime = False
                break
        if numb == 2:
            return True
        else:           
            return prime
    for i in range(int(math.sqrt(numb))):
        i = i+2
        if checkPrime(i) == True and numb % i == 0:
            print i
            pf = i
    print "Largest Prime factor of",numb,"is:",pf
largestPrimeFactor()                        


No comments:

Post a Comment