Created
February 13, 2018 07:50
-
-
Save MohanSha/4ce0226cf651e075e45e4faeca4e71ad to your computer and use it in GitHub Desktop.
Have the user enter a number and find all Prime Factors (if there are any) and display them.
This file contains 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
# Prime Factorization | |
# Have the user enter a number and find all Prime Factors (if there are any) and display them. | |
import math | |
def is_prime(num): | |
if num < 2: | |
return False | |
for i in range(2, int(math.sqrt(num))+1): | |
if num % i == 0: | |
return False | |
return True | |
def get_prime_factors(n): | |
res = [] | |
for i in range(1, n/2+1): | |
if (n % i == 0) and is_prime(i): | |
res.append(i) | |
if is_prime(n): | |
res.append(n) | |
return res | |
if __name__ == "__main__": | |
try: | |
n = int(raw_input("Pick a number: ")) | |
pf = get_prime_factors(n) | |
print pf | |
except: | |
print "Invalid input" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment