- Notifications
You must be signed in to change notification settings - Fork 152
/
Copy pathprimeFactorization.py
33 lines (24 loc) · 749 Bytes
/
primeFactorization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
frommathimportsqrt
defget_prime_factors(num):
factors= []
# get all the 2's
whilenum%2==0:
factors.append(2)
num=num/2
# check for other prime factors
# sqrt is used to reduce the range by log(n)
# step size of 2 to avoid checking with even numbers
foriinrange(3, int(sqrt(num))+1, 2):
whilenum%i==0:
# print(num, i)
factors.append(i)
num=num/i
# num is now the last prime number
ifnum>2:
factors.append(int(num))
returnfactors
n=int(input("Enter the number: "))
result=get_prime_factors(n)
print("The factors of {n} are {result}".format(n=n, result=result))
# Enter the number: 1081310109
# The factors of 1081310109 are [3, 11, 17, 23, 181, 463]