07 Least Prime Factor
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
To solve the problem, I have used a simple brute force approach to find the least prime factor for each number up to n
. Here are the steps I followed:
I implemented the findFactor
function, which takes an integer n
as input and returns its least prime factor.
Within the findFactor
function, I first check if n
is less than 2. If it is, I return n
itself.
Then, I iterate from 2 to the square root of n
(inclusive) and check if n
is divisible by i
. If it is, I return i
as the least prime factor.
If no prime factor is found in the range, I return n
as the least prime factor.
Finally, I implemented the leastPrimeFactor
function, which takes an integer n
as input and returns a vector out
containing the least prime factor for each number from 0 to n
.
Within the leastPrimeFactor
function, I initialized the vector out
with size n+1
.
Then, I iterated from 0 to n
and assigned the least prime factor of each number to the corresponding index in the out
vector.
Finally, I returned the out
vector.
Time Complexity: O(n*sqrt(n))
, where n
is the given number. This is because for each number from 0 to n
, we iterate up to the square root of the number to find its least prime factor.
Auxiliary Space Complexity: O(n)
since we are using a vector out
of size n+1
to store the least prime factors for each number.
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.