Prime Factorization of a Number
Implement primeFactors
Given a positive integer
n, return its prime factorization as a list — every prime that divides n, repeated as many times as it divides it, in ascending order.
A composite number always has a factor at or below its own square root, so trial division never needs to test past √n — and once nothing else divides evenly, whatever is left over (if greater than 1) is itself prime.
Example 1:
Input: n = 12
Output: [2,2,3]
Example 2:
Input: n = 100
Output: [2,2,5,5]
Example 3:
Input: n = 17
Output: [17]
+ 13 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ n ≤ 10⁹
n =
12