Skip to content

Instantly share code, notes, and snippets.

@yask123
Last active August 29, 2015 14:04
Show Gist options
  • Save yask123/861c29cdd5ca1462f788 to your computer and use it in GitHub Desktop.
Save yask123/861c29cdd5ca1462f788 to your computer and use it in GitHub Desktop.
Finding Prime Factors
import math
def isprime(n):
for num in range(2,math.floor(math.sqrt(n))+1):
if n%num==0:
return(False)
return(True)
def findfactors(n):
primes=[]
b=0
for num in range(2,math.floor(n/2)+1):
if isprime(n):
primes.append(n)
break
c=n
if(n%num==0 and isprime(num)):#Numbers must be prime and should divide n
"""B=n and A=num and C=B/A"""
c=n/num#Refer to the image in Blog
primes.append(num)
while(c%num==0):#Continue using num as long as it divides
primes.append(num)
b=c
c=b/num
n=c#Refer to the image in blog
return(primes)
print (findfactors(600851475143))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment