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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Check if a number exists having exactly N factors and K prime factors
- Maximum number of prime factors a number can have with exactly x factors
- Print all numbers whose set of prime factors is a subset of the set of the prime factors of X
- Prime factors of a big number
- Sum of Factors of a Number using Prime Factorization
- Number which has the maximum number of distinct prime factors in the range M to N
- Check if all Prime factors of number N are unique or not
- Product of unique prime factors of a number
- Maximum number of unique prime factors
- Check if there exists a number with X factors out of which exactly K are prime
- Super Ugly Number (Number whose prime factors are in given set)
- Number of factors of very large number N modulo M where M is any prime number
- C Program for efficiently print all prime factors of a given number
- Efficient program to print all prime factors of a given number
- Number of distinct prime factors of first n natural numbers