Program to print the series 1, 9, 17, 33, 49, 73, 97... till N terms - GeeksforGeeks

24 Nov, 2021
Given a number N, the task is to print the first N terms of the series: 
Examples:  
Input: N = 7 Output: 1, 9, 17, 33, 49, 73, 97Input: N = 3 Output: 1, 9, 17  
 
Approach: From the given series, find the formula for Nth term:  
1st term = 1
2nd term = 9 = 2 * 4 + 1
3rd term = 17 = 2 * 9 - 1
4th term = 33 = 2 * 16 + 1
5th term = 49 = 2 * 25 - 1
6th term = 73 = 2 * 36 + 1
.
.
Nth term = (2 * N2 + (-1)N)
Therefore:  
Nth term of the series 
*** QuickLaTeX cannot compile formula:
 

*** Error message:
Error: Nothing to show, formula is empty

Then iterate over numbers in the range [1, N] to find all the terms using the above formula and print them.Below is the implementation of the above approach:  
CPP
Java
Python3
C#
Javascript
// C++ implementation of the above approach #include "bits/stdc++.h"using namespace std; // Function to print the seriesvoid printSeries(int N){     int ith_term = 0;     // Generate the ith term and    // print it    for (int i = 1; i <= N; i++) {         ith_term = i % 2 == 0                       ? 2 * i * i + 1                       : 2 * i * i - 1;        cout << ith_term << ", ";    }} // Driver Codeint main(){    int N = 7;     printSeries(N);    return 0;}
// Java implementation of the above approachimport java.util.*; class GFG{  // Function to print the seriesstatic void printSeries(int N){      int ith_term = 0;      // Generate the ith term and    // print it    for (int i = 1; i <= N; i++) {          ith_term = i % 2 == 0                       ? 2 * i * i + 1                       : 2 * i * i - 1;        System.out.print(ith_term+ ", ");    }}  // Driver Codepublic static void main(String[] args){    int N = 7;      printSeries(N);}} // This code is contributed by PrinciRaj1992
# Python implementation of the above approach # Function to print seriesdef printSeries(N):     ith_term = 0;     # Generate the ith term and    # print    for i in range(1,N+1):         ith_term = 0;        if(i % 2 == 0):            ith_term = 2 * i * i + 1;        else:            ith_term = 2 * i * i - 1;        print(ith_term,end= ", ");     # Driver Codeif __name__ == '__main__':    N = 7;     printSeries(N);     # This code is contributed by Princi Singh
// C# implementation of the above approachusing System; class GFG{ // Function to print the seriesstatic void printSeries(int N){     int ith_term = 0;     // Generate the ith term and    // print it    for (int i = 1; i <= N; i++) {         ith_term = i % 2 == 0? 2 * i * i + 1:                                2 * i * i - 1;        Console.Write(ith_term+ ", ");    }} // Driver Codepublic static void Main(){    int N = 7;     printSeries(N);}} // This code is contributed by AbhiThakur
<script>// javascript implementation of the above approach // Function to print the seriesfunction printSeries( N){    let ith_term = 0;     // Generate the ith term and    // print it    for (let i = 1; i <= N; i++)    {        ith_term = i % 2 == 0                       ? 2 * i * i + 1                       : 2 * i * i - 1;        document.write( ith_term + ", ");    }} // Driver Code    let N = 7;     printSeries(N);         // This code is contributed by gauravrajput1 </script>
1, 9, 17, 33, 49, 73, 97,
 
Time Complexity: O(N)
Auxiliary Space: O(1)
princiraj1992
abhaysingh290895
princi singh
GauravRajput1
sweetyty
khushboogoyal499
rishavmahato348
series
series-sum
Mathematical
School Programming
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to print prime numbers from 1 to N.
Segment Tree | Set 1 (Sum of given range)
Modular multiplicative inverse
Count all possible paths from top left to bottom right of a mXn matrix
Fizz Buzz Implementation
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
C++ Classes and Objects