Created
January 7, 2020 13:21
-
-
Save ksdme/5e0c3f534f570198c464319ccdf2de11 to your computer and use it in GitHub Desktop.
Find the biggest prime factor of a number.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I have a number n | |
# Test divisinility by numbers in the range 2, Sqrt(n) | |
# If n is divisible by a prime p, for the first time, keep on dividing it with p till you find the largets power of p that divides n. | |
# Replace n by n/(p**alpha) | |
# Repeat the process until I get n = 1 or | |
# Print p | |
from math import ceil | |
subject = 600851475143 | |
answer = 0 | |
while subject > 1: | |
for index in range(2, ceil(subject ** 0.5)): | |
if subject % index == 0: | |
answer = index | |
while subject % index == 0: | |
subject /= index | |
else: | |
answer = subject | |
break | |
print(answer) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment