Python BasicsEasy

Finding Prime Factors of a number

Detailed guide and Python implementation for the 'Finding Prime Factors of a number' problem.

Problem Statement

Easy

Write a function prime_factors(n) that takes a positive integer n (n >= 2) and returns a list of its prime factors in ascending order. If a prime factor appears multiple times, include it multiple times in the list.

Constraints
  • 2 <= n <= 10^6

Examples

Example 1
Input
n = 12
Output
[2, 2, 3]
Explanation

12 = 2 × 2 × 3. The prime factors are 2, 2, and 3.

Example 2
Input
n = 7
Output
[7]
Explanation

7 is itself a prime number.

Example 3
Input
n = 60
Output
[2, 2, 3, 5]
Explanation

60 = 2 × 2 × 3 × 5.

Need a Hint?
Consider using Basics-specific data structures like sets or heaps.
Edge Cases to Watch
  • Empty input structures
  • Single element inputs
  • Large numerical bounds

Ready to Solve?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Open in Editor

Recommended Python Resources

Expand your knowledge with related interactive tutorials, cheat sheets, and code comparisons.