Given two integers N and B, the task is to find the largest N digit numbers of Base B which is a perfect square.
Examples:
Input: N = 2, B = 10
Output: 81
Explanation:
81 is the largest 2-digit perfect square in base 10.Input: N = 1, B = 8
Output: 4
Explanation:
4 is the largest 1 digit Octal number which is also a perfect square.
Approach: The largest number N in base B is given by
Below is the implementation of the above approach:
// C++ implementation to find Largest // N digit perfect square number in Base B #include <bits/stdc++.h> using namespace std;
// Function to find the // largest N digit number void nDigitPerfectSquares( int n, int b)
{ // Largest n-digit perfect square
int largest
= pow ( ceil ( sqrt ( pow (b, n))) - 1, 2);
// Print the result
cout << largest;
} // Driver Code int main()
{ int N = 1, B = 8;
nDigitPerfectSquares(N, B);
return 0;
} |
// Java implementation to find largest N // digit perfect square number in base B import java.io.*;
import java.util.*;
class GFG {
// Function to find the // largest N digit number static double nDigitPerfectSquares( int n, int b)
{ // Largest n-digit perfect square
double largest = Math.pow(Math.ceil
(Math.sqrt
(Math.pow(b, n))) - 1 , 2 );
// Print the result
return largest;
} // Driver code public static void main(String[] args)
{ int N = 1 , B = 8 ;
System.out.println(nDigitPerfectSquares(N, B));
} } // This code is contributed by coder001 |
# Python3 implementation to find the largest # N digit perfect square number in base B import math
# Function to find the # largest N digit number def nDigitPerfectSquares(n, b):
# Largest n-digit perfect square
largest = pow (math.ceil
(math.sqrt( pow (b, n))) - 1 , 2 )
# Print the result
print (largest)
# Driver Code N = 1
B = 8
nDigitPerfectSquares(N, B) # This code is contributed by divyamohan123 |
// C# implementation to find largest N // digit perfect square number in base B using System;
class GFG {
// Function to find the // largest N digit number static double nDigitPerfectSquares( int n, int b)
{ // Largest n-digit perfect square
double largest = Math.Pow(Math.Ceiling
(Math.Sqrt
(Math.Pow(b, n))) - 1, 2);
// Print the result
return largest;
} // Driver code public static void Main(String[] args)
{ int N = 1, B = 8;
Console.WriteLine(nDigitPerfectSquares(N, B));
} } // This code is contributed by PrinciRaj1992 |
4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.