Faithful Numbers | Practice | GeeksforGeeks

A number is called faithful if you can write it as the sum of distinct powers of 7. 
e.g.,  2457 = 7 + 72 + 74 . If we order all the faithful numbers, we get the sequence 1 = 70, 7 = 71, 8 = 70 + 71, 49 = 72, 50 = 70 + 72 . . . and so on.
Given some value of N, you have to find the N'th faithful number.
 
Example 1:
Input:
N = 3
Output:
8
Explanation:
8 is the 3rd Faithful number.

Example 2:
Input:
N = 7
Output:
57
Explanation:
57 is the 7th Faithful number.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function nthFaithfulNum() which takes an Integer N as input and returns the answer.
 
Expected Time Complexity: O(log(N))
Expected Auxiliary Space: O(log(N))
 
Constraints:
1 <= N <= 105
0
1912100074 months ago
Just convert n to it's binary representation. Now start from the LSB and check if that bit is set then just add the power(7, that_set_bit_position-1) to the answer. and finally return the answer.                    ( Akash_musky)
 
class Solution { public:   long long nthFaithfulNum(int N) {        long long set=1;     long long Num=0;     for(long long i=0;i<=20;i++)     {         set=((N)&(1<<i));                  if(set)         {             Num+=pow(7,i);         }     }          return Num;   }}; 
0
abhinav32544 months ago
Very Simple answer in Java
 
class Solution {   static Long nthFaithfulNum(int n) {      // code here            long ans = 0;      long base = 1;      while(n>0){          int last = n&1;          n = n>>1;          ans += last*base;          base = base*7;      }      return ans;   }};
0
saivishnu1286 months ago
class Solution { public:   long long nthFaithfulNum(int N) {       if(N==1)       return 1;       long long int x=1,res=0;       while(N>0){           if(N&1)           res+=x;           x=x*7;           N>>=1;       }       return res;       // code here   }}; 
0
Anurag Dhudhwani11 months ago
Anurag Dhudhwani
JAVA Solutionstatic Long nthFaithfulNum(int N) {        int c = 0;        long s =0;      while(N>0){          int n = N&1;          s += n*(long)Math.pow(7,c++);          N = N>>1;      }      return s;    }
0
Afriduzzaman1 year ago
Afriduzzaman
long long nthFaithfulNum(long long int n) {        if(n==1)        return 1;  long long int pow = 1, answer = 0;
    while (n>0)     {
       if (n & 1)        answer += pow;        pow = pow*7;       n >>= 1;    }       return answer;    }
0
Garima Chaubey1 year ago
Garima Chaubey
class Solution {  public:    long long powers_7[25];
    void pc()    {        powers_7[0]=1;        for(int i = 1; i<=23; i++)            powers_7[i]=7*powers_7[i-1];     }
    long long nthFaithfulNum(int N) {
        pc();
        int a[30] = {0};        int i =0;
        int m = N;
        while(m>0)        {            a[i++] = m%2;            m/=2;        }
        long long sum = 0;
        for(int j = i-1; j>=0; j--)        {            if(a[j]==1)                sum += powers_7[j];        }
        return sum;    }};
0
Siddharth Sharma2 years ago
Siddharth Sharma
long long nthFaithfulNum(int N) {       long long sum=0,i;       vector<int>v;       while(N)       {           v.push_back(N%2);           N/=2;       }       for(i=0;i<v.size();i++) {="" if(v[i]="=1)" sum+="pow(7,i);" }="" return="" sum;="" }="">
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.