📜  先前的理想平方和立方数小于N

📅  最后修改于: 2021-05-07 09:46:32             🧑  作者: Mango

给定整数N ,任务是找到小于数字N的先前的完美正方形或完美立方体。
例子

方法:小于N的先前的完美平方数可以计算如下:

  • 找出给定数字N的平方根。
  • 使用相应语言的下限函数计算其下限值。
  • 如果N已经是一个完美的平方,则从中减去1。
  • 打印该数字的正方形。

小于N的先前的完美立方体数可以计算如下:

  • 找到给定N的立方根。
  • 使用相应语言的下限函数计算其下限值。
  • 如果N已经是一个完美的立方体,则从中减去1。
  • 打印该数字的多维数据集。

下面是上述方法的实现:

C++
// C++ implementation to find the
// previous perfect square and cube
// smaller than the given number
 
#include 
#include 
 
using namespace std;
 
// Function to find the previous
// perfect square of the number N
int previousPerfectSquare(int N)
{
    int prevN = floor(sqrt(N));
     
    // If N is alreay a perfect square
    // decrease prevN by 1.
    if (prevN * prevN == N)
        prevN -= 1;
 
    return prevN * prevN;
}
 
// Function to find the
// previous perfect cube
int previousPerfectCube(int N)
{
    int prevN = floor(cbrt(N));
     
    // If N is alreay a perfect cube
    // decrease prevN by 1.
    if (prevN * prevN * prevN == N)
        prevN -= 1;
         
    return prevN * prevN * prevN;
}
 
// Driver Code
int main()
{
    int n = 30;
    cout << previousPerfectSquare(n) << "\n";
    cout << previousPerfectCube(n) << "\n";
    return 0;
}


Java
// Java implementation to find the
// previous perfect square and cube
// smaller than the given number
import java.util.*;
 
class GFG{
 
// Function to find the previous
// perfect square of the number N
static int previousPerfectSquare(int N)
{
    int prevN = (int)Math.floor(Math.sqrt(N));
     
    // If N is alreay a perfect square
    // decrease prevN by 1.
    if (prevN * prevN == N)
        prevN -= 1;
 
    return prevN * prevN;
}
 
// Function to find the
// previous perfect cube
static int previousPerfectCube(int N)
{
    int prevN = (int)Math.floor(Math.cbrt(N));
     
    // If N is alreay a perfect cube
    // decrease prevN by 1.
    if (prevN * prevN * prevN == N)
        prevN -= 1;
         
    return prevN * prevN * prevN;
}
 
// Driver Code
public static void main(String[] args)
{
    int n = 30;
    System.out.println(previousPerfectSquare(n));
    System.out.println(previousPerfectCube(n));
}
}
 
// This code is contributed by Rohit_ranjan


Python3
# Python3 implementation to find the
# previous perfect square and cube
# smaller than the given number
import math
import numpy as np
 
# Function to find the previous
# perfect square of the number N
def previousPerfectSquare(N):
 
    prevN = math.floor(math.sqrt(N));
     
    # If N is alreay a perfect square
    # decrease prevN by 1.
    if (prevN * prevN == N):
        prevN -= 1;
 
    return prevN * prevN;
 
# Function to find the
# previous perfect cube
def previousPerfectCube(N):
 
    prevN = math.floor(np.cbrt(N));
     
    # If N is alreay a perfect cube
    # decrease prevN by 1.
    if (prevN * prevN * prevN == N):
        prevN -= 1;
         
    return prevN * prevN * prevN;
 
# Driver Code
n = 30;
 
print(previousPerfectSquare(n));
print(previousPerfectCube(n));
 
# This code is contributed by Code_Mech


C#
// C# implementation to find the
// previous perfect square and cube
// smaller than the given number
using System;
 
class GFG{
 
// Function to find the previous
// perfect square of the number N
static int previousPerfectSquare(int N)
{
    int prevN = (int)Math.Floor(Math.Sqrt(N));
     
    // If N is alreay a perfect square
    // decrease prevN by 1.
    if (prevN * prevN == N)
        prevN -= 1;
 
    return prevN * prevN;
}
 
// Function to find the
// previous perfect cube
static int previousPerfectCube(int N)
{
    int prevN = (int)Math.Floor(Math.Cbrt(N));
     
    // If N is alreay a perfect cube
    // decrease prevN by 1.
    if (prevN * prevN * prevN == N)
        prevN -= 1;
         
    return prevN * prevN * prevN;
}
 
// Driver Code
public static void Main(String[] args)
{
    int n = 30;
     
    Console.WriteLine(previousPerfectSquare(n));
    Console.WriteLine(previousPerfectCube(n));
}
}
 
// This code is contributed by sapnasingh4991


Javascript


输出:
25
27

时间复杂度: O(sqrt(n))

辅助空间: O(1)