![]() |
VOOZH | about |
Given a number n, print least prime factors of all numbers from 1 to n. The least prime factor of an integer n is the smallest prime number that divides the number.
Note: We need to print 1 for 1 and 0 for 0 even of 0
Input: n = 6
Output: [0, 1, 2, 3, 2, 5, 2]
Explanation:
Number : 0 1 2 3 4 5 6
Least Prime Factor: 0 1 2 3 2 5 2Input: n = 11
Output: [0, 1, 2, 3, 2, 5, 2, 7, 2, 3, 2, 11]
Explanation:
Number: 0 1 2 3 4 5 6 7 8 9 10 11
Least Prime Factor: 0 1 2 3 2 5 2 7 2 3 2 11
Table of Content
1 to n, find its least prime factor by checking divisibility starting from 2 up to its square root. i is its smallest prime factor, and if no such divisor is found, then the number itself is prime.1 and 0 are handled separately by assigning it values as 1 and 0 respectively.0 1 2 3 2 5 2
We can use a variation of sieve of Eratosthenes to solve the above problem.
0 1 2 3 2 5 2