A Seed of a number n is a number x such that multiplication of x with its digits is equal to n. The task is to find all seeds of a given number n. If no seed exists, then print the same.
Input : n = 138 Output : 23 23 is a seed of 138 because 23*2*3 is equal to 138 Input : n = 4977 Output : 79 711 79 is a seed of 4977 because 79 * 7 * 9 = 4977. 711 is also a seed of 4977 because 711 * 1 * 1 * 7 = 4977 Input : n = 9 Output : No seed exists Input : n = 738 Output : 123
Asked in Epic
The idea is to traverse all numbers from 1 to n/2. For every number being traversed, find product of digits with the number. An important optimization done in below program is to avoid re-computations of digit products. We store the products in an array. If a product has already been computed, we return it, else we compute it.
Below is the implementation of the idea.
Further Optimization :
We can further optimize above code. The idea is to make a call to getDigitProduct(i) only if i is divisible by n. Please refer https://ide.geeksforgeeks.org/oLYduu for implementation.
This article is contributed by Rakesh Kumar. 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.
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.
- Find the number of primitive roots modulo prime
- Roots of Unity
- Find the quadratic equation from the given roots
- Form the Cubic equation from the given roots
- Roots of the quadratic equation when a + b + c = 0 without using Shridharacharya formula
- Check if roots of a Quadratic Equation are reciprocal of each other or not
- Find the integral roots of a given Cubic equation
- Bakhshali Approximation for computing square roots
- Find if two given Quadratic equations have common roots or not
- Program to find the Roots of Quadratic equation
- Boundary Value Analysis : Nature of Roots of a Quadratic equation
- Absolute difference between sum and product of roots of a quartic equation
- Check if roots of a Quadratic Equation are numerically equal but opposite in sign or not
- Number of factors of very large number N modulo M where M is any prime number
- Count number of triplets with product equal to given number with duplicates allowed
- Minimum number of distinct powers of 2 required to express a given binary number
- Maximum number formed from array with K number of adjacent swaps allowed
- Count number of trailing zeros in Binary representation of a number using Bitset
- Find minimum number to be divided to make a number a perfect square
- Find the largest number smaller than integer N with maximum number of set bits
Improved By : Mithun Kumar