Magic Number
PROBLEM :
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:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the value of N.
Output:
Print the nth magic number in a separate line.
Constraints:
1 = T = 30
1 = N = 100
Example:
Input:
2
1
2
Output:
5
25
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Magic_Number(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Magic_Number(no)<<endl ;
}
return 0;
}
int Magic_Number(int no)
{
int i,k,j ;
int arr[1000] ;
i=1;
k=0 ;
arr[k]=1 ;
no=no ;
while(true)
{
arr[i]=arr[k]*5 ;
if(i==no)
return arr[i] ;
j=1 ;
k=i ;
i++ ;
while(j!=k)
{
arr[i]=arr[k]+arr[j++] ;
if(i==no)
return arr[i] ;
i++ ;
}
}
}
---------------------------------------------------------------------------------
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:
The first line contains an integer T, depicting total number of test cases.
Then following T lines contains an integer N depicting the value of N.
Output:
Print the nth magic number in a separate line.
Constraints:
1 = T = 30
1 = N = 100
Example:
Input:
2
1
2
Output:
5
25
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int Magic_Number(int ) ;
int main()
{
int t,no ;
cin>>t ;
while(t--)
{
cin>>no ;
cout<<Magic_Number(no)<<endl ;
}
return 0;
}
int Magic_Number(int no)
{
int i,k,j ;
int arr[1000] ;
i=1;
k=0 ;
arr[k]=1 ;
no=no ;
while(true)
{
arr[i]=arr[k]*5 ;
if(i==no)
return arr[i] ;
j=1 ;
k=i ;
i++ ;
while(j!=k)
{
arr[i]=arr[k]+arr[j++] ;
if(i==no)
return arr[i] ;
i++ ;
}
}
}
---------------------------------------------------------------------------------
Comments
Post a Comment