# Sum of first n odd numbers in O(1) Complexity

Given the sequence of odd numbers

1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, ….

Find the sum of first n odd numbers

Examples:

Input : n = 2 Output : 4 Sum of first two odd numbers is 1 + 3 = 4. Input : 5 Output : 25 Sum of first 5 odd numbers is 1 + 3 + 5 + 7 + 9 = 25

A **simple solution **is to iterate through all odd numbers.

## C++

`// A naive CPP program to find sum of ` `// first n odd numbers ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns the sum of first ` `// n odd numbers ` `int` `oddSum(` `int` `n) ` `{ ` ` ` `int` `sum = 0, curr = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += curr; ` ` ` `curr += 2; ` ` ` `} ` ` ` `return` `sum; ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `n = 20; ` ` ` `cout << ` `" Sum of first "` `<< n ` ` ` `<< ` `" Odd Numbers is: "` `<< oddSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// first n odd numbers ` `import` `java.util.*; ` ` ` `class` `Odd ` `{ ` ` ` `// Returns the sum of first ` ` ` `// n odd numbers ` ` ` `public` `static` `int` `oddSum(` `int` `n) ` ` ` `{ ` ` ` `int` `sum = ` `0` `, curr = ` `1` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) { ` ` ` `sum += curr; ` ` ` `curr += ` `2` `; ` ` ` `} ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `20` `; ` ` ` `System.out.println(` `" Sum of first "` `+ n ` ` ` `+` `" Odd Numbers is: "` `+oddSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find sum ` `# of first n odd numbers ` ` ` `def` `oddSum(n) : ` ` ` `sum` `=` `0` ` ` `curr ` `=` `1` ` ` `i ` `=` `0` ` ` `while` `i < n: ` ` ` `sum` `=` `sum` `+` `curr ` ` ` `curr ` `=` `curr ` `+` `2` ` ` `i ` `=` `i ` `+` `1` ` ` `return` `sum` ` ` `# Driver Code ` `n ` `=` `20` `print` `(` `" Sum of first"` `, n, ` `"Odd Numbers is: "` `, ` ` ` `oddSum(n) ) ` ` ` `# This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// first n odd numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns the sum of first ` ` ` `// n odd numbers ` ` ` `public` `static` `int` `oddSum(` `int` `n) ` ` ` `{ ` ` ` `int` `sum = 0, curr = 1; ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `sum += curr; ` ` ` `curr += 2; ` ` ` `} ` ` ` ` ` `return` `sum; ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 20; ` ` ` `Console.WriteLine(` `" Sum of first "` `+ n ` ` ` `+ ` `" Odd Numbers is: "` `+ oddSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// A naive PHP program to find sum of ` `// first n odd numbers ` ` ` `// Returns the sum of first ` `// n odd numbers ` `function` `oddSum(` `$n` `) ` `{ ` ` ` `$sum` `= 0; ` `$curr` `= 1; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$n` `; ` `$i` `++) ` ` ` `{ ` ` ` `$sum` `+= ` `$curr` `; ` ` ` `$curr` `+= 2; ` ` ` `} ` ` ` `return` `$sum` `; ` `} ` ` ` `// Driver Code ` `$n` `= 20; ` `echo` `" Sum of first "` `, ` `$n` ` ` `, ` `" Odd Numbers is: "` `, oddSum(` `$n` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Sum of first 20 odd numbers is 400

Time Complexity: O(n)

Auxiliary Space : O(1)

An **efficient solution** is to use direct formula. To find the sum of first n odd numbers we can apply odd number theorem, it states that the sum of first n odd numbers is equal to the square of n.

∑(2i – 1) = n

^{2}where i varies from 1 to n

**let n = 10, therefore sum of first 10 odd numbers is**

1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 + 19 = 100

**if we apply odd number theorem:**

sum of first 10 odd numbers = n * n = 10 * 10 = 100.

Below is the implementation of the above approach:

## C++

