Given a positive number n, we need to print exactly k positive numbers (all greater than 1) such that product of those k numbers is n. If there doesn’t exist such k numbers, print -1 . If there are many possible answer you have to print one of that answer where k numbers are sorted.
Input : n = 54, k = 3 Output : 2, 3, 9 Note that 2, 3 and 9 are k numbers with product equals to n. Input : n = 54, k = 8 Output : -1
This problem uses idea very similar to print all prime factors of a given number.
The idea is very simple. First we calculate all prime factors of n and store them in a vector. Note we store each prime number as many times as it appears in it’s prime factorization. Now to find k numbers greater than 1, we check if size of our vector is greater then or equal to k or not.
- If size is less than k we print -1.
- Else we print first k-1 factors as it is from vector and last factor is product of all the remaining elements of vector.
Note we inserted all the prime factors in sorted manner hence all our number in vector are sorted. This also satisfy our sorted condition for k numbers.
2, 3, 9
This article is contributed by Pratik Chhajer. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Find the prime numbers which can written as sum of most consecutive primes
- Find two numbers with sum and product both same as N
- Find the Product of first N Prime Numbers
- Find two distinct prime numbers with given product
- Find maximum product of digits among numbers less than or equal to N
- Check if a number can be written as a sum of 'k' prime numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Check whether product of 'n' numbers is even or odd
- Number of digits in the product of two numbers
- Product of all the Composite Numbers in an array
- First digit in product of an array of numbers
- Largest palindrome which is product of two n-digit numbers
- Counting numbers whose difference from reverse is a product of k
- Check if product of first N natural numbers is divisible by their sum