Skip to content

Commit

Permalink
Factors.py
Browse files Browse the repository at this point in the history
calculate out each factor of a number
  • Loading branch information
linye1111 authored Jan 12, 2018
1 parent 1e70080 commit 195f8b7
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Factors.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import time
start = time.time()
def factors(num):
L = list()
factors = list()
i = 2
while i < num:
if num%i == 0:
L.append(i)
num = int(num/i)
i = 2
continue
if i == num-1:
L.append(num)
i += 1
print(L)
print(len(L), sum(L))
i = 0
_len = len(L)
while i < _len-1:
j = i+1
p = L[i]
while j < _len and _len > 2:
y = L[i]*L[j]
if y not in L:
L.append(y)
if not (i == 0 and j==_len-1):
p*=L[j]
if p not in L:
L.append(p)
j+=1
i+=1
for x in L:
if x not in factors:
factors.append(x)
return [1]+factors

print(factors(33550336))

# bad algorithm
# L = list([1])
# for i in range(2,int(33550336/2+1)):
# if 33550336 % i == 0:
# L.append(i)
# print(len(L), sum(L))
# print(L)
print(time.time()-start)

0 comments on commit 195f8b7

Please sign in to comment.