Given an integer **N**, the task is to print a sequence of length **N** consisting of alternate odd and even numbers in increasing order such that the sum of any two consecutive terms is a perfect square.

**Examples:**

Input:N = 4Output:1 8 17 32Explanation:

1 + 8 = 9 = 3^{2}

8 + 17 = 25 = 5^{2}

17 + 32 = 49 = 7^{2}

Input:N = 2Output:1 8

**Approach:** The given problem can be solved based on the observation from the above examples, that for an integer **N**, sequence will be of the form **1, 8, 17, 32, 49 **and so on. Therefore, the **N ^{th} **term can be calculated by the following equation:

Therefore, to solve the problem, traverse the range **[1, N]** to calculate and print every term of the sequence using the above formula.

Below is the implementation of the above approach:

## C++

`// C++ Program to implement` `// the above approach` `#include <iostream>` `using` `namespace` `std;` `// Function to print the` `// required sequence` `void` `findNumbers(` `int` `n)` `{` ` ` `int` `i = 0;` ` ` `while` `(i <= n) {` ` ` `// Print ith odd number` ` ` `cout << 2 * i * i + 4 * i` ` ` `+ 1 + i % 2` ` ` `<< ` `" "` `;` ` ` `i++;` ` ` `}` `}` `// Driver Code` `int` `main()` `{` ` ` `int` `n = 6;` ` ` `findNumbers(n);` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to implement ` `// the above approach ` `import` `java.util.*;` `class` `GFG{` ` ` `// Function to print the ` `// required sequence ` `static` `void` `findNumbers(` `int` `n) ` `{ ` ` ` `int` `i = ` `0` `; ` ` ` `while` `(i <= n)` ` ` `{ ` ` ` `// Print ith odd number ` ` ` `System.out.print(` `2` `* i * i + ` `4` `* i + ` ` ` `1` `+ i % ` `2` `+ ` `" "` `); ` ` ` `i++; ` ` ` `} ` `} ` `// Driver code` `public` `static` `void` `main (String[] args)` `{` ` ` `int` `n = ` `6` `;` ` ` ` ` `// Function call` ` ` `findNumbers(n); ` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to implement` `# the above approach` ` ` `# Function to prthe` `# required sequence` `def` `findNumbers(n):` ` ` ` ` `i ` `=` `0` ` ` `while` `(i <` `=` `n):` ` ` ` ` `# Print ith odd number` ` ` `print` `(` `2` `*` `i ` `*` `i ` `+` `4` `*` `i ` `+` ` ` `1` `+` `i ` `%` `2` `, end ` `=` `" "` `)` ` ` ` ` `i ` `+` `=` `1` ` ` `# Driver Code` `n ` `=` `6` `findNumbers(n)` `# This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

## C#

`// C# program to implement ` `// the above approach ` `using` `System;` ` ` `class` `GFG{` ` ` `// Function to print the ` `// required sequence ` `static` `void` `findNumbers(` `int` `n) ` `{ ` ` ` `int` `i = 0; ` ` ` `while` `(i <= n)` ` ` `{ ` ` ` ` ` `// Print ith odd number ` ` ` `Console.Write(2 * i * i + 4 * i + ` ` ` `1 + i % 2 + ` `" "` `); ` ` ` `i++; ` ` ` `} ` `} ` ` ` `// Driver code` `public` `static` `void` `Main ()` `{` ` ` `int` `n = 6;` ` ` ` ` `// Function call` ` ` `findNumbers(n); ` `}` `}` ` ` `// This code is contributed by sanjoy_62` |

*chevron_right*

*filter_none*

**Output:**

1 8 17 32 49 72 97

**Time Complexity:** O(N)**Auxiliary Space:** 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:

- Check if a number is a perfect square having all its digits as a perfect square
- Generate a matrix having sum of secondary diagonal equal to a perfect square
- Find smallest perfect square number A such that N + A is also a perfect square number
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Smallest N digit number whose sum of square of digits is a Perfect Square
- Count numbers upto N which are both perfect square and perfect cube
- Count of pairs in an array whose sum is a perfect square
- Generate a Binary String without any consecutive 0's and at most K consecutive 1's
- Check if a number is perfect square without finding square root
- Count of subarrays having exactly K perfect square numbers
- Count of pairs in an array whose product is a perfect square
- Construct an Array of size N whose sum of cube of all elements is a perfect square
- Construct an Array such that cube sum of all element is a perfect square
- Sum of all perfect square divisors of numbers from 1 to N
- Count ways to generate pairs having Bitwise XOR and Bitwise AND equal to X and Y respectively
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Generate an alternate increasing and decreasing Array
- Sequence with sum K and minimum sum of absolute differences between consecutive elements
- Smallest perfect square divisible by all elements of an array
- Find all Factors of Large Perfect Square Natural Number in O(sqrt(sqrt(N))

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.