The Odd Pyramid

Programming challenges during job interviews are discouraged nowadays, as it gives a skewed image of the skills of the respective candidate.
But I really liked to give a candidate this particular programming challenge, as it showed me how the developer thought about problems before he started coding. For thinking of how the solutions look like before starting to code saves a lot of time in this case.
Task
The odd numbers are aligned as follows to a triangle of infinite height:
In the first row thereβs the first odd number, 1. In the second row the next two odd numbers, so 3 and 5. In the third the next three, and so on.
Write a program that provides the sum of all the numbers in the row with the number i.
Solution
I really like this task. It appears to be very complicated and complex to calculate, until you sum the first couple of lines manually.
That would look like this:
Row | Numbers | Sum | resp. |
---|---|---|---|
1 | 1 | 1 | 1^3 |
2 | 3 5 | 8 | 2^3 |
3 | 7 9 11 | 27 | 3^3 |
4 | 13 15 17 19 | 64 | 4^3 |
For the row with the number the solution appears to be - a program calculating this is trivial. Hereβs a solution in Python that also returns a table with the first 10 lines:
1def solution(i):
2 return i ** 3
3
4def nth_odd(n):
5 return 2 * n - 1
6
7def sum_i(i):
8 return i * (i + 1) // 2
9
10def gen_line(i):
11 k = sum_i(i - 1) + 1
12 return [nth_odd(k + j) for j in range(i)]
13
14def gen_line_str(i):
15 return " ".join(map(str, gen_line(i)))
16
17return [["*Row*", "*Numbers*", "*Result*"], None] + [[i,gen_line_str(i), solution(i)] for i in range(1, 11)]
This code also contains methods to generate the table, something that the task actually doesnβt ask for. But that is usually the biggest headscratcher for a prospective candidate until you give him the hint that the solution may be way easier than he thinks and he should just have a look at what the numbers look like.
I do not expect people to be able to prove that this is the case, even though the method to construct the pyramid is actually the key to understanding why i^3 is the correct solution.
But for that we need some number theory.
Proof
First we need a formula for the sum of all natural numbers up until and including a number . That formula has been found by the German mathematician GauΓ: - letβs prove that this is the case:
Lemma GauΓ
For the function defined as
we want to prove:
Proof by induction
Anchor
For :
which was to be shown.
Assumption
Letβs assume, the property holds for all numbers up to .
Step
Then:
which was to be proven.
Lemma Sum of the first n odd numbers
Hereβs a surprising fact: the sum of the first odd numbers is always - meaning, all square numbers can be formed through the sums of the odd numbers:
Es gibt den folgenden ΓΌberraschenden Zusammenhang: die Summe der ersten ungeraden Zahlen ist immer - sprich, alle Quadratzahlen lassen sich als Summe der ungeraden Zahlen abbilden:
n | Numbers | Sum | resp. |
---|---|---|---|
1 | 1 | 1 | 1^2 |
2 | 1 3 | 4 | 2^2 |
3 | 1 3 5 | 9 | 3^2 |
4 | 1 3 5 7 | 16 | 4^2 |
Proof by induction
To prove
Given the function defined as:
meaning the sum of the first odd numbers. We want to show that .
Anchor
For we have:
and therefore also .
Assumption
Letβs assume, the property is true for all numbers up to .
Step
As
this holds:
which was to be proven.
Proof of the solution
Note that the sum of all numbers in row i is equal to the sum of all numbers including row i minus the sum of all numbers up to row i - 1.
The number of odd numbers up until and including row i is exactly the sum of all odd numbers up to the sum of all natural numbers up to i. For our problem that means:
After substituting we have:
which proves that our solution is correct.