Find the Largest N digit perfect square number in Base B

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 B^N-1. So if we find the square root of this number in integer form and then we have to again do its square then it will be the largest perfect square of N digits which is given by the formula: \left\lfloor\sqrt{B^N-1}\right\rfloor^2.



Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.