📌  相关文章
📜  检查数字是否可以表示为两个连续立方体的差

📅  最后修改于: 2021-04-27 23:38:31             🧑  作者: Mango

给定数字N ,任务是检查数字N是否可以表示为两个连续立方体的差。如果是,则打印这些数字,否则打印“否”

例子:

方法:该问题的主要观察结果是,当且仅当以下情况下,数字可以表示为两个连续立方体的差值:

因此,如果上述条件成立,那么我们将使用a循环打印数字,方法是检查i的值是否为(i + 1) 3 – i 3 = N并打印数字ii +1

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to print the two consecutive
// numbers whose difference is N
void print(int N)
{
    // Iterate in the range [0, 10^5]
    for (int i = 0; i < 100000; i++) {
 
        if (pow(i + 1, 3)
                - pow(i, 3)
            == N) {
 
            cout << i << ' ' << i + 1;
            return;
        }
    }
}
 
// Function to check if N is a
// perfect cube
bool isPerfectSquare(long double x)
{
    // Find floating point value of
    // square root of x.
    long double sr = sqrt(x);
 
    // If square root is an integer
    return ((sr - floor(sr)) == 0);
}
 
// Function to check whether a number
// can be represented as difference
// of two consecutive cubes
bool diffCube(int N)
{
    // Check if 12 * N - 3 is a
    // perfect square or not
    return isPerfectSquare(12 * N - 3);
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 19;
    if (diffCube(N)) {
        cout << "Yes\n";
        print(N);
    }
    else {
        cout << "No\n";
    }
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG{
   
// Function to print the two consecutive
// numbers whose difference is N
static void print(int N)
{
    // Iterate in the range [0, 10^5]
    for (int i = 0; i < 100000; i++)
    {
  
        if (Math.pow(i + 1, 3) - Math.pow(i, 3) == N)
        {
            int j = i + 1;
            System.out.println(i + " " + j);
            return;
        }
    }
}
  
// Function to check if N is a
// perfect cube
static boolean isPerfectSquare(double x)
{
    // Find floating point value of
    // square root of x.
    double sr = Math.sqrt(x);
  
    // If square root is an integer
    return ((sr - Math.floor(sr)) == 0);
}
  
// Function to check whether a number
// can be represented as difference
// of two consecutive cubes
static boolean diffCube(int N)
{
    // Check if 12 * N - 3 is a
    // perfect square or not
    return isPerfectSquare(12 * N - 3);
}
  
// Driver Code
public static void main(String[] args)
{
    // Given Number N
    int N = 19;
    if (diffCube(N))
    {
        System.out.println("Yes");
        print(N);
    }
    else
    {
        System.out.println("No");
    }
}
}
 
// This code is contributed by rock_cool


Python3
# Python3 program for the above approach
import math
 
# Function to print the two consecutive
# numbers whose difference is N
def printt(N):
     
    # Iterate in the range [0, 10^5]
    for i in range(100000):
        if (pow(i + 1, 3) - pow(i, 3) == N):
            print(i, '', i + 1)
            return
         
# Function to check if N is a
# perfect cube
def isPerfectSquare(x):
     
    # Find floating povalue of
    # square root of x.
    sr = math.sqrt(x)
 
    # If square root is an integer
    return ((sr - math.floor(sr)) == 0)
 
# Function to check whether a number
# can be represented as difference
# of two consecutive cubes
def diffCube(N):
     
    # Check if 12 * N - 3 is a
    # perfect square or not
    return isPerfectSquare(12 * N - 3)
 
# Driver Code
 
# Given number N
N = 19
 
if (diffCube(N)):
    print("Yes")
    printt(N)
     
else:
    print("No")
 
# This code is contributed by sanjoy_62


C#
// C# program for the above approach
using System;
class GFG{
   
// Function to print the two consecutive
// numbers whose difference is N
static void print(int N)
{
    // Iterate in the range [0, 10^5]
    for (int i = 0; i < 100000; i++)
    {
  
        if (Math.Pow(i + 1, 3) - Math.Pow(i, 3) == N)
        {
            int j = i + 1;
            Console.WriteLine(i + " " + j);
            return;
        }
    }
}
  
// Function to check if N is a
// perfect cube
static bool isPerfectSquare(double x)
{
    // Find floating point value of
    // square root of x.
    double sr = Math.Sqrt(x);
  
    // If square root is an integer
    return ((sr - Math.Floor(sr)) == 0);
}
  
// Function to check whether a number
// can be represented as difference
// of two consecutive cubes
static bool diffCube(int N)
{
    // Check if 12 * N - 3 is a
    // perfect square or not
    return isPerfectSquare(12 * N - 3);
}
  
// Driver Code
public static void Main(String[] args)
{
    // Given Number N
    int N = 19;
    if (diffCube(N))
    {
        Console.WriteLine("Yes");
        print(N);
    }
    else
    {
        Console.WriteLine("No");
    }
}
}
 
// This code is contributed by Rajput-Ji


Javascript


输出:
Yes
2 3

时间复杂度: O(N),其中N在10 5的范围内
辅助空间: O(1)