The Wayback Machine - https://web.archive.org/web/20240621020746/https://www.geeksforgeeks.org/find-sum-modulo-k-first-n-natural-number/
Open In App

Find sum of modulo K of first N natural number

Last Updated : 12 Sep, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.
Recommended Practice

Method 1: 

Iterate a variable 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
?>


Javascript




<script>
 
// JavaScript 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)
{
    let ans = 0;
 
    // Iterate from 1 to N && evaluating
    // and adding i % K.
    for(let i = 1; i <= N; i++)
        ans += (i % K);
 
    return ans;
}
 
// Driver Code
let N = 10, K = 2;
 
document.write(findSum(N, K));
 
// This code is contributed by code_hunt
 
</script>


Output : 

5

Time Complexity : O(N).

Auxiliary Space: O(1)

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, the required sum will be the sum of the 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 numbers of times this sequence appears and multiply it with the 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.
?>


Javascript




<script>
 
// Javascript 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)
{
    let ans = 0;
 
    // Counting the number of times
    // 1, 2, .., K-1, 0 sequence occurs.
    let y = N / K;
 
    // Finding the number of elements
    // left which are incomplete of
    // sequence Leads to Case 1 type.
    let 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 code
let N = 10;
let K = 2;
 
document.write(findSum(N, K));
 
// This code is contributed by _saurabh_jaiswal
 
</script>


Output : 

5

Time Complexity : O(1).

Auxiliary Space: O(1)

 



Previous Article
Next Article

Similar Reads

Sum of the natural numbers (up to N) whose modulo with K yield R
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.Examples: Input: N = 20, K = 4, R = 3 Output: 55 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 = 55Input: N = 15, K = 13, R = 2 Output: 17 Ap
4 min read
Expressing a fraction as a natural number under modulo 'm'
Given two integers A and B where A is not divisible by B, the task is to express A / B as a natural number modulo m where m = 1000000007. Note: This representation is useful where we need to express Probability of an event, Area of Curves and polygons etc.Examples: Input: A = 2, B = 6 Output: 333333336Input: A = 4, B = 5 Output: 600000005 Approach:
12 min read
Check if a given number can be expressed as pair-sum of sum of first X natural numbers
Given an integer N, the task is to check if N is the sum of a pair of integers which can be expressed as the sum of first X natural numbers, where X can be any positive integer. If satisfies the required condition. Print “YES”. Otherwise, print “NO”. Examples: Input: N = 25Output: YESExplanation:=&gt; 10 + 15 = 25Since 10 and 15 are the sum of firs
10 min read
Find if given number is sum of first n natural numbers
Given a number s (1 &lt;= s &lt;= 1000000000). If this number is the sum of first n natural number then print n, otherwise print -1. Examples: Input: s = 10Output: n = 4Explanation: 1 + 2 + 3 + 4 = 10 Input: s = 17Output: n = -1Explanation: 17 can't be expressed as a sum of first n natural numbers as sum of first 5 natural numbers is 15 and sum of
8 min read
Smallest number to be added in first Array modulo M to make frequencies of both Arrays equal
Given two arrays A[] and B[] consisting of N positive integers and an integer M, the task is to find the minimum value of X such that operation (A[i] + X) % M performed on every element of array A[] results in the formation of an array with frequency of elements same as that in another given array B[]. Examples: Input: N = 4, M = 3, A[] = {0, 0, 2,
8 min read
Sum of sum of all subsets of a set formed by first N natural numbers
Given N, and ff(N) = f(1) + f(2) + ...... + f(N), where f(k) is the sum of all subsets of a set formed by first k natural numbers. The task is to find ff(N) modulo 1000000007. Examples: Input: 2 Output: 7 f(1) + f(2) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 Input: 3 Output: 31 f(1) + f(2) + f(3) f(1) = 1 = 1 f(2) = 1 + 2 + {1 + 2} = 6 f(3) = 1 + 2 +
11 min read
Difference between Sum of Cubes and Sum of First N Natural Numbers
Given an integer N, find the absolute difference between sum of the cubes of first N natural numbers and the sum of first N natural numbers. Input: N = 3 Output: 30 Sum of first three numbers is 3 + 2 + 1 = 6 Sum of Cube of first three numbers is = 1 + 8 + 27 = 36 Absolute difference = 36 - 6 = 30 Input: N = 5 Output: 210 Approach: The sum of the c
4 min read
Sum of sum-series of first N Natural numbers
Given a natural number n, find the sum of the sum-series of the first N natural number. Sum-Series: is sum of first N natural numbers, i.e, sum-series of 5 is 15 ( 1 + 2 + 3 + 4 + 5 ). Natural number123456Sum of natural number (sum-series)136101521Sum of sum-series1410203556 Example: Input: N = 5 Output: 35 Explanation: Sum of sum-series of {1, 2,
6 min read
Difference between sum of the squares of first n natural numbers and square of sum
Given an integer n, find the absolute difference between sum of the squares of first n natural numbers and square of sum of first n natural numbers.Examples : Input : n = 3 Output : 22 Sum of first three numbers is 3 + 2 + 1 = 6 Square of the sum = 36 Sum of squares of first three is 9 + 4 + 1 = 14 Absolute difference = 36 - 14 = 22 Input : n = 10
7 min read
Program to find sum of first n natural numbers
Given a number n, find the sum of the first natural numbers. Examples : Input: n = 3 Output: 6 Explanation: Note that 1 + 2 + 3 = 6 Input : 5 Output : 15 Explanation : Note that 1 + 2 + 3 + 4 + 5 = 15Program to Find the Sum of First N Natural Numbers A simple solution is to do the following. 1) Initialize : sum = 0 2) Run a loop from x = 1 to n and
10 min read