Mathematics
Number Theory
Divisibility
Summation
Algorithms

Find the sum of all numbers between 1 and N divisible by either x or y

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In mathematical problems and algorithmic challenges, it often becomes crucial to efficiently calculate sums involving specific properties, such as being divisible by given factors. One typical problem is to determine the sum of all numbers between 1 and N that are divisible by either x or y. This problem has both practical applications in areas like computer science for generating sequences and also provides a solid ground for understanding concepts such as inclusion-exclusion principle.

Approach

To solve the problem of finding the sum of all numbers divisible by either x or y, we can make use of the inclusion-exclusion principle, ensuring we do not double-count numbers divisible by both.

Steps:

  1. Find all numbers divisible by x: Compute the count as floor(N / x), which gives how many multiples of x appear in the range.
  2. Find all numbers divisible by y: Compute floor(N / y) to count the multiples of y.
  3. Sum multiples of x: Use the arithmetic series formula S_x = x * (k_x * (k_x + 1) / 2) where k_x = floor(N / x).
  4. Sum multiples of y: Apply the same logic with S_y = y * (k_y * (k_y + 1) / 2) and k_y = floor(N / y).
  5. Sum common multiples: Determine lcm(x, y) and compute S_{xy} = lcm(x, y) * (k_{xy} * (k_{xy} + 1) / 2) with k_{xy} = floor(N / lcm(x, y)).
  6. Apply inclusion-exclusion: Combine the partial sums with S_total = S_x + S_y - S_{xy} to avoid double counting.

Example

Consider N = 20, x = 3, and y = 5.

  1. Numbers divisible by x: k_x = floor(20 / 3) = 6, so S_x = 3 * (6 * (6 + 1) / 2) = 63.
  2. Numbers divisible by y: k_y = floor(20 / 5) = 4, so S_y = 5 * (4 * (4 + 1) / 2) = 50.
  3. Common multiples of x and y: lcm(3, 5) = 15, giving k_{xy} = floor(20 / 15) = 1, so S_{xy} = 15 * (1 * (1 + 1) / 2) = 15.
  4. Total using inclusion-exclusion: S_total = 63 + 50 - 15 = 98.

The sum of all numbers between 1 and 20 that are divisible by either 3 or 5 is therefore 98.

Key Points Summary

StepExpressionCalculation Result
Numbers divisible by xk_x = floor(N / x)6
Sum multiples of xS_x = x * (k_x * (k_x + 1) / 2)63
Numbers divisible by yk_y = floor(N / y)4
Sum multiples of yS_y = y * (k_y * (k_y + 1) / 2)50
Common multiplesk_{xy} = floor(N / lcm(x, y))1
Sum common multiplesS_{xy} = lcm(x, y) * (k_{xy} * (k_{xy} + 1) / 2)15
Total sum using inclusion-exclusionS_total = S_x + S_y - S_{xy}98

Conclusion

Calculating the sum of numbers between 1 and N divisible by either x or y involves effectively using arithmetic series formulas paired with the inclusion-exclusion principle to ensure that multiples are not double-counted. This method is both computationally efficient and conceptually clear for large ranges and diverse conditions.


Course illustration
Course illustration

All Rights Reserved.