Prime Factorization - Problem

Write a function that takes a positive integer and returns its prime factorization as a list of prime factors.

Prime factorization breaks down a number into its constituent prime numbers. For example, 12 = 2 × 2 × 3, so the prime factors are [2, 2, 3].

Requirements:

  • Return factors in ascending order
  • Include repeated factors (e.g., for 12, include both 2's)
  • Handle edge cases like prime numbers and small inputs

Input & Output

Example 1 — Basic Composite Number
$ Input: n = 12
Output: [2, 2, 3]
💡 Note: 12 = 2 × 2 × 3, so the prime factors are [2, 2, 3] in ascending order
Example 2 — Prime Number
$ Input: n = 17
Output: [17]
💡 Note: 17 is already prime, so it has only itself as a prime factor
Example 3 — Large Factors
$ Input: n = 60
Output: [2, 2, 3, 5]
💡 Note: 60 = 2 × 2 × 3 × 5, breaking down: 60 → 30 → 15 → 5 → 1

Constraints

  • 1 ≤ n ≤ 106
  • n is a positive integer

Visualization

Tap to expand
INPUTALGORITHMRESULTn = 60To FactorFind all prime factors of 601Test factor 2: 60÷2=30, 30÷2=152Test factor 3: 15÷3=53Test factor 5: 5÷5=1 (done)4Stop at √60 ≈ 7.7Prime Factors[2, 2, 3, 5]60 = 2 × 2 × 3 × 5All factors are prime numbersKey Insight:Only test divisors up to √n - larger factors would have smaller pairs already foundTutorialsPoint - Prime Factorization | Square Root Optimization
Asked in
Google 25 Microsoft 18 Amazon 15
23.0K Views
Medium Frequency
~15 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen