📌  相关文章
📜  打印N个数字,以使其产品成为完美的多维数据集

📅  最后修改于: 2021-04-29 10:11:39             🧑  作者: Mango

给定数字N ,任务是找到不同的N个数字,以使它们的乘积是一个理想的立方体。
例子:

方法:解决方案基于以下事实:

因此,将输出前N个自然数的Perfect Cube。
例如:

For N = 1 => [1]
    Product is 1
    and cube root of 1 is also 1

For N = 2 => [1, 8]
    Product is 8
    and cube root of 8 is 2

For N = 3 => [1, 8, 27]
    Product is 216
    and cube root of 216 is 6

and so on

下面是上述方法的实现:

C++
// C++ program to find N numbers such that
// their product is a perfect cube
 
#include 
using namespace std;
 
// Function to find the N numbers such
//that their product is a perfect cube
void findNumbers(int N)
{
    int i = 1;
 
    // Loop to traverse each
//number from 1 to N
    while (i <= N) {
 
// Print the cube of i
//as the ith term of the output
        cout << (i * i * i)
             << " ";
 
        i++;
    }
}
 
// Driver Code
int main()
{
    int N = 4;
 
    // Function Call
    findNumbers(N);
}


Java
// Java program to find N numbers such that
// their product is a perfect cube
import java.util.*;
 
class GFG
{
     
// Function to find the N numbers such
//that their product is a perfect cube
static void findNumbers(int N)
{
    int i = 1;
 
    // Loop to traverse each
    //number from 1 to N
    while (i <= N) {
 
        // Print the cube of i
        //as the ith term of the output
        System.out.print( (i * i * i)
            + " ");
 
        i++;
    }
}
 
// Driver Code
public static void main (String []args)
{
    int N = 4;
 
    // Function Call
    findNumbers(N);
}
}
 
// This code is contributed by chitranayal


Python3
# Python3 program to find N numbers such that
# their product is a perfect cube
 
# Function to find the N numbers such
# that their product is a perfect cube
def findNumbers(N):
    i = 1
 
    # Loop to traverse each
    # number from 1 to N
    while (i <= N):
     
        # Print the cube of i
        # as the ith term of the output
        print((i * i * i), end=" ")
 
        i += 1
 
# Driver Code
if __name__ == '__main__':
    N = 4
 
    # Function Call
    findNumbers(N)
 
# This code is contributed by mohit kumar 29


C#
// C# program to find N numbers such that
// their product is a perfect cube
using System;
 
class GFG
{
     
// Function to find the N numbers such
//that their product is a perfect cube
static void findNumbers(int N)
{
    int i = 1;
 
    // Loop to traverse each
    //number from 1 to N
    while (i <= N) {
 
        // Print the cube of i
        //as the ith term of the output
        Console.Write( (i * i * i)
            + " ");
 
        i++;
    }
}
 
// Driver Code
public static void Main (string []args)
{
    int N = 4;
 
    // Function Call
    findNumbers(N);
}
}
 
// This code is contributed by Yash_R


Javascript


输出:
1 8 27 64

性能分析:

  • 时间复杂度:与上述方法一样,我们正在找到N个数字的完美立方体,因此将花费O(N)个时间。
  • 辅助空间复杂度:和上面的方法一样,没有使用额外的空间。因此辅助空间复杂度将为O(1)