Skip to content

Instantly share code, notes, and snippets.

@MohanSha
Created February 13, 2018 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohanSha/4ce0226cf651e075e45e4faeca4e71ad to your computer and use it in GitHub Desktop.
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.
# 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