|
| 1 | +--- |
| 2 | +id: faithful-numbers |
| 3 | +title: Faithful Numbers |
| 4 | +sidebar_label: Faithful-Numbers |
| 5 | +tags: |
| 6 | + - Array |
| 7 | + - Data Structure |
| 8 | +description: "This tutorial covers the solution to the Faithful Numbers problem from the GeeksforGeeks website." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +A number is called faithful if you can write it as the sum of distinct powers of 7. |
| 13 | +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. |
| 14 | +Given some value of `N`, you have to find the N'th faithful number. |
| 15 | + |
| 16 | +## Examples |
| 17 | + |
| 18 | +**Example 1:** |
| 19 | + |
| 20 | +``` |
| 21 | +Input: |
| 22 | +N = 3 |
| 23 | +Output: |
| 24 | +8 |
| 25 | +Explanation: |
| 26 | +8 is the 3rd Faithful number. |
| 27 | +``` |
| 28 | + |
| 29 | +**Example 2:** |
| 30 | + |
| 31 | +``` |
| 32 | +Input: |
| 33 | +N = 7 |
| 34 | +Output: |
| 35 | +57 |
| 36 | +Explanation: |
| 37 | +57 is the 7th Faithful number. |
| 38 | +``` |
| 39 | + |
| 40 | +## Your Task |
| 41 | +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. |
| 42 | + |
| 43 | + |
| 44 | +Expected Time Complexity: $O(log(n))$ |
| 45 | + |
| 46 | +Expected Auxiliary Space: $O(log(n))$ |
| 47 | + |
| 48 | +## Constraints |
| 49 | + |
| 50 | +* `1 ≤ n ≤ 10^5` |
| 51 | + |
| 52 | +## Problem Explanation |
| 53 | +A number is called faithful if you can write it as the sum of distinct powers of 7. |
| 54 | +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. |
| 55 | +Given some value of `N`, you have to find the N'th faithful number. |
| 56 | + |
| 57 | + |
| 58 | +## Code Implementation |
| 59 | + |
| 60 | +<Tabs> |
| 61 | + <TabItem value="Python" label="Python" default> |
| 62 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 63 | + |
| 64 | + ```python |
| 65 | + |
| 66 | +def get_nth_faithful_number(n): |
| 67 | + faithful_numbers = [] |
| 68 | + power = 0 |
| 69 | + while len(faithful_numbers) < n: |
| 70 | + num = 7 ** power |
| 71 | + faithful_numbers.append(num) |
| 72 | + for i in range(len(faithful_numbers) - 1): |
| 73 | + faithful_numbers.append(num + faithful_numbers[i]) |
| 74 | + power += 1 |
| 75 | + return faithful_numbers[n - 1] |
| 76 | + |
| 77 | +n = int(input("Enter the value of N: ")) |
| 78 | +print("The {}th faithful number is: {}".format(n, get_nth_faithful_number(n))) |
| 79 | + |
| 80 | + ``` |
| 81 | + |
| 82 | + </TabItem> |
| 83 | + <TabItem value="C++" label="C++"> |
| 84 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 85 | + |
| 86 | + ```cpp |
| 87 | +#include <iostream> |
| 88 | +#include <vector> |
| 89 | +#include <cmath> |
| 90 | + |
| 91 | +int getNthFaithfulNumber(int n) { |
| 92 | + std::vector<int> faithfulNumbers; |
| 93 | + int power = 0; |
| 94 | + while (faithfulNumbers.size() < n) { |
| 95 | + int num = pow(7, power); |
| 96 | + faithfulNumbers.push_back(num); |
| 97 | + for (int i = 0; i < faithfulNumbers.size() - 1; i++) { |
| 98 | + faithfulNumbers.push_back(num + faithfulNumbers[i]); |
| 99 | + } |
| 100 | + power++; |
| 101 | + } |
| 102 | + return faithfulNumbers[n - 1]; |
| 103 | +} |
| 104 | +int main() { |
| 105 | + int n; |
| 106 | + std::cout << "Enter the value of N: "; |
| 107 | + std::cin >> n; |
| 108 | + std::cout << "The " << n << "th faithful number is: " << getNthFaithfulNumber(n) << std::endl; |
| 109 | + return 0; |
| 110 | +} |
| 111 | + |
| 112 | + ``` |
| 113 | +
|
| 114 | + </TabItem> |
| 115 | +
|
| 116 | + <TabItem value="Javascript" label="Javascript" default> |
| 117 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 118 | +
|
| 119 | + ```javascript |
| 120 | + function getNthFaithfulNumber(n) { |
| 121 | + let faithfulNumbers = []; |
| 122 | + let power = 0; |
| 123 | + while (faithfulNumbers.length < n) { |
| 124 | + let num = Math.pow(7, power); |
| 125 | + faithfulNumbers.push(num); |
| 126 | + for (let i = 0; i < faithfulNumbers.length - 1; i++) { |
| 127 | + faithfulNumbers.push(num + faithfulNumbers[i]); |
| 128 | + } |
| 129 | + power++; |
| 130 | + } |
| 131 | + return faithfulNumbers[n - 1]; |
| 132 | +} |
| 133 | +let n = parseInt(prompt("Enter the value of N:")); |
| 134 | +alert("The " + n + "th faithful number is: " + getNthFaithfulNumber(n)); |
| 135 | +
|
| 136 | +
|
| 137 | + ``` |
| 138 | + |
| 139 | + </TabItem> |
| 140 | + |
| 141 | + <TabItem value="Typescript" label="Typescript" default> |
| 142 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 143 | + |
| 144 | + ```typescript |
| 145 | + |
| 146 | +function getNthFaithfulNumber(n: number): number { |
| 147 | + let faithfulNumbers: number[] = []; |
| 148 | + let power: number = 0; |
| 149 | + while (faithfulNumbers.length < n) { |
| 150 | + let num: number = Math.pow(7, power); |
| 151 | + faithfulNumbers.push(num); |
| 152 | + for (let i: number = 0; i < faithfulNumbers.length - 1; i++) { |
| 153 | + faithfulNumbers.push(num + faithfulNumbers[i]); |
| 154 | + } |
| 155 | + power++; |
| 156 | + } |
| 157 | + return faithfulNumbers[n - 1]; |
| 158 | +} |
| 159 | + |
| 160 | +let n: number = parseInt(prompt("Enter the value of N:")); |
| 161 | +alert("The " + n + "th faithful number is: " + getNthFaithfulNumber(n)); |
| 162 | + |
| 163 | + |
| 164 | + ``` |
| 165 | + |
| 166 | + </TabItem> |
| 167 | + |
| 168 | + <TabItem value="Java" label="Java" default> |
| 169 | + <SolutionAuthor name="@Ishitamukherjee2004"/> |
| 170 | + |
| 171 | + ```java |
| 172 | +import java.util.*; |
| 173 | + |
| 174 | +public class Main { |
| 175 | + public static int getNthFaithfulNumber(int n) { |
| 176 | + List<Integer> faithfulNumbers = new ArrayList<>(); |
| 177 | + int power = 0; |
| 178 | + while (faithfulNumbers.size() < n) { |
| 179 | + int num = (int) Math.pow(7, power); |
| 180 | + faithfulNumbers.add(num); |
| 181 | + for (int i = 0; i < faithfulNumbers.size() - 1; i++) { |
| 182 | + faithfulNumbers.add(num + faithfulNumbers.get(i)); |
| 183 | + } |
| 184 | + power++; |
| 185 | + } |
| 186 | + return faithfulNumbers.get(n - 1); |
| 187 | + } |
| 188 | + public static void main(String[] args) { |
| 189 | + Scanner scanner = new Scanner(System.in); |
| 190 | + System.out.print("Enter the value of N: "); |
| 191 | + int n = scanner.nextInt(); |
| 192 | + System.out.println("The " + n + "th faithful number is: " + getNthFaithfulNumber(n)); |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | + |
| 197 | + ``` |
| 198 | + |
| 199 | + </TabItem> |
| 200 | +</Tabs> |
| 201 | + |
| 202 | + |
| 203 | +## Solution Logic: |
| 204 | +This solution works by generating faithful numbers on the fly and storing them in a vector. It starts with the smallest faithful number, 1 (which is 7^0), and then generates larger faithful numbers by adding powers of 7 to the previously generated numbers. |
| 205 | + |
| 206 | + |
| 207 | +## Time Complexity |
| 208 | + |
| 209 | +* The function iterates through the array once, so the time complexity is $O(n log n)$. |
| 210 | + |
| 211 | +## Space Complexity |
| 212 | + |
| 213 | +* The function uses additional space for the result list, so the auxiliary space complexity is $O(n)$. |
0 commit comments