# Generate an alternate odd-even sequence having sum of all consecutive pairs as a perfect square

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);` `}` |

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

## Python3

`# Python3 program to implement` `# the above approach` ` ` `# Function to print the` `# 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` |

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

## Javascript

`<script>` `// Javascript Program to implement` `// the above approach` `// Function to print the` `// required sequence` `function` `findNumbers(n)` `{` ` ` `var` `i = 0;` ` ` `while` `(i <= n) {` ` ` `// Print ith odd number` ` ` `document.write(2 * i * i + 4 * i + 1 + i % 2+` `" "` `);` ` ` `i++;` ` ` `}` `}` `// Driver Code` `var` `n = 6;` `findNumbers(n);` `</script>` |

**Output:**

1 8 17 32 49 72 97

**Time Complexity:** O(N)**Auxiliary Space:** O(1)