Given three integers N, K and R. The task is to calculate the sum of all those numbers from 1 to N which yields remainder R upon division by K.
Input: N = 20, K = 4, R = 3
3, 7, 11, 15 and 19 are the only numbers that give 3 as the remainder on division with 4.
3 + 7 + 11 + 15 + 19 = 55
Input: N = 15, K = 13, R = 2
- Initialize sum = 0 and take the modulo of each element from 1 to N with K.
- If the remainder is equal to R, then update sum = sum + i where i is the current number that gave R as the remainder on dividing by K.
- Print the value of sum in the end.
Below is the implementation of the above approach:
- Find sum of modulo K of first N natural number
- Expressing a fraction as a natural number under modulo 'm'
- LCM of N numbers modulo M
- Modulo power for large numbers represented as strings
- Natural Numbers
- LCM of First n Natural Numbers
- Sum of sum of first n natural numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of fifth powers of the first n natural numbers
- Sum of kth powers of first n natural numbers
- Sum of all natural numbers in range L to R
- Sum of first N natural numbers which are divisible by X or Y
- Average of first n even natural numbers
- Sum of all odd natural numbers in range L and R
- Sum of cubes of even and odd natural 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 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.