Given a natural number N, the task is to find the largest N digit Octal number which is a perfect square.
Examples:
Input: N = 1
Output: 4
Explanation:
4 is the largest 1 digit Octal number which is also perfect square
Input: N = 2
Output: 61
Explanation:
49 is the largest number which is a 2-Digit Octal Number and also a perfect square.
Therefore 49 in Octal = 61
Brute Force Approach:
Check each octal number starting from the largest one until it finds a perfect square. It uses the isPerfectSquare function to check if a number is a perfect square, and the decimalToOctal function to convert decimal numbers to octal.
#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 octal int decimalToOctal( int n) {
int octalNum = 0, count = 0;
while (n != 0) {
int rem = n % 8;
octalNum += rem * pow (10, count);
count++;
n /= 8;
}
return octalNum;
} int nDigitPerfectSquares( int n){
// Largest N digit octal number is 777...N times
int largestOctalNum = 0;
for ( int i = 1; i <= n; i++) {
largestOctalNum = largestOctalNum * 10 + 7;
}
// Check perfect squares starting from the largest octal number
for ( int i = largestOctalNum; i >= 0; i--) {
int decimalNum = 0, count = 0;
int num = i;
while (num != 0) {
int rem = num % 10;
decimalNum += rem * pow (8, count);
count++;
num /= 10;
}
if (isPerfectSquare(decimalNum)) {
cout << decimalToOctal(decimalNum) << endl;
break ;
}
}
} int main() {
int n = 2;
nDigitPerfectSquares(n);
return 0;
} |
import java.util.*;
public class Main {
// Function to check if a number is a perfect square
public static boolean isPerfectSquare( int n) {
int root = ( int )Math.sqrt(n);
return (root * root == n);
}
// Function to convert decimal to octal
public static int decimalToOctal( int n) {
int octalNum = 0 , count = 0 ;
while (n != 0 ) {
int rem = n % 8 ;
octalNum += rem * Math.pow( 10 , count);
count++;
n /= 8 ;
}
return octalNum;
}
public static void nDigitPerfectSquares( int n) {
// Largest N digit octal number is 777...N times
int largestOctalNum = 0 ;
for ( int i = 1 ; i <= n; i++) {
largestOctalNum = largestOctalNum * 10 + 7 ;
}
// Check perfect squares starting from the largest octal number
for ( int i = largestOctalNum; i >= 0 ; i--) {
int decimalNum = 0 , count = 0 ;
int num = i;
while (num != 0 ) {
int rem = num % 10 ;
decimalNum += rem * Math.pow( 8 , count);
count++;
num /= 10 ;
}
if (isPerfectSquare(decimalNum)) {
System.out.println(decimalToOctal(decimalNum));
break ;
}
}
}
public static void main(String[] args) {
int n = 2 ;
nDigitPerfectSquares(n);
}
} |
import math
# Function to check if a number is a perfect square def isPerfectSquare(n):
root = int (math.sqrt(n))
return (root * root = = n)
# Function to convert decimal to octal def decimalToOctal(n):
octalNum = 0
count = 0
while (n ! = 0 ):
rem = n % 8
octalNum + = rem * pow ( 10 , count)
count + = 1
n / / = 8
return octalNum
def nDigitPerfectSquares(n):
# Largest N digit octal number is 777...N times
largestOctalNum = 0
for i in range ( 1 , n + 1 ):
largestOctalNum = largestOctalNum * 10 + 7
# Check perfect squares starting from the largest octal number
for i in range (largestOctalNum, - 1 , - 1 ):
decimalNum = 0
count = 0
num = i
while (num ! = 0 ):
rem = num % 10
decimalNum + = rem * pow ( 8 , count)
count + = 1
num / / = 10
if (isPerfectSquare(decimalNum)):
print (decimalToOctal(decimalNum))
break
n = 2
nDigitPerfectSquares(n) # This code is contributed by Prajwal Kandekar |
using System;
class MainClass {
// Function to check if a number is a perfect square
public static bool IsPerfectSquare( int n)
{
int root = ( int )Math.Sqrt(n);
return (root * root == n);
}
// Function to convert decimal to octal
public static int DecimalToOctal( int n)
{
int octalNum = 0, count = 0;
while (n != 0) {
int rem = n % 8;
octalNum += rem * ( int )Math.Pow(10, count);
count++;
n /= 8;
}
return octalNum;
}
public static void NDigitPerfectSquares( int n)
{
// Largest N digit octal number is 777...N times
int largestOctalNum = 0;
for ( int i = 1; i <= n; i++) {
largestOctalNum = largestOctalNum * 10 + 7;
}
// Check perfect squares starting from the largest
// octal number
for ( int i = largestOctalNum; i >= 0; i--) {
int decimalNum = 0, count = 0;
int num = i;
while (num != 0) {
int rem = num % 10;
decimalNum += rem * ( int )Math.Pow(8, count);
count++;
num /= 10;
}
if (IsPerfectSquare(decimalNum)) {
Console.WriteLine(
DecimalToOctal(decimalNum));
break ;
}
}
}
public static void Main()
{
int n = 2;
NDigitPerfectSquares(n);
}
} |
// Function to check if a number is a perfect square function isPerfectSquare(n) {
const root = Math.sqrt(n);
return Math.floor(root) * Math.floor(root) === n;
} // Function to convert decimal to octal function decimalToOctal(n) {
let octalNum = 0;
let count = 0;
while (n !== 0) {
const rem = n % 8;
octalNum += rem * Math.pow(10, count);
count += 1;
n = Math.floor(n / 8);
}
return octalNum;
} function nDigitPerfectSquares(n) {
// Largest N digit octal number is 777...N times
let largestOctalNum = 0;
for (let i = 1; i <= n; i++) {
largestOctalNum = largestOctalNum * 10 + 7;
}
// Check perfect squares starting from the largest octal number
for (let i = largestOctalNum; i >= 0; i--) {
let decimalNum = 0;
let count = 0;
let num = i;
while (num !== 0) {
const rem = num % 10;
decimalNum += rem * Math.pow(8, count);
count += 1;
num = Math.floor(num / 10);
}
if (isPerfectSquare(decimalNum)) {
console.log(decimalToOctal(decimalNum));
break ;
}
}
} const n = 2; nDigitPerfectSquares(n); // This code is contributed by Samim Hossain Mondal |
61
Time Complexity: O(N^2*log(N)), where N is the input variable from the problem statement
Space Complexity: O(1)
Approach:
It can be observed that the series of largest numbers which is also a perfect square in Octal is:
4, 61, 744, 7601, 77771, 776001 …..
As we know the digits in the octal system increases when a number Greater than 8k where k denotes the number of digits in the number. So for any N digit number in the octal number system must be less than the value of 8N+1. So, the general term that can be derived using this observation is –
N-Digit Octal Number = octal(pow(ceil(sqrt(pow(8, N))) -1, 2))
Below is the implementation of the above approach:
// C++ implementation to find the maximum // N-digit octal number which is perfect square #include <bits/stdc++.h> using namespace std;
// Function to convert decimal number // to a octal number void decToOctal( int n)
{ // Array to store octal number
int octalNum[100];
// Counter for octal number array
int i = 0;
while (n != 0) {
// Store remainder in
// octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// Print octal number array
// in reverse order
for ( int j = i - 1; j >= 0; j--)
cout << octalNum[j];
cout << "\n" ;
} void nDigitPerfectSquares( int n)
{ // Largest n-digit perfect square
int decimal = pow (
ceil ( sqrt ( pow (8, n))) - 1, 2
);
decToOctal(decimal);
} // Driver Code int main()
{ int n = 2;
nDigitPerfectSquares(n);
return 0;
} |
// Java implementation to find the maximum // N-digit octal number which is perfect square import java.util.*;
import java.lang.*;
import java.io.*;
class GFG
{ // Function to convert decimal number
// to a octal number
static void decToOctal( int n)
{
// Array to store octal number
int octalNum[] = new int [ 100 ];
// Counter for octal number array
int i = 0 ;
while (n != 0 )
{
// Store remainder in
// octal array
octalNum[i] = n % 8 ;
n = n / 8 ;
i++;
}
// Print octal number array
// in reverse order
for ( int j = i - 1 ; j >= 0 ; j--)
System.out.print(octalNum[j]);
System.out.println( "\n" );
}
static void nDigitPerfectSquares( int n)
{
// Largest n-digit perfect square
int decimal = ( int ) Math.pow(Math.ceil(Math.sqrt(Math.pow( 8 , n))) - 1 , 2 );
decToOctal(decimal);
}
// Driver code
public static void main (String[] args)
{
int n = 2 ;
nDigitPerfectSquares(n);
}
} // This code is contributed by nidhiva |
# Python3 implementation to find the maximum # N-digit octal number which is perfect square from math import sqrt,ceil
# Function to convert decimal number # to a octal number def decToOctal(n) :
# Array to store octal number
octalNum = [ 0 ] * 100 ;
# Counter for octal number array
i = 0 ;
while (n ! = 0 ) :
# Store remainder in
# octal array
octalNum[i] = n % 8 ;
n = n / / 8 ;
i + = 1 ;
# Print octal number array
# in reverse order
for j in range (i - 1 , - 1 , - 1 ) :
print (octalNum[j], end = "");
print ();
def nDigitPerfectSquares(n) :
# Largest n-digit perfect square
decimal = pow (ceil(sqrt( pow ( 8 , n))) - 1 , 2 );
decToOctal(decimal);
# Driver Code if __name__ = = "__main__" :
n = 2 ;
nDigitPerfectSquares(n);
# This code is contributed by AnkitRai01 |
// C# implementation to find the maximum // N-digit octal number which is perfect square using System;
class GFG
{ // Function to convert decimal number
// to a octal number
static void decToOctal( int n)
{
// Array to store octal number
int []octalNum = new int [100];
// Counter for octal number array
int i = 0;
while (n != 0)
{
// Store remainder in
// octal array
octalNum[i] = n % 8;
n = n / 8;
i++;
}
// Print octal number array
// in reverse order
for ( int j = i - 1; j >= 0; j--)
Console.Write(octalNum[j]);
Console.WriteLine();
}
static void nDigitPerfectSquares( int n)
{
// Largest n-digit perfect square
int _decimal = ( int ) Math.Pow(Math.Ceiling(Math.Sqrt(Math.Pow(8, n))) - 1, 2);
decToOctal(_decimal);
}
// Driver code
public static void Main()
{
int n = 2;
nDigitPerfectSquares(n);
}
} // This code is contributed by AnkitRai01 |
<script> // Javascript implementation to find the maximum // N-digit octal number which is perfect square // Function to convert decimal number // to a octal number function decToOctal(n)
{ // Array to store octal number
var octalNum = Array(100).fill(0);
// Counter for octal number array
var i = 0;
while (n != 0) {
// Store remainder in
// octal array
octalNum[i] = n % 8;
n = parseInt(n / 8);
i++;
}
// Print octal number array
// in reverse order
for ( var j = i - 1; j >= 0; j--)
document.write(octalNum[j]);
document.write( "<br>" );
} function nDigitPerfectSquares(n)
{ // Largest n-digit perfect square
var decimal = Math.pow(
Math.ceil(Math.sqrt(Math.pow(8, n))) - 1, 2
);
decToOctal(decimal);
} // Driver Code var n = 2;
nDigitPerfectSquares(n); </script> |
61
Time Complexity: O(log8n)
Auxiliary Space: O(1)