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.
1# Helper function2def is_prime(n):3 if n < 2:4 return False5 for i in range(2, int(n ** 0.5) + 1):6 if n % i == 0:7 return False8 return True910# Take number11num = int(input("Enter a number: "))1213# Find prime factors14print(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)1920print(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
Visit possible divisors two through num.
- 2
Require divisibility and primality together.
- 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].
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum