HiveBrain v1.2.0
Get Started
← Back to all entries
snippetpythonTip

Prime factors of number

Submitted by: @import:30-seconds-of-code··
0
Viewed 0 times
pythonfactorsnumberprime

Problem

The list of prime factors of a number is a list of prime numbers that multiply together to give the original number. You can find the list of prime factors of a number using a simple Python function.
All you really need is a while loop to iterate over all possible prime factors, starting with 2. If the current factor exactly divides num, you can add factor to the factors list and divide num by factor. Otherwise, you can increment factor by one.

Solution

def prime_factors(num):
  factors = []
  factor = 2

  while (num >= 2):
    if (num % factor == 0):
      factors.append(factor)
      num = num / factor
    else:
      factor += 1
  return factors

prime_factors(12) # [2,2,3]
prime_factors(42) # [2,3,7]

Code Snippets

def prime_factors(num):
  factors = []
  factor = 2

  while (num >= 2):
    if (num % factor == 0):
      factors.append(factor)
      num = num / factor
    else:
      factor += 1
  return factors

prime_factors(12) # [2,2,3]
prime_factors(42) # [2,3,7]

Context

From 30-seconds-of-code: prime-factors

Revisions (0)

No revisions yet.