![]() |
VOOZH | about |
Given a number n, the task is to print all of its prime factors. Prime factors are the prime numbers that divide a given number exactly without leaving a remainder. For example:
Input: 12
Output: 2 2 3
Let's explore different methods to find all prime factors of a number in Python.
In this method, we precompute smallest prime factor for every number up to n using a sieve-like approach. Then, we use these precomputed values to efficiently print all prime factors of the given number.
3 3 5 7
Explanation:
Here, we use math module to divide the number continuously by 2 and then by all odd divisors up to its square root. Each divisor that divides the number completely is printed as a prime factor.
3 3 5 7
Explanation:
In this method, number is divided repeatedly by the smallest divisor starting from 2. Each divisor that divides the number is printed immediately, and the process continues until the number becomes 1.
3 3 5 7
Explanation:
Here, we use a lambda expression to generate prime factors of the number dynamically. The code repeatedly finds and removes each factor until the number is reduced to 1.
3 5 7 3
Explanation: