Skip to content

Instantly share code, notes, and snippets.

@gauravghongde
Created March 15, 2020 17:57
Show Gist options
  • Save gauravghongde/dd1a2b9da282fabf50aaed43221218ca to your computer and use it in GitHub Desktop.
Save gauravghongde/dd1a2b9da282fabf50aaed43221218ca to your computer and use it in GitHub Desktop.
Prime factors of a number
from math import sqrt
from time import perf_counter
lst = []
#myCode -
def getPF1(n):
if n <= 1:
return
elif n == 2 :
lst.append(2)
return
elif n == 3:
lst.append(3)
return
else:
for i in range(2,int(sqrt(n))+1):
if n%i==0:
lst.append(i)
getPF1(n//i)
return
lst.append(n)
return
#cleanCode -
def getPF2(n):
factors = list()
divisor = 2
while(divisor <= n):
if (n % divisor) == 0:
factors.append(divisor)
n = n/divisor
else:
divisor += 1
return factors
#---------------
k = 3246576635467
t1_start1 = perf_counter()
getPF1(k)
print(lst)
t1_stop1 = perf_counter()
print("Elapsed time during the whole program in seconds [func 1]:", t1_stop1-t1_start1)
t1_start2 = perf_counter()
print(getPF2(k))
t1_stop2 = perf_counter()
print("Elapsed time during the whole program in seconds [func 2]:", t1_stop2-t1_start2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment