Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

Prime Factors of Number

List distinct prime divisors of a positive integer with Python. Students combine factor and prime tests and explain why repeated factors appear only once.

AdvancedPhase 6 - Category 3: Loop & Conditional MixExample 6 of 10
prime-factors-of-number.py
1# Helper function
2def is_prime(n):
3 if n < 2:
4 return False
5 for i in range(2, int(n ** 0.5) + 1):
6 if n % i == 0:
7 return False
8 return True
9
10# Take number
11num = int(input("Enter a number: "))
12
13# Find prime factors
14print(f"Prime factors of {num}:")
15factors = []
16for i in range(2, num + 1):
17 if num % i == 0 and is_prime(i):
18 factors.append(i)
19
20print(factors)

Output

Enter a number: 60
Prime factors of 60:
[2, 3, 5]

In this one

  • Filter prime divisors
  • List distinct factors
  • Recognize multiplicity differences

How it works

  1. 1

    Visit possible divisors two through num.

  2. 2

    Require divisibility and primality together.

  3. 3

    Append each matching divisor once to factors.

Edge cases

Are repeated prime factors listed repeatedly?

Are repeated prime factors listed repeatedly?

No. Each candidate is visited once.

What is the result for one?

What is the result for one?

An empty list.

What's going on

Each candidate i is tested once from two through num. It is appended only when it divides num and is itself prime.

For twelve, the result is [2, 3], not [2, 2, 3]. This lists distinct prime divisors rather than a full repeated-factor decomposition. One has no prime divisors; nonpositive inputs create an empty candidate range in this basic version.

Are repeated prime factors listed repeatedly? No. Each candidate is visited once.

What is the result for one? An empty list.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does twelve produce?

Pick an answer to check it.

Question 2

Are repeated prime factors listed repeatedly?

Pick an answer to check it.

Questions people ask

Are repeated prime factors listed repeatedly?

No. Each candidate is visited once.

What is the result for one?

An empty list.

What does twelve produce?

[2, 3].