Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7c889b

Browse files
author
Amogh Singhal
authored
Create primeFactorization.py
1 parent 28c699c commit c7c889b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

‎bits_wilp/primeFactorization.py‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from math import sqrt
2+
3+
def get_prime_factors(num):
4+
factors = []
5+
6+
# get all the 2's
7+
while num % 2 == 0:
8+
factors.append(2)
9+
num = num / 2
10+
11+
# check for other prime factors
12+
# sqrt is used to reduce the range by log(n)
13+
# step size of 2 to avoid checking with even numbers
14+
for i in range(3, int(sqrt(num))+1, 2):
15+
while num % i == 0:
16+
# print(num, i)
17+
factors.append(i)
18+
num = num / i
19+
20+
# num is now the last prime number
21+
if num > 2:
22+
factors.append(int(num))
23+
24+
return factors
25+
26+
27+
n = int(input("Enter the number: "))
28+
result = get_prime_factors(n)
29+
30+
print("The factors of {n} are {result}".format(n=n, result=result))
31+
32+
# Enter the number: 1081310109
33+
# The factors of 1081310109 are [3, 11, 17, 23, 181, 463]

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /