# Largest N digit Octal number which is a Perfect square

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

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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 ` `using` `namespace` `std; ` ` `  `// Function to convert decimal number ` `// to a octal number ` `void` `decToOctal(``int` `n) ` `{ ` ` `  `    ``// Array to store octal number ` `    ``int` `octalNum; ` ` `  `    ``// 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``; ` `         `  `        ``// 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 `

Output:

```61
```

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.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : nidhiva, AnkitRai01