Skip to main content

Prime Factors

Definition​

The Prime Factors Algorithm is a computational method used to find the prime factors of a given integer. It leverages the fundamental theorem of arithmetic, which states that every integer greater than 1 either is a prime number itself or can be factorized into a unique combination of prime numbers. The algorithm efficiently identifies these prime factors, aiding in various mathematical and cryptographic applications

Practice​

prime_factors(n):
factors = [] # Initialize empty list to store prime factors
divisor = 2 # Start with the smallest prime number

while n > 1:
while n % divisor == 0: # Check if divisor divides n evenly
factors.append(divisor) # Add divisor to list of prime factors
n = n / divisor # Update n by dividing by divisor
divisor = divisor + 1 # Move to the next number
return factors # Return the list of prime factors