Largest N digit number in Base B
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: 99
Input: 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++
#include <bits/stdc++.h>
using namespace std;
void findNumbers( int n, int b)
{
int largest = pow (b, n) - 1;
cout << largest << endl;
}
int main()
{
int N = 2, B = 5;
findNumbers(N, B);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void findNumbers( int n, int b)
{
double largest = Math.pow(b, n) - 1 ;
System.out.println(largest);
}
public static void main(String []args)
{
int N = 2 , B = 5 ;
findNumbers(N, B);
}
}
|
Python3
def findNumbers(n, b):
largest = pow (b, n) - 1
print (largest)
N, B = 2 , 5
findNumbers(N, B)
|
C#
using System;
class GFG{
static void findNumbers( int n, int b)
{
double largest = Math.Pow(b, n) - 1;
Console.Write(largest);
}
public static void Main(String []args)
{
int N = 2, B = 5;
findNumbers(N, B);
}
}
|
Javascript
<script>
function findNumbers( n, b)
{
let largest = Math.pow(b, n) - 1;
document.write(largest);
}
let N = 2, B = 5;
findNumbers(N, B);
</script>
|
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++
#include <iostream>
#include <cmath> // Include the cmath library to use pow() function.
int main() {
int N = 2;
int B = 10;
int result = 0;
for ( int i = 0; i < N; i++) {
result += pow (B, i) * (B - 1);
}
std::cout << "The largest " << N << "-digit number in base " << B << " is " << result << std::endl;
N = 2;
B = 5;
result = 0;
for ( int i = 0; i < N; i++) {
result += pow (B, i) * (B - 1);
}
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 ;
int B = 10 ;
int result = 0 ;
for ( int i = 0 ; i < N; i++) {
result += Math.pow(B, i) * (B - 1 );
}
System.out.println(
"The largest " + N + "-digit number in base " + B + " is " + result);
N = 2 ;
B = 5 ;
result = 0 ;
for ( int i = 0 ; i < N; i++) {
result += Math.pow(B, i) * (B - 1 );
}
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;
int B = 10;
int result = 0;
for ( int i = 0; i < N; i++)
{
result += ( int )Math.Pow(B, i) * (B - 1);
}
Console.WriteLine( "The largest " + N + "-digit number in base " + B + " is " + result);
N = 2;
B = 5;
result = 0;
for ( int i = 0; i < N; i++)
{
result += ( int )Math.Pow(B, i) * (B - 1);
}
Console.WriteLine( "The largest " + N + "-digit number in base " + B + " is " + result);
}
}
|
Javascript
function calculateLargestNDigitNumber(N, B) {
let result = 0;
for (let i = 0; i < N; i++) {
result += Math.pow(B, i) * (B - 1);
}
return result;
}
let N = 2;
let B = 10;
let result = calculateLargestNDigitNumber(N, B);
console.log(`The largest ${N}-digit number in base ${B} is ${result}`);
N = 2;
B = 5;
result = calculateLargestNDigitNumber(N, B);
console.log(`The largest ${N}-digit number in base ${B} is ${result}`);
|
Output
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)
Last Updated :
04 Oct, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...