Find sum of modulo K of first N natural number
Given two integer N ans K, the task is to find sum of modulo K of first N natural numbers i.e 1%K + 2%K + ….. + N%K.
Examples :
Input : N = 10 and K = 2.
Output : 5
Sum = 1%2 + 2%2 + 3%2 + 4%2 + 5%2 + 6%2 +
7%2 + 8%2 + 9%2 + 10%2
= 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0 + 1 + 0
= 5.
Method 1:
Iterate a varible i from 1 to N, evaluate and add i%K.
Below is the implementation of this approach:
C++
// C++ program to find sum of // modulo K of first N natural numbers. #include <bits/stdc++.h> using namespace std; // Return sum of modulo K of // first N natural numbers. int findSum(int N, int K) { int ans = 0; // Iterate from 1 to N && // evaluating and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver Program int main() { int N = 10, K = 2; cout << findSum(N, K) << endl; return 0; } |
Java
// Java program to find sum of modulo // K of first N natural numbers. import java.io.*; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N, int K) { int ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver program static public void main(String[] args) { int N = 10, K = 2; System.out.println(findSum(N, K)); } } // This code is contributed by vt_m. |
Python3
# Python3 program to find sum # of modulo K of first N # natural numbers. # Return sum of modulo K of # first N natural numbers. def findSum(N, K): ans = 0; # Iterate from 1 to N && # evaluating and adding i % K. for i in range(1, N + 1): ans += (i % K); return ans; # Driver Code N = 10; K = 2; print(findSum(N, K)); # This code is contributed by mits |
C#
// C# program to find sum of modulo // K of first N natural numbers. using System; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N, int K) { int ans = 0; // Iterate from 1 to N && evaluating // and adding i % K. for (int i = 1; i <= N; i++) ans += (i % K); return ans; } // Driver program static public void Main() { int N = 10, K = 2; Console.WriteLine(findSum(N, K)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find sum // of modulo K of first N // natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum($N, $K) { $ans = 0; // Iterate from 1 to N && // evaluating and adding i % K. for ($i = 1; $i <= $N; $i++) $ans += ($i % $K); return $ans; } // Driver Code $N = 10; $K = 2; echo findSum($N, $K), "\n"; // This code is contributed by ajit ?> |
Output :
5
Time Complexity : O(N).
Method 2 :
Two cases arise in this method.
Case 1: When N < K, for each number i, N >= i >= 1, will give i as result when operate with modulo K. So, required sum will be sum of first N natural number, N*(N+1)/2.
Case 2: When N >= K, then integers from 1 to K in natural number sequence will produce, 1, 2, 3, ….., K – 1, 0 as result when operate with modulo K. Similarly, from K + 1 to 2K, it will produce same result. So, the idea is to count how many number of times this sequence appears and multiply it with sum of first K – 1 natural numbers.
Below is the implementation of this approach:
C++
// C++ program to find sum of modulo // K of first N natural numbers. #include <bits/stdc++.h> using namespace std; // Return sum of modulo K of // first N natural numbers. int findSum(int N, int K) { int ans = 0; // Counting the number of times 1, 2, .., // K-1, 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of // times 1, 2, .., K-1, 0 sequence occurs // and sum of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program int main() { int N = 10, K = 2; cout << findSum(N, K) << endl; return 0; } |
Java
// Java program to find sum of modulo // K of first N natural numbers. import java.io.*; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N, int K) { int ans = 0; // Counting the number of times 1, 2, .., // K-1, 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of times // 1, 2, .., K-1, 0 sequence occurs and sum // of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program static public void main(String[] args) { int N = 10, K = 2; System.out.println(findSum(N, K)); } } // This Code is contributed by vt_m. |
Python3
# Python3 program to find sum of modulo # K of first N natural numbers. # Return sum of modulo K of # first N natural numbers. def findSum(N, K): ans = 0; # Counting the number of times # 1, 2, .., K-1, 0 sequence occurs. y = N / K; # Finding the number of elements # left which are incomplete of # sequence Leads to Case 1 type. x = N % K; # adding multiplication of number # of times 1, 2, .., K-1, 0 # sequence occurs and sum of # first k natural number and # sequence from case 1. ans = ((K * (K - 1) / 2) * y + (x * (x + 1)) / 2); return int(ans); # Driver Code N = 10; K = 2; print(findSum(N, K)); # This code is contributed by mits |
C#
// C# program to find sum of modulo // K of first N natural numbers. using System; class GFG { // Return sum of modulo K of // first N natural numbers. static int findSum(int N, int K) { int ans = 0; // Counting the number of times 1, 2, .., // K-1, 0 sequence occurs. int y = N / K; // Finding the number of elements left which // are incomplete of sequence Leads to Case 1 type. int x = N % K; // adding multiplication of number of times // 1, 2, .., K-1, 0 sequence occurs and sum // of first k natural number and sequence // from case 1. ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2; return ans; } // Driver program static public void Main() { int N = 10, K = 2; Console.WriteLine(findSum(N, K)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find sum of modulo // K of first N natural numbers. // Return sum of modulo K of // first N natural numbers. function findSum($N, $K) { $ans = 0; // Counting the number of times // 1, 2, .., K-1, 0 sequence occurs. $y = $N / $K; // Finding the number of elements // left which are incomplete of // sequence Leads to Case 1 type. $x = $N % $K; // adding multiplication of number // of times 1, 2, .., K-1, 0 // sequence occurs and sum of // first k natural number and // sequence from case 1. $ans = ($K * ($K - 1) / 2) * $y + ($x * ($x + 1)) / 2; return $ans; } // Driver program $N = 10; $K = 2; echo findSum($N, $K) ; // This code is contributed by anuj_67. ?> |
Output :
5
Time Complexity : O(1).
This article is contributed by Anuj Chauhan. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Expressing a fraction as a natural number under modulo 'm'
- Sum of the natural numbers (up to N) whose modulo with K yield R
- Find the number of primitive roots modulo prime
- Find if given number is sum of first n natural numbers
- Find all divisors of a natural number | Set 2
- Find all divisors of a natural number | Set 1
- Find the k-th smallest divisor of a natural number N
- Find the first natural number whose factorial is divisible by x
- Find the number of sub arrays in the permutation of first N natural numbers such that their median is M
- Program to find remainder without using modulo or % operator
- Find Square Root under Modulo p | Set 1 (When p is in form of 4*i + 3)
- Find sum of divisors of all the divisors of a natural number
- Discrete logarithm (Find an integer k such that a^k is congruent modulo b)
- Find Square Root under Modulo p | Set 2 (Shanks Tonelli algorithm)
- Primitive root of a prime number n modulo n



