# Sum of first N natural numbers which are divisible by X or Y

Given a number **N**. Given two numbers **X** and **Y**, the task is to find the sum of all those numbers from 1 to N that are divisible by X or by Y.

**Examples**:

Input: N = 20Output: 98Input: N = 14Output: 45

**Approach**: To solve the problem, follow the below steps:

->Find the sum of numbers that are divisible by X upto N. Denote it by S1.

->Find the sum of numbers that are divisible by Y upto N. Denote it by S2.

->Find the sum of numbers that are divisible by both X and Y (X*Y) upto N. Denote it by S3.

->The final answer will be **S1 + S2 – S3**.

In order to find the sum, we can use the general formula of A.P. which is:

Sn = (n/2) * {2*a + (n-1)*d}

**For S1**: The total numbers that will be divisible by X upto N will be N/X and the sum will be:

Hence, S1 = ((N/X)/2) * (2 * X + (N/X - 1) * X)

**For S2**: The total numbers that will be divisible by Y upto N will be N/Y and the sum will be:

Hence, S2 = ((N/Y)/2) * (2 * Y + (N/Y - 1) * Y)

**For S3**: The total numbers that will be divisible by both X and Y upto N will be N/(X*Y) and the sum will be:

Hence, S2 = ((N/(X*Y))/2) * (2 * Y + (N/(X*Y) - 1) * (X*Y))

Therefore, the result will be:

S = S1 + S2 - S3

Below is the implementation of the above approach:

## C++

