2.3 KiB
id, title, challengeType, dashedName
| id | title | challengeType | dashedName |
|---|---|---|---|
| 6a15cadf5f240d05a2649558 | Challenge 314: Prime Factorization | 29 | challenge-314 |
--description--
Given an integer greater than 1, return its prime factorization as an array of numbers in ascending order.
A prime factorization is the set of prime numbers that multiply together to produce the given integer. Each number has exactly one set. For example, the prime factorization of 20 is [2, 2, 5] because 2 * 2 * 5 = 20.
If the given integer is itself prime, return it in a single-element array.
--hints--
prime_factorization(20) should return [2, 2, 5].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(20), [2, 2, 5])`)
}})
prime_factorization(17) should return [17].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(17), [17])`)
}})
prime_factorization(15) should return [3, 5].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(15), [3, 5])`)
}})
prime_factorization(35) should return [5, 7].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(35), [5, 7])`)
}})
prime_factorization(999) should return [3, 3, 3, 37].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(999), [3, 3, 3, 37])`)
}})
prime_factorization(360) should return [2, 2, 2, 3, 3, 5].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(360), [2, 2, 2, 3, 3, 5])`)
}})
prime_factorization(510510) should return [2, 3, 5, 7, 11, 13, 17].
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(prime_factorization(510510), [2, 3, 5, 7, 11, 13, 17])`)
}})
--seed--
--seed-contents--
def prime_factorization(n):
return n
--solutions--
def prime_factorization(n):
factors = []
divisor = 2
while divisor * divisor <= n:
while n % divisor == 0:
factors.append(divisor)
n //= divisor
divisor += 1
if n > 1:
factors.append(n)
return factors