Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #The prime factors of 13195 are 5, 7, 13 and 29.
- #What is the largest prime factor of the number 600851475143 ?
- #Algorithm from http://jonlandrum.com/2012/09/10/calculating-the-largest-prime-factor-of-a-number/
- #1. Set the factor variable to 2 (the first prime)
- #2. Determine if the user input is divisible by factor
- #A. If so, repeatedly divide the user input by factor until it is not evenly divisible anymore. If the user input is #not divisible by factor, skip this step.
- #B. Iterate factor to the next prime, and start step 2 again.
- #3. Continue like so until the user input is equal to the final value of factor, which is then your answer.
- def isPrime(n):
- '''check if integer n is a prime'''
- # range starts with 2 and only needs to go up the square-root of n
- for x in range(2, int(n**0.5)+1):
- if n % x == 0:
- return False
- return True
- input = 600851475143
- factor = 2
- while input != factor:
- while input % factor == 0:
- input = input / factor
- factor += 1
- while not isPrime(factor):
- factor += 1
- print input
Advertisement
Add Comment
Please, Sign In to add comment