# Sum of first K numbers which are not divisible by N

Given two numbers** N** and **K**, the task is to find the sum of first **K** numbers which are not divisible by **N**.

**Examples:**

Input:N = 5, K = 10

Output:63

Explanation:Sum of { 1, 2, 3, 4, 6, 7, 8, 9, 11, 12 } is 63.

Input:N = 3, k = 13

Output:127

Explanation:Sum of { 1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19 } is 127.

**Approach:** In order to solve this problem, we ned to follow the following steps:

- Calculate the last multiple of
**N**by**(K / (N – 1)) * N** - Calculte
**K%(N – 1)**. If the remainder is 0,**(K / (N – 1)) * N – 1**gives the last value which is not divisible by N. Otherwise, we need to add the remainder to**(K / (N – 1)) * N**. - Calculate the sum up to the last value which is not divisible by N obtained in the above step.
- Calculate the sum of multiples of N and subtract from the sum calculated in the above step to get the desired result.

Below code is the implementation of the above approach:

## C++

`// C++ Program to calculate ` `// the sum of first K ` `// numbers not divisible by N ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the sum ` `int` `findSum(` `int` `n, ` `int` `k) ` `{ ` ` ` `// Find the last multiple of N ` ` ` `int` `val = (k / (n - 1)) * n; ` ` ` ` ` `int` `rem = k % (n - 1); ` ` ` ` ` `// Find the K-th non-multiple of N ` ` ` `if` `(rem == 0) { ` ` ` `val = val - 1; ` ` ` `} ` ` ` `else` `{ ` ` ` `val = val + rem; ` ` ` `} ` ` ` ` ` `// Calculate the sum of ` ` ` `// all elements from 1 to val ` ` ` `int` `sum = (val * (val + 1)) / 2; ` ` ` ` ` `// Calculate the sum of ` ` ` `// all multiples of N ` ` ` `// between 1 to val ` ` ` `int` `x = k / (n - 1); ` ` ` `int` `sum_of_multiples ` ` ` `= (x ` ` ` `* (x + 1) * n) ` ` ` `/ 2; ` ` ` `sum -= sum_of_multiples; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 7, k = 13; ` ` ` `cout << findSum(n, k) ` ` ` `<< endl; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to calculate ` `// the sum of first K numbers ` `// not divisible by N ` `import` `java.util.*; ` ` ` `class` `GFG { ` ` ` `// Function to find the sum ` `static` `int` `findSum(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Find the last multiple of N ` ` ` `int` `val = (k / (n - ` `1` `)) * n; ` ` ` ` ` `int` `rem = k % (n - ` `1` `); ` ` ` ` ` `// Find the K-th non-multiple of N ` ` ` `if` `(rem == ` `0` `) ` ` ` `{ ` ` ` `val = val - ` `1` `; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `val = val + rem; ` ` ` `} ` ` ` ` ` `// Calculate the sum of ` ` ` `// all elements from 1 to val ` ` ` `int` `sum = (val * (val + ` `1` `)) / ` `2` `; ` ` ` ` ` `// Calculate the sum of ` ` ` `// all multiples of N ` ` ` `// between 1 to val ` ` ` `int` `x = k / (n - ` `1` `); ` ` ` `int` `sum_of_multiples = (x * (x + ` `1` `) * n) / ` `2` `; ` ` ` `sum -= sum_of_multiples; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `7` `, k = ` `13` `; ` ` ` ` ` `System.out.println(findSum(n, k)); ` `} ` `} ` ` ` `// This code is contributed by offbeat ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 Program to calculate ` `# the sum of first K ` `# numbers not divisible by N ` ` ` `# Function to find the sum ` `def` `findSum(n, k): ` ` ` ` ` `# Find the last multiple of N ` ` ` `val ` `=` `(k ` `/` `/` `(n ` `-` `1` `)) ` `*` `n; ` ` ` ` ` `rem ` `=` `k ` `%` `(n ` `-` `1` `); ` ` ` ` ` `# Find the K-th non-multiple of N ` ` ` `if` `(rem ` `=` `=` `0` `): ` ` ` `val ` `=` `val ` `-` `1` `; ` ` ` ` ` `else` `: ` ` ` `val ` `=` `val ` `+` `rem; ` ` ` ` ` `# Calculate the sum of ` ` ` `# all elements from 1 to val ` ` ` `sum` `=` `(val ` `*` `(val ` `+` `1` `)) ` `/` `/` `2` `; ` ` ` ` ` `# Calculate the sum of ` ` ` `# all multiples of N ` ` ` `# between 1 to val ` ` ` `x ` `=` `k ` `/` `/` `(n ` `-` `1` `); ` ` ` `sum_of_multiples ` `=` `(x ` `*` `(x ` `+` `1` `) ` `*` `n) ` `/` `/` `2` `; ` ` ` `sum` `-` `=` `sum_of_multiples; ` ` ` ` ` `return` `sum` `; ` ` ` `# Driver code ` `n ` `=` `7` `; k ` `=` `13` `; ` `print` `(findSum(n, k)) ` ` ` `# This code is contributed by Code_Mech ` |

*chevron_right*

*filter_none*

## C#

`// C# program to calculate ` `// the sum of first K numbers ` `// not divisible by N ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to find the sum ` `static` `int` `findSum(` `int` `n, ` `int` `k) ` `{ ` ` ` ` ` `// Find the last multiple of N ` ` ` `int` `val = (k / (n - 1)) * n; ` ` ` ` ` `int` `rem = k % (n - 1); ` ` ` ` ` `// Find the K-th non-multiple of N ` ` ` `if` `(rem == 0) ` ` ` `{ ` ` ` `val = val - 1; ` ` ` `} ` ` ` `else` ` ` `{ ` ` ` `val = val + rem; ` ` ` `} ` ` ` ` ` `// Calculate the sum of ` ` ` `// all elements from 1 to val ` ` ` `int` `sum = (val * (val + 1)) / 2; ` ` ` ` ` `// Calculate the sum of ` ` ` `// all multiples of N ` ` ` `// between 1 to val ` ` ` `int` `x = k / (n - 1); ` ` ` `int` `sum_of_multiples = (x * (x + 1) * n) / 2; ` ` ` `sum -= sum_of_multiples; ` ` ` ` ` `return` `sum; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `n = 7, k = 13; ` ` ` ` ` `Console.WriteLine(findSum(n, k)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

99

**Time Complexity:** *O(1)*

**Auxilary Space Complexity:** *O(1)*

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count numbers which are divisible by all the numbers from 2 to 10
- Sum of numbers from 1 to N which are divisible by 3 or 4
- Divisible by 37 for large numbers
- Sum of first N natural numbers which are divisible by 2 and 7
- Sum of first N natural numbers which are divisible by X or Y
- Sum of the numbers upto N that are divisible by 2 or 5
- Sum of all numbers divisible by 6 in a given range
- Sum of all numbers in the given range which are divisible by M
- Count numbers in range 1 to N which are divisible by X but not by Y
- Numbers that are not divisible by any number in the range [2, 10]
- Split a number as sum of K numbers which are not divisible by K
- Smallest number divisible by first n numbers
- Print k numbers where all pairs are divisible by m
- Count the numbers divisible by 'M' in a given range
- Sum of n digit numbers divisible by a given number
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count natural numbers whose factorials are divisible by x but not y
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Prove that atleast one of three consecutive even numbers is divisible by 6
- Number of pairs from the first N natural numbers whose sum is divisible by K

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.