Find nth Magic Number

07 Jun, 2022
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.Example: 
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 a given integer n. Below is the implementation based on this idea.  
Approach : 
Step 1 : declare and assign a number for which you want to find the magic number.
Step 2 : assign a pow = 1, and ans = 0
Step 3 : use while loop to iterate each bit until ends (while n > 0)
Step 4 : inside loop, find last bit using & operation and  keep updating answer and  power as well
Step 5 : Once loop exit return answer
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find nth magic number#include <bits/stdc++.h>using namespace std; // Function to find nth magic numberint nthMagicNo(int n){    int pow = 1, answer = 0;     // Go through every bit of n    while (n)    {       pow = pow*5;        // If last bit of n is set       if (n & 1)         answer += pow;        // proceed to next bit       n >>= 1;  // or n = n/2    }    return answer;} // Driver program to test above functionint main(){    int n = 5;    cout << "nth magic number is " << nthMagicNo(n) << endl;    return 0;}
// Java program to find nth// magic numberimport java.io.*; class GFG{  // Function to find nth magic number  static int nthMagicNo(int n)  {     int pow = 1, answer = 0;       // Go through every bit of n     while (n != 0)     {       pow = pow*5;         // If last bit of n is set       if ((int)(n & 1) == 1)         answer += pow;         // proceed to next bit       // or n = n/2       n >>= 1;      }     return answer;  }    // Driver program to test  // above function  public static void main(String[] args)  {    int n = 5;    System.out.println("nth magic" +    " number is " + nthMagicNo(n));  }}  // This code is contributed by// prerna saini
# Python program to find nth magic number # Function to find nth magic numberdef nthMagicNo(n):     pow = 1    answer = 0     # Go through every bit of n    while (n):         pow = pow*5         # If last bit of n is set        if (n & 1):            answer += pow         # proceed to next bit        n >>= 1 # or n = n/2         return answer  # Driver program to test above functionn = 5print("nth magic number is", nthMagicNo(n)) # This code is contributed by# Smitha Dinesh Semwal
// C# program to find nth// magic numberusing System; public class GFG{     // Function to find nth magic numberstatic int nthMagicNo(int n){    int pow = 1, answer = 0;     // Go through every bit of n    while (n != 0)    {        pow = pow * 5;         // If last bit of n is set        if ((int)(n & 1) == 1)            answer += pow;             // proceed to next bit        // or n = n/2        n >>= 1;    }    return answer;} // Driver Codepublic static void Main(){    int n = 5;    Console.WriteLine("nth magic" +    " number is "                       + nthMagicNo(n)); }} // This code is contributed by Sam007
<?php// PHP program to find nth// magic number // Function to find nth// magic numberfunction nthMagicNo($n){    $pow = 1;    $answer = 0;     // Go through every bit of n    while ($n)    {    $pow = $pow * 5;     // If last bit of n is set    if ($n & 1)        $answer += $pow;     // proceed to next bit    $n >>= 1; // or $n = $n/2    }    return $answer;} // Driver Code$n = 5;echo "nth magic number is ",       nthMagicNo($n), "\n"; // This code is contributed by Ajit.?>
<script>     // Javascript program to find nth    // magic number         // Function to find nth magic number    function nthMagicNo(n)    {        let pow = 1, answer = 0;         // Go through every bit of n        while (n != 0)        {            pow = pow * 5;             // If last bit of n is set            if ((n & 1) == 1)                answer += pow;             // proceed to next bit            // or n = n/2            n >>= 1;        }        return answer;    }         let n = 5;    document.write("nth magic" + " number is " + nthMagicNo(n));     </script>
Output : 
 nth magic number is 130 
Complexity : 
Time complexity : O(logN)
Auxiliary Space : O(1)
Thanks to manrajsingh for suggesting the above solution. 
Find nth Magic Number | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersFind nth Magic Number | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:23•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=Rmjqr6U4k50" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
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. 
Sam007
jit_t
decode2207
sagar0719kumar
devendrasalunke
Accolite
Amazon
Samsung
Bit Magic
Mathematical
Accolite
Amazon
Samsung
Mathematical
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Little and Big Endian Mystery
Bits manipulation (Important tactics)
Binary representation of a given number
Josephus problem | Set 1 (A O(n) Solution)
Bit Fields in C
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays