- Related Questions & Answers
- Sum of first N natural numbers which are divisible by 2 and 7 in C++
- PHP program to find the sum of first n natural numbers that are divisible by a number ‘x’ or a number ‘y’
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Check if product of first N natural numbers is divisible by their sum in Python
- Sum of sum of first n natural numbers in C++
- Sum of square-sums of first n natural numbers
- Number of pairs from the first N natural numbers whose sum is divisible by K in C++
- Sum of first n natural numbers in C Program
- Sum of all subsets of a set formed by first n natural numbers
- C Program for cube sum of first n natural numbers?
- C++ Program for cube sum of first n natural numbers?
- Python Program for cube sum of first n natural numbers
- Java Program to cube sum of first n natural numbers
- Sum which is divisible by n in JavaScript
- C++ Program for Sum of squares of first n natural numbers?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Adding up all natural numbers up to n, that are divisible by X or Y is selecting all the numbers that are divisible by X or Y and adding them to a variable that stores the sum.

To find the sum of the first N natural numbers which are divisible by X or Y, there are two methods −

- Using loops and conditional statements
- Using formula

This method uses a loop that counts up to n numbers and selects numbers that are divisible by X or Y and adds them and save to a variables at each iteration.

#include <stdio.h> int main(void) { int n = 54; int x = 2 ; int y = 5; int sum = 0; for(int i = 0; i<= n; i++) { if(i%x == 0 || i% y == 0) sum = sum + i; } printf("sum of %d natural numbers divisible by %d and %d is %d" ,n,x,y,sum); return 0; }

sum of 54 natural numbers divisible by 2 and 5 is 881

This method uses the formula to find the sum of the first n number divisible by a number.

This can be found using the formula − SN/X = ((N/X)/2) * (2 * X + (N/X - 1) * X)

Using this formula the sum of n natural numbers divisible by x is found − S _{n/x} = ((n/x)/2) * (2 * x + (n/x - 1) * x)

Using this formula the sum of n natural numbers divisible by y is found − S _{n/y} = ((n/y)/2) * (2 * y + (n/y - 1) * y)

Now, Using this formula the sum of n natural numbers divisible by x and y is found : S _{n/x*y} = ((n/(x*y)/2) * (2 * (x*y) + (n/(x*y) - 1) * (x*y))

Now, we will add the sum of x and sum of y and subtract sum of x*y that are add two times.

#include <stdio.h> int main() { int n = 54; int x = 2, y = 5; int Sx, Sy, Sxy, sum; Sx = ((n / x)) * (2 * x + (n / x - 1) * x) / 2; Sy = ((n / y)) * (2 * y + (n / y - 1) * y) / 2; Sxy= ((n / (x * y))) * (2 * (x * y) + (n / (x * y) - 1) * (x * y))/ 2; sum = Sx + Sy - Sxy; printf("sum of %d natural numbers divisible by %d and %d is %d" ,n,x,y,sum); return 0; }

sum of 54 natural numbers divisible by 2 and 5 is 881

The second method is better because it does not use any loop that means better time complexity. But if the input cases are smaller than first one can also be used. But for large input cases the second method is not the best option.

Advertisements