Given a positive integer N and base B, the task is to find the largest N-digit numbers of Base B in decimal form.
Examples:
Input: N = 2, B = 10
Output: 99Input: N = 2, B = 5
Output: 24
Approach:
Since there are B digits in base B, so with these digits we can create BN strings of length N. They represent the integers in range 0 to BN – 1
Therefore, the largest N-digits number of base B in decimal form is given by BN – 1.
Below is the implementation of the above approach:
C++
// C++ program for the approach #include <bits/stdc++.h> using namespace std; // Function to print the largest // N-digit numbers of base b void findNumbers( int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 int largest = pow (b, n) - 1; // Print the largest number cout << largest << endl; } // Driver Code int main() { // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); return 0; } |
Java
// Java program for the approach import java.util.*; class GFG{ // Function to print the largest // N-digit numbers of base b static void findNumbers( int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 double largest = Math.pow(b, n) - 1 ; // Print the largest number System.out.println(largest); } // Driver Code public static void main(String []args) { // Given Number and Base int N = 2 , B = 5 ; // Function Call findNumbers(N, B); } } // This code is contributed by Ritik Bansal |
Python3
# Python3 program for the above approach # Function to print the largest # N-digit numbers of base b def findNumbers(n, b): # Find the largest N digit # number in base b using the # formula B^N - 1 largest = pow (b, n) - 1 # Print the largest number print (largest) # Driver Code # Given number and base N, B = 2 , 5 # Function Call findNumbers(N, B) # This code is contributed by jrishabh99 |
C#
// C# program for the approach using System; class GFG{ // Function to print the largest // N-digit numbers of base b static void findNumbers( int n, int b) { // Find the largest N digit // number in base b using the // formula B^N - 1 double largest = Math.Pow(b, n) - 1; // Print the largest number Console.Write(largest); } // Driver Code public static void Main(String []args) { // Given Number and Base int N = 2, B = 5; // Function Call findNumbers(N, B); } } // This code is contributed by shivanisinghss2110 |
Javascript
<script> // javascript program for the approach // Function to print the largest // N-digit numbers of base b function findNumbers( n, b) { // Find the largest N digit // number in base b using the // formula B^N - 1 let largest = Math.pow(b, n) - 1; // Print the largest number document.write(largest); } // Driver Code // Given Number and Base let N = 2, B = 5; // Function Call findNumbers(N, B); // This code is contributed by aashish1995 </script> |
24
Time Complexity: O(1)
Auxiliary Space: O(1)
METHOD 2:Using Math formula
APPROACH:
We can find the largest N-digit number in base B by simply calculating the sum of (B-1) multiplied by B to the power of i (where i ranges from 0 to N-1).
ALGORITHM:
1.Take the input values of N and B from the user.
2.Initialize a variable ‘result’ to 0.
3.Run a loop from 0 to N-1 and for each iteration, calculate the sum of (B-1) multiplied by B to the power of i and add it to ‘result’.
4.Display the value of ‘result’ as the largest N-digit number in base B.
C++
// C++ program for the approach #include <iostream> #include <cmath> // Include the cmath library to use pow() function. int main() { int N = 2; // Number of digits in the number. int B = 10; // Base of the number system. int result = 0; // Initialize the variable to store the result. // Loop to calculate the largest N-digit number in base B. for ( int i = 0; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += pow (B, i) * (B - 1); } // Print the result. std::cout << "The largest " << N << "-digit number in base " << B << " is " << result << std::endl; N = 2; // Reset N to 2. B = 5; // Change the base to 5. result = 0; // Reset the result variable. // Loop to calculate the largest N-digit number in base B. for ( int i = 0; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += pow (B, i) * (B - 1); } // Print the result. std::cout << "The largest " << N << "-digit number in base " << B << " is " << result << std::endl; return 0; } |
Java
import java.lang.Math; public class Main { public static void main(String[] args) { int N = 2 ; // Number of digits in the number. int B = 10 ; // Base of the number system. int result = 0 ; // Initialize the variable to store the result. // Loop to calculate the largest N-digit number in base B. for ( int i = 0 ; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += Math.pow(B, i) * (B - 1 ); } // Print the result. System.out.println( "The largest " + N + "-digit number in base " + B + " is " + result); N = 2 ; // Reset N to 2. B = 5 ; // Change the base to 5. result = 0 ; // Reset the result variable. // Loop to calculate the largest N-digit number in base B. for ( int i = 0 ; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += Math.pow(B, i) * (B - 1 ); } // Print the result. System.out.println( "The largest " + N + "-digit number in base " + B + " is " + result); } } |
Python3
N = int ( 2 ) B = int ( 10 ) result = 0 for i in range (N): result + = (B * * i) * (B - 1 ) print (f "The largest {N}-digit number in base {B} is {result}" ) N = int ( 2 ) B = int ( 5 ) result = 0 for i in range (N): result + = (B * * i) * (B - 1 ) print (f "The largest {N}-digit number in base {B} is {result}" ) |
C#
using System; class GFG { static void Main() { int N = 2; // Number of digits in the number. int B = 10; // Base of the number system. int result = 0; // Initialize the variable to store the result. // Loop to calculate the largest N-digit number in base B. for ( int i = 0; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += ( int )Math.Pow(B, i) * (B - 1); } // Print the result. Console.WriteLine( "The largest " + N + "-digit number in base " + B + " is " + result); N = 2; // Reset N to 2. B = 5; // Change the base to 5. result = 0; // Reset the result variable. // Loop to calculate the largest N-digit number in base B. for ( int i = 0; i < N; i++) { // Calculate (B^i) * (B - 1) and add it to the result. result += ( int )Math.Pow(B, i) * (B - 1); } // Print the result. Console.WriteLine( "The largest " + N + "-digit number in base " + B + " is " + result); } } |
Javascript
// JavaScript program to calculate the largest N-digit number in base B // Function to calculate the largest N-digit number in base B function calculateLargestNDigitNumber(N, B) { let result = 0; // Loop through each digit position from 0 to N-1 for (let i = 0; i < N; i++) { // Calculate the value of the digit position by multiplying B to the power of i // and then subtracting 1 (since the largest digit in base B is B-1) result += Math.pow(B, i) * (B - 1); } // Return the result, which is the largest N-digit number in base B return result; } // Test case 1: N = 2, B = 10 (decimal) let N = 2; let B = 10; let result = calculateLargestNDigitNumber(N, B); console.log(`The largest ${N}-digit number in base ${B} is ${result}`); // Test case 2: N = 2, B = 5 (quinary) N = 2; B = 5; result = calculateLargestNDigitNumber(N, B); console.log(`The largest ${N}-digit number in base ${B} is ${result}`); |
The largest 2-digit number in base 10 is 99 The largest 2-digit number in base 5 is 24
Time complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!