Given a number **N**, the task is to find the **N** numbers such that their sum is a perfect cube.

**Examples:**

Input:N = 3

Output:1 7 19

Explanation:

Sum of numbers = 1 + 7 + 19 = 27,

which is the perfect cube of 3 => 3^{3}= 27

Input:N = 4

Output:1 7 19 37

Sum of numbers = 1 + 7 + 19 + 37 = 64,

which is the perfect cube of 4 => 4^{3}= 64

**Approach:**

Upon considering **Centered Hexagonal Numbers** which states that:

The sum of first N Centered Hexagonal Numbers is a perfect cube of N

So from Centered Hexagonal Numbers, the first N terms of the series will give the N numbers such that their sum is a perfect cube.

**For example:**

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

Therefore it can be said that printing the first N terms of the Centered Hexagonal Numbers will give the required N numbers.

Also, the Nth term of the Centered Hexagonal Numbers is:

**Algorithm:**

- Iterate a loop with a loop variable (say
**i**) from 1 to N and for each of the iteration –- Find the N
^{th}term of the centered hexagonal number using the formulae**3*i*(i-1) + 1**. - Append the i
^{th}term in an array.

- Find the N

Below is the implementation of the above approach:

## C++

`// C++ implementation to find the N ` `// numbers such that their ` `// sum is a perfect cube ` ` ` `#include <bits/stdc++.h> ` `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); ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

1 7 19 37

**Performance Analysis:**

**Time Complexity:**As in the above approach, we are finding all the N Centered hexagonal numbers, So it will take**O(N)**.**Auxiliary Space:**As in the above approach, there are no extra space used, So the Auxiliary Space used will be**O(1)**Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the

**DSA Self Paced Course**at a student-friendly price and become industry ready.## Recommended Posts:

- Print N numbers such that their product is a Perfect Cube
- Count numbers upto N which are both perfect square and perfect cube
- Print n numbers such that their sum is a perfect square
- Construct an Array such that cube sum of all element is a perfect square
- Percentage increase in volume of the cube if a side of cube is increased by a given percentage
- Check if number formed by joining two Numbers is Perfect Cube
- Count of pairs in an Array whose sum is a Perfect Cube
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Find smallest perfect square number A such that N + A is also a perfect square number
- Perfect cube greater than a given number
- Largest number in an array that is not a perfect cube
- Smallest perfect Cube divisible by all elements of an array
- Smallest perfect cube in an array
- Number of times the largest Perfect Cube can be subtracted from N
- Minimum divisor of a number to make the number perfect cube
- Least number to be added to or subtracted from N to make it a Perfect Cube
- Perfect Cube String

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.