`// Efficient program to find sum of ` `// first n odd numbers ` `#include <iostream> ` `using` `namespace` `std; ` ` ` `// Returns the sum of first ` `// n odd numbers ` `int` `oddSum(` `int` `n) ` `{ ` ` ` `return` `(n * n); ` `} ` ` ` `// Driver function ` `int` `main() ` `{ ` ` ` `int` `n = 20; ` ` ` `cout << ` `" Sum of first "` `<< n ` ` ` `<< ` `" Odd Numbers is: "` `<< oddSum(n); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of ` `// first n odd numbers ` `import` `java.util.*; ` ` ` `class` `Odd ` `{ ` ` ` `// Returns the sum of first ` ` ` `// n odd numbers ` ` ` `public` `static` `int` `oddSum(` `int` `n) ` ` ` `{ ` ` ` `return` `(n * n); ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `20` `; ` ` ` `System.out.println(` `" Sum of first "` `+ n ` ` ` `+` `" Odd Numbers is: "` `+oddSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find sum ` `# of first n odd numbers ` ` ` `def` `oddSum(n) : ` ` ` `return` `(n ` `*` `n); ` ` ` `# Driver Code ` `n ` `=` `20` `print` `(` `" Sum of first"` `, n, ` `"Odd Numbers is: "` `, ` ` ` `oddSum(n) ) ` ` ` `# This code is contributed by rishabh_jain ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of ` `// first n odd numbers ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `// Returns the sum of first ` ` ` `// n odd numbers ` ` ` `public` `static` `int` `oddSum(` `int` `n) ` ` ` `{ ` ` ` `return` `(n * n); ` ` ` `} ` ` ` ` ` `// driver function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 20; ` ` ` `Console.WriteLine(` `" Sum of first "` `+ n ` ` ` `+ ` `" Odd Numbers is: "` `+ oddSum(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by vt_m. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// Efficient program to find sum of ` `// first n odd numbers ` ` ` `// Returns the sum of first ` `// n odd numbers ` `function` `oddSum(` `$n` `) ` `{ ` ` ` `return` `(` `$n` `* ` `$n` `); ` `} ` ` ` `// Driver Code ` `$n` `= 20; ` `echo` `" Sum of first "` `, ` `$n` `, ` ` ` `" Odd Numbers is: "` `, oddSum(` `$n` `); ` ` ` `// This code is contributed by vt_m. ` `?> ` |

*chevron_right*

*filter_none*

Output:

Sum of first 20 odd numbers is 400

Time Complexity: O(1)

Auxiliary Space : O(1)

**How does it work?**

We can prove it using mathematical induction. We know it is true for n = 1 and n = 2 as sums are 1 and 4 (1 + 3) respectively.

Let it be true for n = k-1. Sum of first k odd numbers = Sum of first k-1 odd numbers + k'th odd number = (k-1)*(k-1) + (2k - 1) = k*k

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Sieve of Eratosthenes in 0(n) time complexity
- How is the time complexity of Sieve of Eratosthenes is n*log(log(n))?
- Complexity Analysis of Binary Search
- Check for balanced parentheses in an expression | O(1) space | O(N^2) time complexity
- Fill the missing numbers in the array of N natural numbers such that arr[i] not equal to i
- Permutation of numbers such that sum of two consecutive numbers is a perfect square
- Check if a given pair of Numbers are Betrothed numbers or not
- Numbers within a range that can be expressed as power of two numbers
- Numbers less than N which are product of exactly two distinct prime numbers
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Count numbers which are divisible by all the numbers from 2 to 10
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Count numbers which can be constructed using two numbers
- Absolute difference between the Product of Non-Prime numbers and Prime numbers of an Array
- Number of ways to obtain each numbers in range [1, b+c] by adding any two numbers in range [a, b] and [b, c]
- Absolute Difference between the Sum of Non-Prime numbers and Prime numbers of an Array
- Print numbers such that no two consecutive numbers are co-prime and every three consecutive numbers are co-prime
- D Numbers
- Self Numbers
- Sum of first n even numbers

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.