# Sum of all i such that (2^i + 1) % 3 = 0 where i is in range [1, n]

Given an integer N, the task is to calculate the sum of all i from 1 to N such that (2i + 1) % 3 = 0.

Examples:

Input: N = 3
Output: 4
For i = 1, 21 + 1 = 3 is divisible by 3.
For i = 2, 22 + 1 = 5 which is not divisible by 3.
For i = 3, 23 + 1 = 9 is divisible by 3.
Hence, sum = 1 + 3 = 4 (for i = 1, 3)

Input: N = 13
Output: 49

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: If we observe carefully then i will always be an odd number i.e. 1, 3, 5, 7, …... We will use the formula for the sum of first n odd numbers which is n * n.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the required sum ` `int` `sumN(``int` `n) ` `{ ` ` `  `    ``// Total odd numbers from 1 to n ` `    ``n = (n + 1) / 2; ` ` `  `    ``// Sum of first n odd numbers ` `    ``return` `(n * n); ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``int` `n = 3; ` `    ``cout << sumN(n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `class` `GFG { ` ` `  `    ``// Function to return the required sum ` `    ``static` `int` `sum(``int` `n) ` `    ``{ ` ` `  `        ``// Total odd numbers from 1 to n ` `        ``n = (n + ``1``) / ``2``; ` ` `  `        ``// Sum of first n odd numbers ` `        ``return` `(n * n); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``3``; ` `        ``System.out.println(sum(n)); ` `    ``} ` `} `

## Python3

 `# Python3 implementation of the approach ` ` `  `# Function to return the required sum ` `def` `sumN(n): ` ` `  `    ``# Total odd numbers from 1 to n ` `    ``n ``=` `(n ``+` `1``) ``/``/` `2``; ` ` `  `    ``# Sum of first n odd numbers ` `    ``return` `(n ``*` `n); ` ` `  `# Driver code ` `n ``=` `3``; ` `print``(sumN(n)); ` ` `  `# This code is contributed by mits `

## C#

 `// C# implementation of the approach ` `using` `System; ` `public` `class` `GFG { ` ` `  `    ``// Function to return the required sum ` `    ``public` `static` `int` `sum(``int` `n) ` `    ``{ ` ` `  `        ``// Total odd numbers from 1 to n ` `        ``n = (n + 1) / 2; ` ` `  `        ``// Sum of first n odd numbers ` `        ``return` `(n * n); ` `    ``} ` ` `  `    ``// Driver code ` `    ``public` `static` `void` `Main(``string``[] args) ` `    ``{ ` `        ``int` `n = 3; ` `        ``Console.WriteLine(sum(n)); ` `    ``} ` `} ` ` `  `// This code is contributed by Shrikant13 `

## PHP

 ` `

Output:

```4
```

Time Complexity: O(1)

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : shrikanth13, Mithun Kumar

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.