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
. 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:
.
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 numbervoid 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 Codeint main(){ int N = 1, B = 8; nDigitPerfectSquares(N, B); return 0;} |
Java
// Java implementation to find largest N// digit perfect square number in base Bimport java.io.*;import java.util.*;class GFG { // Function to find the// largest N digit numberstatic 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 codepublic 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 Bimport math# Function to find the# largest N digit numberdef 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 CodeN = 1B = 8nDigitPerfectSquares(N, B)# This code is contributed by divyamohan123 |
C#
// C# implementation to find largest N// digit perfect square number in base Busing System;class GFG { // Function to find the// largest N digit numberstatic 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 codepublic 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 numberfunction 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 Codevar N = 1, B = 8;nDigitPerfectSquares(N, B);</script> |
4
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.


