Teaching Kids Programming – Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math)


Teaching Kids Programming: Videos on Data Structures and Algorithms

You are given positive integers n and m.

Define two integers, num1 and num2, as follows:

  • num1: The sum of all integers in the range [1, n] that are not divisible by m.
  • num2: The sum of all integers in the range [1, n] that are divisible by m.

Return the integer num1 – num2.

Example 1:
Input: n = 10, m = 3
Output: 19
Explanation: In the given example:
– Integers in the range [1, 10] that are not divisible by 3 are [1,2,4,5,7,8,10], num1 is the sum of those integers = 37.
– Integers in the range [1, 10] that are divisible by 3 are [3,6,9], num2 is the sum of those integers = 18.
We return 37 – 18 = 19 as the answer.

Example 2:
Input: n = 5, m = 6
Output: 15
Explanation: In the given example:
– Integers in the range [1, 5] that are not divisible by 6 are [1,2,3,4,5], num1 is the sum of those integers = 15.
– Integers in the range [1, 5] that are divisible by 6 are [], num2 is the sum of those integers = 0.
We return 15 – 0 = 15 as the answer.

Example 3:
Input: n = 5, m = 1
Output: -15
Explanation: In the given example:
– Integers in the range [1, 5] that are not divisible by 1 are [], num1 is the sum of those integers = 0.
– Integers in the range [1, 5] that are divisible by 1 are [1,2,3,4,5], num2 is the sum of those integers = 15.
We return 0 – 15 = -15 as the answer.

Constraints:
1 <= n, m <= 1000

Divisible and Non-divisible Sums Difference (Brute Force Algorithm)

We can check each number and categorize it. Then we compute the sums for each group. But we don’t need to actually put them in a group, rather, we can add the number if it is not disivible by m, or subtract it if it is disible by m.

1
2
3
4
5
6
7
8
9
10
class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        ## BRUTE FORCE
        ans = 0
        for i in range(1, n + 1):
            if i % m == 0:
                ans -= i
            else:
                ans += i
        return ans
class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        ## BRUTE FORCE
        ans = 0
        for i in range(1, n + 1):
            if i % m == 0:
                ans -= i
            else:
                ans += i
        return ans

The time complexity is O(N). The space complexity is O(1).

Using Math fo Compute the Divisible and Non-divisible Sums Difference

Suppose there are k numbers within the range [1, n] that are divisible by m. So we have:

tex_ed0b2d03f3538512b17ef023b716f98b Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming

Let tex_97f7b47b146085ad64e1605f5ef62e45 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming be the sum of the numbers that are divisible by m. And tex_46160c0f81358526087e6936a1c53335 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming be the sum of the numbers that are not divisible by m.
Let tex_e63be394c81829edba551a57db63d230 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming be the sum of all the numbers between 1 to N inclusive, we know that:

tex_34f3f82e20c693ef59256aba07ce5fa1 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming
Therefore,
tex_6b6253dca66f85bc547526a4d765a342 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming

There are k numbers which are divisble by m so they are, [m, 2m, 3m, 4m, … km]

tex_3cc21c3f820e66eb1cd127fa1309a03f Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming
So, tex_f63e2033f3a7e25359d8d3f419ca2993 Teaching Kids Programming - Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math) algorithms Brute Force Algorithm math python Python teaching kids programming

Using the equation, we can implement a O(1) (both time and space) math solution:

1
2
3
4
class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        k = n // m
        return n * (n + 1) // 2 - k * (k + 1) * m
class Solution:
    def differenceOfSums(self, n: int, m: int) -> int:
        k = n // m
        return n * (n + 1) // 2 - k * (k + 1) * m

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
923 words
Last Post: How to Fix "Returned Error: replacement transaction underpriced" when Sending the ETH on Ethereum Blockchain?
Next Post: What are the Differences between uBPF and eBPF?

The Permanent URL is: Teaching Kids Programming – Divisible and Non-divisible Sums Difference (Brute Force Algorithm and Math)

Leave a Reply