Skip to content
Related Articles

Related Articles

Improve Article

Largest N digit Octal number which is a Perfect square

  • Last Updated : 06 Apr, 2021

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

 

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: 
 

CPP




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




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




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




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

Javascript




<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>
Output: 
61

 

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :