Back to Practice Dashboard
Python BasicsEasy

Finding Prime Factors of a number

Learn how to solve the 'Finding Prime Factors of a number' problem. This detailed resource details brute force and optimized approaches.

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?
Use simple arithmetic operators (like modulo `%`, division `//`), conditional checks, or loops to inspect number properties.
Edge Cases to Watch
  • Empty list or null input variables
  • Single item lists/arrays
  • Extremely large input bounds causing integer or stack overflow

Ready to Solve?

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

Open in Editor