📌  相关文章
📜  打印N个数字,使它们的和成为一个完美的多维数据集

📅  最后修改于: 2021-05-04 19:24:57             🧑  作者: Mango

给定数字N ,任务是找到N个数字,以使它们的和成为一个完美的立方体。

例子:

方法:
考虑到中心六边形数字,该数字指出:

因此,从中心六边形数开始,该系列的前N个项将给出N个数字,以使它们的和成为一个完美的立方体。

例如:

For N = 1,
    Centered Hexagonal Series = 1
    and 13 = 1
    Hence, {1} is the required N number

For N = 2,
    Centered Hexagonal Series = 1, 7
    and 23 = 1 + 7 = 8
    Hence, {1, 7} are the required N number

For N = 3,
    Centered Hexagonal Series = 1, 7, 19
    and 33 = 1 + 7 + 19 = 27
    Hence, {1, 7, 19} are the required N number
.
.
and so on.

因此可以说,打印居中六边形数字的前N个术语将得到所需的N个数字。
同样,中心六边形数的第N个项是:
3 * N * (N - 1) + 1

算法:

  • 使用1到N的循环变量(例如i )迭代循环,并针对每次迭代–
    1. 使用公式3 * i *(i-1)+1找到中心六边形数的N项。
    2. 附加阵列中的i术语。

下面是上述方法的实现:

C++
// C++ implementation to find the N
// numbers such that their
// sum is a perfect cube
 
#include 
using namespace std;
 
// Function to find the N
// numbers such that their
// sum is a perfect cube
void findNumbers(int n)
{
    int i = 1;
    // Loop to find the Ith term
    // of the Centered Hexagonal number
    while (i <= n) {
        cout << (3 * i * (i - 1) + 1)
             << " ";
        i++;
    }
}
 
// Driver Code
int main()
{
    int n = 4;
 
    // Function Call
    findNumbers(n);
}


Java
// Java implementation to find the N
// numbers such that their
// sum is a perfect cube
class GFG
{
         
    // Function to find the N
    // numbers such that their
    // sum is a perfect cube
    static void findNumbers(int n)
    {
        int i = 1;
         
        // Loop to find the Ith term
        // of the Centered Hexagonal number
        while (i <= n)
        {
            System.out.print((3 * i * (i - 1) + 1) + " ");
            i++;
        }
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int n = 4;
     
        // Function Call
        findNumbers(n);
    }
}
 
// This code is contributed by AnkitRai01


C#
// C# implementation to find the N
// numbers such that their
// sum is a perfect cube
using System;
 
public class GFG
{
         
    // Function to find the N
    // numbers such that their
    // sum is a perfect cube
    static void findNumbers(int n)
    {
        int i = 1;
         
        // Loop to find the Ith term
        // of the Centered Hexagonal number
        while (i <= n)
        {
            Console.Write((3 * i * (i - 1) + 1) + " ");
            i++;
        }
    }
     
    // Driver Code
    public static void Main()
    {
        int n = 4;
     
        // Function Call
        findNumbers(n);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python3 implementation to find the N
# numbers such that their
# sum is a perfect cube
 
# Function to find the N
# numbers such that their
# sum is a perfect cube
def findNumbers(n):
    i = 1
 
    # Loop to find the Ith term
    # of the Centered Hexagonal number
    while (i <= n):
        print((3 * i * (i - 1) + 1), end=" ")
        i += 1
 
# Driver Code
n = 4
 
# Function Call
findNumbers(n)
 
# This code is contributed by mohit kumar 29


Javascript


输出:
1 7 19 37

性能分析:

  • 时间复杂度:与上述方法一样,我们找到了所有N个中心六边形数,因此它将采用O(N)
  • 辅助空间:与上述方法一样,没有使用额外的空间,因此使用的辅助空间将为O(1)