Find nth Magic number
A magic number is defined as a number which can be expressed as a power of 5 or sum of unique powers of 5. First few magic numbers are 5, 25, 30(5 + 25), 125, 130(125 + 5).
Given the value of n, find the nth Magic number.
Input: N=1 Output: 5 Input: N=2 Output :25 Input: N=3 Output: 30
One naïve approach is to use a set to store the sequence of magic numbers. We store the powers of 5, and then try all combination sum between the unique powers and insert the sums into the set if it already not there. Set sorts the insertion and so as the set size reaches to N, we can output the last number in the set/ largest no in the set.
Find finding sums between different combinations are tedious and N-P hard problem if solved in general.
So the solution is something different.
Let's manually compute the magic numbers first and try to observe the generated sequence as a whole.
Outputneed an explanation for this answer? contact us directly to get an explanation for this answer