Given an array arr of n positive integers. Represent every number as its factors (x * y = arr[i]) [Here x or y cannot be 1] till it cannot be further represented as x*y = arr[i]. Print the number of steps required to split it till no further representations are possible.
Input: 4 4 4 Output: 3 Explanation: 1st step 2 2 4 4 . 2nd step 2 2 2 2 4 3rd step 2 2 2 2 2 2 Input: 20 4 Output: 3 Explanation: 1st step 20 2 2 (2*2 = 4) 2nd step 2 10 2 2 (2*10 = 20) 3rd step 2 2 5 2 2, (2*5 = 10)
Approach is to pre calculate the prime factors of every number. Prime factors can be efficiently calculated using Sieve’s implementation which requires N * log N. . We know a number can be represented as a multiplication of its prime factors and we can represent it till its not 1, so 1 is not taken into count, so if the number is other than 1, we count the number of prime factors, and then subtract 1 from it.
Time Complexity: O( n * log n)
This article is contributed by Striver. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Maximum number of prime factors a number can have with exactly x factors
- Check if a number exists having exactly N factors and K prime factors
- Min steps to convert N-digit prime number into another by replacing a digit in each step
- Find number of factors of N when location of its two factors whose product is N is given
- Count of prime factors of N to be added at each step to convert N to M
- Number of factors of very large number N modulo M where M is any prime number
- Super Ugly Number (Number whose prime factors are in given set)
- Number with maximum number of prime factors
- Number which has the maximum number of distinct prime factors in the range M to N
- Efficient program to print all prime factors of a given number
- Maximum number of unique prime factors
- Prime factors of a big number
- C Program for efficiently print all prime factors of a given number
- Java Program for efficiently print all prime factors of a given number
- Product of unique prime factors of a number
- Number of distinct prime factors of first n natural numbers
- Sum of Factors of a Number using Prime Factorization
- Check if all Prime factors of number N are unique or not
- Check if there exists a number with X factors out of which exactly K are prime