Given an integer N, the task is to calculate the sum of all i from 1 to N such that (2i + 1) % 3 = 0.
Input: N = 3
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
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:
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
- Sum of i * countDigits(i)^2 for all i in range [L, R]
- XOR of all the elements in the given range [L, R]
- GCD of elements in a given range
- Sum of elements of an AP in the given range
- Sum of all even numbers in range L and R
- Interquartile Range (IQR)
- Compute (a*b)%c such that (a%c) * (b%c) can be beyond range
- Sum of all natural numbers in range L to R
- Sum of all the prime numbers in a given range
- Sum of all odd natural numbers in range L and R
- Find XOR of numbers from the range [L, R]
- Sum of all numbers in the given range which are divisible by M
- Prime numbers in a given range using STL | Set 2
- Find the GCD that lies in given range
- Find Range Value of the Expression
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.