Find sum of modulo K of first N natural number
Last Updated :
12 Sep, 2022
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 variable i from 1 to N, evaluate and add i%K.
Below is the implementation of this approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N, int K)
{
int ans = 0;
for ( int i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
int main()
{
int N = 10, K = 2;
cout << findSum(N, K) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSum( int N, int K)
{
int ans = 0 ;
for ( int i = 1 ; i <= N; i++)
ans += (i % K);
return ans;
}
static public void main(String[] args)
{
int N = 10 , K = 2 ;
System.out.println(findSum(N, K));
}
}
|
Python3
def findSum(N, K):
ans = 0 ;
for i in range ( 1 , N + 1 ):
ans + = (i % K);
return ans;
N = 10 ;
K = 2 ;
print (findSum(N, K));
|
C#
using System;
class GFG {
static int findSum( int N, int K)
{
int ans = 0;
for ( int i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
static public void Main()
{
int N = 10, K = 2;
Console.WriteLine(findSum(N, K));
}
}
|
PHP
<?php
function findSum( $N , $K )
{
$ans = 0;
for ( $i = 1; $i <= $N ; $i ++)
$ans += ( $i % $K );
return $ans ;
}
$N = 10; $K = 2;
echo findSum( $N , $K ), "\n" ;
?>
|
Javascript
<script>
function findSum(N, K)
{
let ans = 0;
for (let i = 1; i <= N; i++)
ans += (i % K);
return ans;
}
let N = 10, K = 2;
document.write(findSum(N, K));
</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++
#include <bits/stdc++.h>
using namespace std;
int findSum( int N, int K)
{
int ans = 0;
int y = N / K;
int x = N % K;
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
int main()
{
int N = 10, K = 2;
cout << findSum(N, K) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int findSum( int N, int K)
{
int ans = 0 ;
int y = N / K;
int x = N % K;
ans = (K * (K - 1 ) / 2 ) * y + (x * (x + 1 )) / 2 ;
return ans;
}
static public void main(String[] args)
{
int N = 10 , K = 2 ;
System.out.println(findSum(N, K));
}
}
|
Python3
def findSum(N, K):
ans = 0 ;
y = N / K;
x = N % K;
ans = ((K * (K - 1 ) / 2 ) * y +
(x * (x + 1 )) / 2 );
return int (ans);
N = 10 ;
K = 2 ;
print (findSum(N, K));
|
C#
using System;
class GFG {
static int findSum( int N, int K)
{
int ans = 0;
int y = N / K;
int x = N % K;
ans = (K * (K - 1) / 2) * y + (x * (x + 1)) / 2;
return ans;
}
static public void Main()
{
int N = 10, K = 2;
Console.WriteLine(findSum(N, K));
}
}
|
PHP
<?php
function findSum( $N , $K )
{
$ans = 0;
$y = $N / $K ;
$x = $N % $K ;
$ans = ( $K * ( $K - 1) / 2) * $y
+ ( $x * ( $x + 1)) / 2;
return $ans ;
}
$N = 10; $K = 2;
echo findSum( $N , $K ) ;
?>
|
Javascript
<script>
function findSum(N, K)
{
let ans = 0;
let y = N / K;
let x = N % K;
ans = (K * (K - 1) / 2) * y +
(x * (x + 1)) / 2;
return ans;
}
let N = 10;
let K = 2;
document.write(findSum(N, K));
</script>
|
Output :
5
Time Complexity : O(1).
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...