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

The sum of first N natural numbers which are divisible by X or Y involves finding all numbers from 1 to N that are multiples of either X or Y, then adding them together. This is a common problem in competitive programming and mathematical computations.

Syntax

// Method 1: Loop approach
for(int i = 1; i <= n; i++) {
    if(i % x == 0 || i % y == 0)
        sum += i;
}

// Method 2: Formula approach  
sum = sumDivisibleBy(n, x) + sumDivisibleBy(n, y) - sumDivisibleBy(n, lcm(x, y))

There are two main approaches to solve this problem −

  • Method 1: Using loops and conditional statements
  • Method 2: Using mathematical formula (inclusion-exclusion principle)

Method 1: Using Loops and Conditional Statements

This method iterates through all numbers from 1 to N and checks if each number is divisible by X or Y −

#include <stdio.h>

int main() {
    int n = 54;
    int x = 2;
    int y = 5;
    int sum = 0;
    
    for(int i = 1; i <= n; i++) {
        if(i % x == 0 || i % y == 0) {
            sum += i;
        }
    }
    
    printf("Sum of first %d natural numbers divisible by %d or %d is %d<br>", n, x, y, sum);
    return 0;
}
Sum of first 54 natural numbers divisible by 2 or 5 is 881

Method 2: Using Mathematical Formula

This method uses the inclusion-exclusion principle. The formula for sum of first N natural numbers divisible by a number d is −

Sum = (count/2) × (first_term + last_term)

Where count = N/d, first_term = d, last_term = (N/d) × d

#include <stdio.h>

int gcd(int a, int b) {
    if (b == 0) return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) {
    return (a * b) / gcd(a, b);
}

int sumDivisible(int n, int d) {
    int count = n / d;
    return (count * (2 * d + (count - 1) * d)) / 2;
}

int main() {
    int n = 54;
    int x = 2, y = 5;
    
    int sumX = sumDivisible(n, x);
    int sumY = sumDivisible(n, y);
    int sumXY = sumDivisible(n, lcm(x, y));
    
    int totalSum = sumX + sumY - sumXY;
    
    printf("Sum of first %d natural numbers divisible by %d or %d is %d<br>", n, x, y, totalSum);
    return 0;
}
Sum of first 54 natural numbers divisible by 2 or 5 is 881

Comparison

Method Time Complexity Space Complexity Best Use Case
Loop Method O(N) O(1) Small values of N
Formula Method O(log min(X,Y)) O(1) Large values of N

Key Points

  • The formula method uses the inclusion-exclusion principle to avoid double counting numbers divisible by both X and Y.
  • LCM (Least Common Multiple) is used to find numbers divisible by both X and Y.
  • For large values of N, the formula method is significantly faster.

Conclusion

The formula-based approach using inclusion-exclusion principle is more efficient for large inputs with O(log min(X,Y)) complexity. For small datasets, the simple loop method is easier to understand and implement.

Updated on: 2026-03-15T10:48:30+05:30

774 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements