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), ….
Write a function to find the nth Magic number.
Input: n = 2 Output: 25 Input: n = 5 Output: 130
If we notice carefully the magic numbers can be represented as 001, 010, 011, 100, 101, 110 etc, where 001 is 0*pow(5,3) + 0*pow(5,2) + 1*pow(5,1). So basically we need to add powers of 5 for each bit set in given integer n.
Below is the implementation based on this idea.
nth magic number is 130
Thanks to manrajsingh for suggesting above solution.
This article is contributed by Abhay. 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 Majority Element | Set 3 (Bit Magic)
- Check if a number is magic (Recursive sum of digits is 1)
- Find Nth number in a sequence which is not a multiple of a given number
- Magic Square
- The Magic of Fibonacci Numbers
- Magic Square | Even Order
- Josephus Problem Using Bit Magic
- Count Magic squares in a grid
- Program to find last two digits of Nth Fibonacci number
- Find nth Fibonacci number using Golden ratio
- Find nth number that contains the digit k or divisible by k.
- Find Nth even length palindromic number formed using digits X and Y
- Find the Nth Mosaic number
- Program to find the nth Kynea number
- Find Nth number of the series 1, 6, 15, 28, 45, .....
- Program to find the Nth Harmonic Number
- Program to find the Nth number of the series 2, 10, 24, 44, 70.....
- Find nth Hermite number
- Find Nth positive number whose digital root is X
- Find Nth smallest number that is divisible by 100 exactly K times