Meet Carl Friedrich Gauss
The traditional Penny Savings Challenge works by incrementing the amount saved by £0.01 each day, leaving you with £667.95 after 365 days. Programming this approach would be relatively easy — transferring 1p x the number of days since starting (or using the day of the year if starting on Jan 1).
But what should you do if you want to reach a set goal after 365 days, such as £2,000? Bear in mind that if you simply saved £5.48 (2000/365) each day, you would lose the characteristic incrementing of the savings challenge.
And what if you want to save your goal over a different time period, like 50 days? Or 500 days?
For this we turn to Carl Friedrich Gauss, who among other things kindly gave to us the formula for calculating the sum of numbers from 1 to n. While at primary school 🤯. I'll digress slightly with the tale here, as it's a good anecdote —
One day Gauss' teacher asked his class to add together all the numbers from 1 to 100, assuming that this task would occupy them for quite a while. He was shocked when young Gauss, after a few seconds thought, wrote down the answer 5050. The teacher couldn't understand how his pupil had calculated the sum so quickly in his head, but the eight year old Gauss pointed out that the problem was actually quite simple:
He had added the numbers in pairs - the first and the last, the second and the second to last and so on, observing that 1+100 = 101, 2+99 = 101, 3+98 = 101, 4+97 = 101 ...so the total would be 50 lots of 101, which is 5050.
Have a look here for more info — and a visualisation of the proof.
Gauss' shortcut gives us the following formula, which we can use as our starting point:
sum = N * (N + 1) / 2
Tweaking the Gauss formula
Returning to the 1p Savings Challenge, if we apply Gauss' formula the total amount of money saved by the end of one year is as we expect:
totalSaved = (365 * 366) * 0.5
totalSaved = 133,590 * 0.5
totalSaved = 66,795p = £667.95
We know our goal is £2,000 and not £667.95, so let's play with this formula. If we remove the division by 2, we get a total of £1,335.90 (equivalent to saving £0.02 on day 1, £0.04 on day 2, £0.06 on day 3, etc.).
totalSaved = (365 * 366) * 1
totalSaved = 133,590 * 1
totalSaved = 133,590p = £1,335.90
Not quite there — but nearly. We need a factor >1 to multiply (365 * 366) by to get us to our £2,000 goal.
200,000 = (365 * 366) * F
200,000 = 133,590 * F
We can calculate this new factor (F) by flipping the above equation.
F = 200,000 / (365 * 366)
F = 200,000 / 133,590
F = 1.4971180478
So, for a given savings goal, we can now say that the multiplication factor (F) to include in the Gauss formula is calculated as follows:
F = savingsGoal / (savingsPeriod * (savingsPeriod + 1))
Where savingsPeriod
is the total number of days after which you wish to reach your savingsGoal
. Hang onto this formula — we'll need it later.
Double checking our work, when we use our new factor in the Gauss formula, the total saved in a one year period is as we expect:
totalSaved = (365 * 366) * F
totalSaved = (365 * 366) * 1.4971180478
totalSaved = 133,590 * 1.4971180478
totalSaved = 200,000p = £2,000
Calculating daily transfer amounts
Let's take another look at the first line of the above:
totalSaved = (365 * 366) * F
Until now, we have been using the formula to check the total saved after one year (365 days). But this formula can now be used to calculate how much we will have saved by any given day. We simply substitute in the day of interest:
totalSaved = (dayNumber * (dayNumber + 1)) * F
A few examples (where F = 1.4971180478
)
By day 10, we will have saved a total of:
total saved = (10 * 11) * 1.4971180478
total saved = 165p = £1.65
By day 200, we will have saved a total of:
totalSaved = (200 * 201) * 1.4971180478
totalSaved = 60,184p = £601.84
By day 365, as we know, we will have saved a total of £2,000:
totalSaved = (dayNumber * (dayNumber + 1)) * 1.4971180478
totalSaved = (365 * 366) * 1.4971180478
totalSaved = 200,000p = £2,000
Great stuff. But how can this information be used to calculate the amount to transfer on a given day?
Well, using the same formula we simply take the total we will have saved after transferring today (day N) and subtract the total we had saved by yesterday (day N-1). This gives us the transfer amount for that day:
We can think of this as follows:
transferAmount(today) = totalSaved(today) - totalSaved(yesterday)
For example (again where F = 1.4971180478
):
On day 10, we will transfer:
transferAmount = (10 * 11) * 1.4971180478 - (9 * 10) * 1.4971180478
transferAmount = 30p = £0.30
On day 200, we will transfer:
transferAmount = (200 * 201) * 1.4971180478 - (199 * 200) * 1.4971180478
transferAmount = 599p = £5.99
On day 365, we will make our final transfer:
transferAmount = (365 * 366) * 1.4971180478 - (364 * 365) * 1.4971180478
transferAmount = 1093p = £10.93
Next we'll take what we've discovered here and give it the Javascript treatment.
Top comments (0)