Skip to content

2894. Divisible and Non-divisible Sums Difference #1734

Answered by mah-shamim
mah-shamim asked this question in Q&A
Discussion options

You must be logged in to vote

We need to find the difference between the sum of integers in the range [1, n] that are not divisible by a given integer m and the sum of those that are divisible by m. The solution can be efficiently derived using arithmetic progression formulas.

Approach

  1. Total Sum Calculation: The sum of all integers from 1 to n can be calculated using the formula for the sum of an arithmetic series: total_sum = (n x (n + 1))/2.
  2. Sum of Divisible Numbers: The sum of integers divisible by m up to n forms an arithmetic sequence. The number of terms in this sequence is k = n/m. The sum of this sequence is given by sum2 = m x ((k x (k + 1))/2).
  3. Difference Calculation: The difference between the sum of non-d…

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@topugit
Comment options

topugit May 27, 2025
Collaborator

@mah-shamim
Comment options

mah-shamim May 27, 2025
Maintainer Author

Answer selected by topugit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested easy Difficulty
2 participants