`// C++ program to find sum of numbers from ` `// 1 to N which are divisible by X or Y ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate the sum ` `// of numbers divisible by X or Y ` `int` `sum(` `int` `N, ` `int` `X, ` `int` `Y) ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2; ` ` ` `S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2; ` ` ` `S3 = ((N / (X * Y))) * (2 * (X * Y) ` ` ` `+ (N / (X * Y) - 1) * (X * Y))/ 2; ` ` ` ` ` `return` `S1 + S2 - S3; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `N = 14; ` ` ` `int` `X = 3, Y = 5; ` ` ` ` ` `cout << sum(N, X, Y); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find sum of numbers from ` `// 1 to N which are divisible by X or Y ` ` ` `public` `class` `GFG{ ` ` ` ` ` `// Function to calculate the sum ` ` ` `// of numbers divisible by X or Y ` ` ` `static` `int` `sum(` `int` `N, ` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / X)) * (` `2` `* X + (N / X - ` `1` `) * X) / ` `2` `; ` ` ` `S2 = ((N / Y)) * (` `2` `* Y + (N / Y - ` `1` `) * Y) / ` `2` `; ` ` ` `S3 = ((N / (X * Y))) * (` `2` `* (X * Y) ` ` ` `+ (N / (X * Y) - ` `1` `) * (X * Y))/ ` `2` `; ` ` ` ` ` `return` `S1 + S2 - S3; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `int` `N = ` `14` `; ` ` ` `int` `X = ` `3` `, Y = ` `5` `; ` ` ` ` ` `System.out.println(sum(N, X, Y)); ` ` ` ` ` `} ` ` ` `// This code is contributed by Ryuga ` `} ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 program to find sum of numbers from ` `# 1 to N which are divisible by X or Y ` `from` `math ` `import` `ceil, floor ` ` ` `# Function to calculate the sum ` `# of numbers divisible by X or Y ` `def` `sum` `(N, X, Y): ` ` ` `S1 ` `=` `floor(floor(N ` `/` `X) ` `*` `floor(` `2` `*` `X ` `+` ` ` `floor(N ` `/` `X ` `-` `1` `) ` `*` `X) ` `/` `2` `) ` ` ` `S2 ` `=` `floor(floor(N ` `/` `Y)) ` `*` `floor(` `2` `*` `Y ` `+` ` ` `floor(N ` `/` `Y ` `-` `1` `) ` `*` `Y) ` `/` `2` ` ` `S3 ` `=` `floor(floor(N ` `/` `(X ` `*` `Y))) ` `*` `floor (` `2` `*` `(X ` `*` `Y) ` `+` ` ` `floor(N ` `/` `(X ` `*` `Y) ` `-` `1` `) ` `*` `(X ` `*` `Y))` `/` `2` ` ` ` ` `return` `S1 ` `+` `S2 ` `-` `S3 ` ` ` `# Driver code ` `if` `__name__ ` `=` `=` `'__main__'` `: ` ` ` `N ` `=` `14` ` ` `X ` `=` `3` ` ` `Y ` `=` `5` ` ` ` ` `print` `(` `int` `(` `sum` `(N, X, Y))) ` ` ` `# This code is contributed by ` `# Surendra_Gangwar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find sum of numbers from ` `// 1 to N which are divisible by X or Y ` ` ` `using` `System; ` `public` `class` `GFG{ ` ` ` ` ` `// Function to calculate the sum ` ` ` `// of numbers divisible by X or Y ` ` ` `static` `int` `sum(` `int` `N, ` `int` `X, ` `int` `Y) ` ` ` `{ ` ` ` `int` `S1, S2, S3; ` ` ` ` ` `S1 = ((N / X)) * (2 * X + (N / X - 1) * X) / 2; ` ` ` `S2 = ((N / Y)) * (2 * Y + (N / Y - 1) * Y) / 2; ` ` ` `S3 = ((N / (X * Y))) * (2 * (X * Y) ` ` ` `+ (N / (X * Y) - 1) * (X * Y))/ 2; ` ` ` ` ` `return` `S1 + S2 - S3; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `N = 14; ` ` ` `int` `X = 3, Y = 5; ` ` ` ` ` `Console.Write(sum(N, X, Y)); ` ` ` ` ` `} ` ` ` `} ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find sum of numbers from ` `// 1 to N which are divisible by X or Y ` `// Function to calculate the sum ` `// of numbers divisible by X or Y ` `function` `sum(` `$N` `, ` `$X` `, ` `$Y` `) ` `{ ` ` ` `$S1` `; ` `$S2` `; ` `$S3` `; ` ` ` ` ` `$S1` `= ` `floor` `(((int)` `$N` `/ ` `$X` `)) * (2 * ` `$X` `+ (int)((int)` `$N` `/ ` `$X` `- 1) * ` `$X` `) / 2; ` ` ` `$S2` `= ` `floor` `(((int)` `$N` `/ ` `$Y` `)) * (2 * ` `$Y` `+ (int)((int)` `$N` `/ ` `$Y` `- 1) * ` `$Y` `) / 2; ` ` ` `$S3` `= ` `floor` `(((int)` `$N` `/ (` `$X` `* ` `$Y` `))) * (2 * (` `$X` `* ` `$Y` `) ` ` ` `+ ((int)` `$N` `/ (` `$X` `* ` `$Y` `) - 1) * (int)(` `$X` `* ` `$Y` `))/ 2; ` ` ` ` ` `return` `ceil` `(` `$S1` `+ (` `$S2` `- ` `$S3` `)); ` `} ` ` ` `// Driver code ` ` ` `$N` `= 14; ` ` ` `$X` `= 3; ` ` ` `$Y` `= 5; ` ` ` ` ` `echo` `sum(` `$N` `, ` `$X` `, ` `$Y` `); ` ` ` `#This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

45

**Time Complexity :** O(1)

## Recommended Posts:

- Sum of first N natural numbers which are divisible by 2 and 7
- Check whether factorial of N is divisible by sum of first N natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Count natural numbers whose factorials are divisible by x but not y
- Check if product of first N natural numbers is divisible by their sum
- Check if factorial of N is divisible by the sum of squares of first N natural numbers
- Find the first natural number whose factorial is divisible by x
- LCM of First n Natural Numbers
- Natural Numbers
- Sum of sum of first n natural numbers
- Sum of all natural numbers in range L to R
- Sum of first N natural numbers which are not powers of K
- Sum of all odd natural numbers in range L and R
- Sum of squares of first n natural numbers
- Sum of cubes of first n 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 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.