Sum Of Multiples

avatar

Hello. With this math post, I would like to cover the sum of multiples. I have added sections of arithmetic sequences, Sigma sum notation and some programming code.

Math text rendered in Latex with Quicklatex.com.


Pixabay Image Source

 

Topics


  • Sigma Sum Notation Review
  • Formula For Adding Multiples
  • Arithmetic Sequences
  • Examples
  • Coding Examples With R & Python

 

Sigma Sum Notation Review


The sigma sum notation does look a bit scary at first but the notation behind it is logical and compact. Instead of adding the numbers 1 to 100 in this format 1 + 2 + 3 + 4 + ... + 100, we can write this in a compact way.

when k = 1 I have 1 as the first number, when k = 2 I have 2 as the second number being added. This continues with adding 3, 4 all the way up to the final number as 100 for the value of k. The variable k is a bit of a counter variable. You can use a different variable letter like i of j instead of k.

Having 2 + 4 + 6 + 8 + ... + 200 is like 1 + 2 + 3 + 4 + ... + 100 but everything is doubled. The sigma sum notation representation would be:


Pixabay Image Source

 

Formula For Adding Multiples


There is a math formula for computing stuff like 1 + 2 + 3 + 4 + ... + 100.

To add the whole numbers from 1 to 100 the formula would be:

 

From the above, the last number 100 is added to the first number 1 to get 101. The second number of 2 is added to the second last number of 99 to get 101. This repeats until there are 50 pairs of 101. This results in the multiplication of 50 times 101 to get 5050 for the sum of whole numbers from 1 to 100 (inclusive).

General Formula

Here is the general formula for adding numbers from a whole number a to some whole number n.

 

Arithmetic Sequences


This section is on arithmetic sequences. An arithmetic sequence is a list of numbers that increases or decreases by the same amount. One example is the odd numbers which are 1, 3, 5, 7, 9 and so on. The difference is +2 to obtain the next number.

Given a non-endless amount of numbers in an arithmetic sequence the formula for obtaining the n-th term is:

where

  • t_n is the n-th term in the arithmetic sequence, could also be last term in a non-endless sequence
  • t1 is the first term in the sequence
  • d is the common difference
  • n is the number of terms in the arithmetic sequence

 

The part of interest is n the number of terms in the arithmetic sequence.

Example

How many numbers are there in 7, 14, 21, 28, 35, ..., 147?

The given sequence is arithmetic as the common difference is 7. Seven is the first term and the last term is 149. Substitute values accordingly into the formula above and solve for n.

 


Pixabay Image Source

 

Examples


Here are a few examples with using the formula above to evaluate sums.

Example One - Add Multiples Of 3 From 1 to 100

The first term here is 3. You could use the arithmetic sequence formula to find the last multiple of 3 from 1 to 100. It is obvious though that the last term that is a multiple of 3 is 99.

 

Example Two - Add Multiples Of 2 & Multiples Of 5 From 1 to 100

For this example we add the multiples of 2 from 1 to 100 and the multiples of 5 from 1 to 100. One thing to note is that some multiples of 5 are multiples of 2. Examples of this include 10, 20, 30, ... , 100. These double counting cases have to be removed by subtracting by the sum of multiples of 10.

 

As a side calculation, you can determine the number of terms for the multiples of 5 from 1 to 100.

There are 20 multiples of 5 from 1 to 100 inclusive. With multiples of 10 from 1 to 100, that would be 10 numbers (Calculation not shown).

 

Here are the calculations for the sum of the multiples of 2 and 5. I use this formula:

 


Pixabay Image Source

Coding Examples With R & Python


Sigma sum notation can be related to a for loop from programming. The sum from 1 to 100 in a for loop in Python code would be something like:

total = 0

for j in range(101):
  total = total + j

In this section, I showcase a few examples from R & Python programming.

Example One - Add Multiples Of 3 From 1 to 100

The multiples of 3 from 1 to 100 are 3, 6, 9, 12, 15, ..., 99.

In R, use the seq() function to obtain the multiples of 3 in a list/vector. Then use sum().

by_three <- seq(from = 3, to = 100, by = 3)

# Sum of multiples of 3 from 1 to 100:
sum(by_three)

 

With Python, I am using list comprehension to obtain the multiples of 3. Afterwards I take the sum of all the multiples of 3.

threes = [x for x in range(3, 100, 3)]

sum(threes)

 

Example Two - Add Multiples Of 8 From 1 to 300

This second example deals with multiples of 8 from 1 to 300.

R Code

by_eights <- seq(8, 300, 8)

# Sum of multiples of 3 from 1 to 100:
sum(by_eights)

 

Python Code

eights = [k for k in range(8, 300, 8)]
sum(eights)

 

Example Three - Add Multiples Of 3 & Multiples Of 5 From 20 to 100

This third example is a bit more involved as there are two sums involved along with a condition. Explanations in comments.

R Code

# Mutliples of 3 from 1 to 100 and then filter to get multiples 20 to 100:
by_threes <- seq(3, 100, 3)
threes <- by_threes[by_threes >= 20]

# Mutliples of 5 from 1 to 100 and then filter to get multiples 20 to 100:
by_fives <- seq(5, 100, 5)
fives <- by_fives[by_fives >= 20]

# Intersection of 3s and 5s are multiples of 15s (Lowest common multiple)
intersect(threes, fives)

# Get sum: sum(3s) + sum(5s) - sum(intersection of 3s & 5s)
sum(threes) + sum(fives) - sum(intersect(threes, fives))

 

List comprehensions with if statements are super helpful in Python. The use of set() and .intersection() is combined to obtain the multiples of 3 and 5.

threes = [k for k in range(3, 100, 3) if k >= 20]

fives = [j for j in range(5, 100, 5) if j >= 20]

intersections = set(threes).intersection(set(fives))

sum(threes) + sum(fives) - sum(intersections)

 


Pixabay Image Source

Thank you for reading.

Posted with STEMGeeks



0
0
0.000
2 comments