Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions primefactorization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import math

def main():
print("Generating the prime factors of a given number.")
num = int(input("\nEnter a number: "))

def prime_factors(n):
count = 0

while n % 2 == 0: #Dividing the number as many times as possible by 2.
print(2)
n /= 2
count += 1

#Dividing the number by all possible odd numbers (now that the number is no longer an even number).
for num in range(3,int(math.sqrt(n)+1), 2):
while n % num == 0: #As many times as possible by each number.
print(num)
n /= num
count += 1

if count == 0: # In essence, if none of the loops have ever been executed. Will happen only in case of odd primes.
print(int(n))
prime_factors(num)