📜  N位最大的八进制数,是一个完美的正方形

📅  最后修改于: 2021-04-22 09:12:50             🧑  作者: Mango

给定自然数N ,任务是找到最大N个数字的八进制数,它是一个完美的平方。
例子:

方法:
可以看出,在八进制数中也是最大的平方的最大数列是:

众所周知,当数字大于8 k时,八进制系统中的数字会增加,其中k表示数字中的数字。因此,对于八进制数系统中的任何N位数字,其值都必须小于8 N + 1的值。因此,可以使用此观察结果得出的一般术语是–

下面是上述方法的实现:

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


输出:
61