Open In App

Find the Largest N digit perfect square number in Base B

Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Brute Force Approach:

1) isPerfectSquare checks if a given number is a perfect square.
2) decimalToBase converts a decimal number to any base.
3) largestNDigitPerfectSquare finds the largest N digit perfect square in base B.

largestNDigitPerfectSquare function first calculates the largest N digit number in base B. It then iterates over all numbers starting from this largest number and checks whether each number is a perfect square in decimal. If a perfect square is found, it converts it to base B and returns it.

Implementation of the above approach:

C++

#include <bits/stdc++.h>
using namespace std;
 
// Function to check if a number is a perfect square
bool isPerfectSquare(int n) {
    int root = sqrt(n);
    return (root * root == n);
}
 
// Function to convert decimal to any base
int decimalToBase(int n, int base) {
    int num = 0, count = 0;
    while (n != 0) {
        int rem = n % base;
        num += rem * pow(10, count);
        count++;
        n /= base;
    }
    return num;
}
 
// Function to find the largest N digit perfect square in base B
int largestNDigitPerfectSquare(int N, int B) {
    // Largest N digit number in base B is (B^N)-1
    int largestNum = pow(B, N) - 1;
 
    // Check perfect squares starting from the largest number
    for (int i = largestNum; i >= 0; i--) {
        int decimalNum = 0, count = 0;
        int num = i;
        while (num != 0) {
            int rem = num % 10;
            decimalNum += rem * pow(B, count);
            count++;
            num /= 10;
        }
        if (isPerfectSquare(decimalNum)) {
            return decimalToBase(decimalNum, B);
        }
    }
 
    // If no perfect square is found, return -1
    return -1;
}
 
int main() {
    int N = 2, B = 10;
    int largestPerfectSquare = largestNDigitPerfectSquare(N, B);
    cout << largestPerfectSquare << endl;
 
    return 0;
}

                    

Java

import java.util.*;
 
public class Main {
    // Function to check if a number is a perfect square
    static boolean isPerfectSquare(int n) {
        int root = (int)Math.sqrt(n);
        return (root * root == n);
    }
 
    // Function to convert decimal to any base
    static int decimalToBase(int n, int base) {
        int num = 0, count = 0;
        while (n != 0) {
            int rem = n % base;
            num += rem * (int)Math.pow(10, count);
            count++;
            n /= base;
        }
        return num;
    }
 
    // Function to find the largest N digit perfect square in base B
    static int largestNDigitPerfectSquare(int N, int B) {
        // Largest N digit number in base B is (B^N)-1
        int largestNum = (int)Math.pow(B, N) - 1;
 
        // Check perfect squares starting from the largest number
        for (int i = largestNum; i >= 0; i--) {
            int decimalNum = 0, count = 0;
            int num = i;
            while (num != 0) {
                int rem = num % 10;
                decimalNum += rem * (int)Math.pow(B, count);
                count++;
                num /= 10;
            }
            if (isPerfectSquare(decimalNum)) {
                return decimalToBase(decimalNum, B);
            }
        }
 
        // If no perfect square is found, return -1
        return -1;
    }
 
    public static void main(String[] args) {
        int N = 2, B = 10;
        int largestPerfectSquare = largestNDigitPerfectSquare(N, B);
        System.out.println(largestPerfectSquare);
    }
}
// This code is contributed by Prajwal Kandekar

                    

Python3

import math
 
# Function to check if a number is a perfect square
def is_perfect_square(n):
    root = int(math.sqrt(n))
    return root * root == n
 
# Function to convert decimal to any base
def decimal_to_base(n, base):
    num, count = 0, 0
    while n != 0:
        rem = n % base
        num += rem * (10 ** count)
        count += 1
        n //= base
    return num
 
# Function to find the largest N digit perfect square in base B
def largest_n_digit_perfect_square(N, B):
    # Largest N digit number in base B is (B^N)-1
    largest_num = B ** N - 1
 
    # Check perfect squares starting from the largest number
    for i in range(largest_num, -1, -1):
        decimal_num, count = 0, 0
        num = i
        while num != 0:
            rem = num % 10
            decimal_num += rem * (B ** count)
            count += 1
            num //= 10
        if is_perfect_square(decimal_num):
            return decimal_to_base(decimal_num, B)
 
    # If no perfect square is found, return -1
    return -1
 
if __name__ == '__main__':
    N, B = 2, 10
    largest_perfect_square = largest_n_digit_perfect_square(N, B)
    print(largest_perfect_square)

                    

Javascript

// Function to check if a number is a perfect square
function isPerfectSquare(n) {
  let root = Math.floor(Math.sqrt(n));
  return root * root === n;
}
 
// Function to convert decimal to any base
function decimalToBase(n, base) {
  let num = 0, count = 0;
  while (n !== 0) {
    let rem = n % base;
    num += rem * Math.pow(10, count);
    count++;
    n = Math.floor(n / base);
  }
  return num;
}
 
// Function to find the largest N digit perfect square in base B
function largestNDigitPerfectSquare(N, B) {
  // Largest N digit number in base B is (B^N)-1
  let largestNum = Math.pow(B, N) - 1;
 
  // Check perfect squares starting from the largest number
  for (let i = largestNum; i >= 0; i--) {
    let decimalNum = 0, count = 0, num = i;
    while (num !== 0) {
      let rem = num % 10;
      decimalNum += rem * Math.pow(B, count);
      count++;
      num = Math.floor(num / 10);
    }
    if (isPerfectSquare(decimalNum)) {
      return decimalToBase(decimalNum, B);
    }
  }
 
  // If no perfect square is found, return -1
  return -1;
}
 
let N = 2, B = 10;
let largestPerfectSquare = largestNDigitPerfectSquare(N, B);
console.log(largestPerfectSquare);

                    

C#

using System;
 
class Program {
  // Function to check if a number is a perfect square
  static bool isPerfectSquare(int n) {
    int root = (int) Math.Sqrt(n);
    return (root * root == n);
  }
 
  // Function to convert decimal to any base
  static int decimalToBase(int n, int b) {
    int num = 0, count = 0;
    while (n != 0) {
      int rem = n % b;
      num += rem * (int) Math.Pow(10, count);
      count++;
      n /= b;
    }
    return num;
  }
 
  // Function to find the largest N digit perfect square in base B
  static int largestNDigitPerfectSquare(int N, int B) {
    // Largest N digit number in base B is (B^N)-1
    int largestNum = (int) Math.Pow(B, N) - 1;
 
    // Check perfect squares starting from the largest number
    for (int i = largestNum; i >= 0; i--) {
      int decimalNum = 0, count = 0;
      int num = i;
      while (num != 0) {
        int rem = num % 10;
        decimalNum += rem * (int) Math.Pow(B, count);
        count++;
        num /= 10;
      }
      if (isPerfectSquare(decimalNum)) {
        return decimalToBase(decimalNum, B);
      }
    }
 
    // If no perfect square is found, return -1
    return -1;
  }
 
  static void Main(string[] args) {
    int N = 2, B = 10;
    int largestPerfectSquare = largestNDigitPerfectSquare(N, B);
    Console.WriteLine(largestPerfectSquare);
  }
}

                    

Output
81

Time Complexity: O(B^N), where B and N are given in the problem statement
Space Complexity: O(1)

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

                    

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>

                    

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

                    

Output: 
4

 

Time Complexity: O(logn)
Auxiliary Space: O(1)



Last Updated : 03 Apr, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads