Find all the prime factors for the given number

Input : A number // 10
Output : An array // [2, 5]

Logic :

Key here is that we need to check the divisor starting with 2 to the square root of the input number.

  • Divide the number with 2 till the remainder is not 0.
    • If its divisible with 2 then 2 to the primeFactors array.
  • Divide the number starting with 3 till the square root of the input number.
    • If its divisible with number then add to the primeFactors array.
  • At the end of the for loop if the number is greater than 2 then add the number to the primeFactors array.

Solution :

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *