# Multiples in Number Range

Repo: https://github.com/Andrewnetwork/MathematicalProgrammingProblems

## 0.) Definitions 
A *range* of natural numbers, positive integers, can be defined by the notation $[x,y]$ where $x$ is the starting number and $y$ is the ending number. Example: $[0,10] = [0,1,2,3,4,5,6,7,8,9,10]$. 

In the range $[x,y]$ there are $y-x = \lvert[x,y]\lvert$ numbers. Example: $\lvert[2,5]\lvert= 5 - 2 = 3.$ There are three numbers in the range $[2,5]$: $[2,3,4]$

A number $m$ is a *multiple* of some natural number $n$ if there is some integer $i$ such that $n = im$.

## 1.) How Many Multiples in a Range

Let $x,y,z \in \mathbb{N}.$ 


In the number range $[x,y]$ given $x<y$, how many multiples of $z$ are there? 

### Solution
Haskell Code:
```Haskell
multInRange strt stop mult = floor((stop-strt)/mult)
```
Mathematics: 
$$\large \lfloor \frac{\lvert[x,y]\lvert}{z} \rfloor$$

$\lvert[x,y]\lvert = y-x$ gives us the total number of natural numbers in this range. $\lfloor \frac{\lvert[x,y]\lvert}{z}\rfloor$ gives us how many times z partitions we can get in this range. I.e. multiples.   

### Problem Examples

#### 1: Within the range of natural numbers $[1,10]$ how many multiples of $3$ are there? 

Answer: $3$. 

Given $x=0,y=10,z=3$

$\large \lfloor \frac{(y-x)}{z} \rfloor = 3$

In haskell: $[1,2..10] = [1,2,3,4,5,6,7,8,9,10]$

Multiples of 3: $[3,6,9]$. 

Sum: $3 + 3*2 + 3*3 = 6*3 = 18$

#### 2:  Within the range of natural numbers $[1,20]$ how many multiples of $3$ are there? 
3,6,9,12,15,18

$3 + 3*2 + 3*3 + 3*4 + 3*5 + 3*6 = 7*3*3 = 7*6 = 21*3 = 63$

## 2.) Sum of Multiples in a Range
Let $x,y,z \in \mathbb{N}.$ 

In the range $[x,y]$ what is the sum of the multiples of z? 

### Solution
Haskell Code:
```Haskell
multInRange strt stop mult = floor((stop-strt)/mult)

sumNat n = (n*(n+1))/2

sumMult strt stop mult = mult * (sumNat (fromInteger (multInRange strt stop mult)))
```
Mathematics: 
$$\large\begin{align}
n = \lfloor \frac{(y-x)}{z} \rfloor\\
z*\frac{n*(n+1)}{2}
\end{align}$$

### Problem Examples
#### 1: In the range [0,100] what's the sum of the multiples of 5?
Multiples of $5$ in $[0,100] = [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100]$


How many multiples of $5$ are there in $[0,100]$? 
$$n = \lfloor \frac{(y-x)}{z} \rfloor == \lfloor \frac{100}{5} \rfloor = 20$$

Haskell Code: 
```Haskell
multInRange 0 100 5
-- Result: 20
```

Sum of these multiples:
$$5 + (5+5) + (5+5+5) + \ldots + (5 + 5 + \ldots n) = 5*1 + 5*2 + 5*3 + \dots + 5*n =  5*\sum_{i=1}^{n}{i} = 5 $$ 

As we see here, to compute the sum of multiples of $5$ in the range $[0,100]$, we compute the sum of integers up to how many multiples are in that range multiplied by $5$. 

$$
\large\begin{align}
z*\frac{n*(n+1)}{2} = 5 * \frac{20*(20+1)}{2} = 1050
\end{align}$$

Haskell Code: 
```Haskell
sumMult 0 100 5
-- Result: 1050.0
```

#### 2: In the range [0,100] what's the sum of the multiples of 5 and 6?
Multiples of $5$ and $6$ in $[0,100] = $

$$[5,6,10,12,15,18,20,24,25,30,35,36,40,42,45,48,50,54,55,60,65,66,70,72,75,78,80,84,85,90,95,96,100]$$

We can extend the results from the previous question to multiple multiples on a range.

We can find the sum of the multiples of $5$ and $6$ on the range $[0,100]$. Independently by:
Haskell Code: 
```Haskell
a = (sumMult 0 100 5)
b = (sumMult 0 100 6)
```
But alas, $a + b$ is not our answer! We need to take into consideration the multiples $5$ and $6$ share in common so we do not include the twice in our calculation. The multiples of $5$ and $6$ are multiples of $5*6 = 30$. Therefore we achieve the result by computing:

Haskell Code: 
```Haskell
(sumMult 0 100 5) + (sumMult 0 100 6) - (sumMult 0 100 (5*6) )
-- Result: 1686.0
```

5f34401edf01079130bb6f23704c2d5af7e44596
ipynb
Jupyter Notebook
MathematicalProgrammingProblems/Notebooks/Multiples in Number Range.ipynb
Andrewnetwork/HaskellExercises
3caeb255b99bd69a39e9d021cb987c3cec4b641a
MIT
MathematicalProgrammingProblems/Notebooks/Multiples in Number Range.ipynb
Andrewnetwork/HaskellExercises
3caeb255b99bd69a39e9d021cb987c3cec4b641a
MIT
MathematicalProgrammingProblems/Notebooks/Multiples in Number Range.ipynb
Andrewnetwork/HaskellExercises
3caeb255b99bd69a39e9d021cb987c3cec4b641a
MIT
2018-12-25T02:35:13.000Z
2018-12-25T02:35:13.000Z
Qwen/Qwen-72B
1. YES
2. YES