Skip to content
Related Articles

Related Articles

Improve Article

Find the Largest N digit perfect square number in Base B

  • Last Updated : 24 Mar, 2021

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:
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++




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




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




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




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

Javascript




<script>
 
// Javascript implementation to find Largest
// N digit perfect square number in Base B
 
// Function to find the
// largest N digit number
function nDigitPerfectSquares(n, b)
{
    // Largest n-digit perfect square
    var largest
        = Math.pow(Math.ceil(Math.sqrt(Math.pow(b, n))) - 1, 2);
 
    // Print the result
    document.write(largest);
}
 
// Driver Code
var N = 1, B = 8;
nDigitPerfectSquares(N, B);
 
</script>
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.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :