So I’m reading some math for pleasure (and for some scripting ideas) and I arrive at the following problem:
Find a formula for the sum of the natural numbers from m to n.
In the chapter they discuss and derive the formula for the sum of the integers from 1 to n. This can be stated as
The solution then is relatively straightforward, just subtract the first m – 1 integers from equation (1). That is, the sum from 1 to the m – 1 natural numbers is given by
Subtracting (2) from (1) gives
Note that m and n are natural numbers, and that m ≤ n.
Programmatically speaking, it would be nice if when I divided by 2, I always got an integer. Logically, this should be the case since I’m just adding together natural numbers and they should sum to an natural number. However, I thought it might be a nice exercise to write out a proof of this fact.
First, a quick preliminary (which I won’t prove). Note that any even integer multiplied by any odd integer always results in an even integer. The the proof follows from the fact that since an even integer is divisible by 2, the product of an even and odd integer is still divisible by 2 and thus also even.
Note then that is the product of an even integer times an odd integer. Similarly for . This result extends to natural numbers since the natural numbers are a subset of the integers.
Here is a quick example. Pick . Then and so . In other words if is odd then is even. Conversely, if is even then is odd. A similar situation occurs with and .
Suppose that and are natural numbers and that . Consider the expression . Note that both and are always even since they are always the product of and even number and odd number for any choice of . Since and this means . Therefore the expression is always an even natural number and further
This completes the proof
At some point maybe I’ll write up a script to calculate the sum of the integers numbers from m to n.
If you’re not math symbol aware then I’ll point out that represents the set of natural numbers. These are the numbers . So means that is an natrual number. The expression says that “2 divides 4″. Another way to say this is 4 is divisible by 2.