Title: Test Nicomachus's theorem in Python
The Greek mathematician Nicomachus of Gerasa (c. 60 – c. 120 CE) noted that an interesting property that you get if you write out a list of odd numbers.
- The first odd number is 1, which is 13
- If you add the next two odd numbers, you get 3 + 5 = 8, which is 23
- If you add the next three odd numbers, you get 7 + 9 + 11 = 27, which is 33
Nicomachus didn't go any further, but this is true if you extend it: The next N odd numbers add up to N3.
From this, you can also derive what's sometimes called Nicomachus's theorem.
13 + 23 + 33 + ... + n3 = (1 + 2 + 3 + ... + n)2
This example simply verifies these results.
Checking Nicomachus's Observation
First, the program uses the following method to check Nicomachus's observation.
def check_nicomachus_observation(max):
'''Check Nicomachus's original observation for i = 1, 2, ..., n.'''
odd_number = 1
for n in range(1, max + 1):
# Add up the next i odd nmbers.
sum_of_odds = 0
for next_odd in range(n):
sum_of_odds += odd_number
odd_number += 2
# Calculate i ** 3.
n_cubed = n ** 3
# Print the result.
print(f'{n:3}: {sum_of_odds} == {n_cubed}')
This code sets odd_number equal to the first odd number: 1. It then enters a loop to check the observation for the value n.
Inside the loop, the code adds up the next n odd numbers. When it's done, it calculates n3 and compares that to the sum of the odd numbers.
Checking Nicomachus's Theorem
Next, the program uses the following code to check Nicomachus's theorem.
def check_nicomachus_theorem(max):
'''Check Nicomachus's theorem for i = 1, 2, ..., n.'''
for n in range(1, max + 1):
# Add up cubes 1**3 + 2**3 + 3**3 + ... + n**3.
sum_of_cubes = 0
for i in range(1, n + 1):
sum_of_cubes += i ** 3
# Calculate the sum of the numbers and square the result.
sum_of_numbers = 0
for i in range(1, n + 1):
sum_of_numbers += i
sum_squared = sum_of_numbers * sum_of_numbers
# Print the result.
print(f'{n:3}: {sum_of_cubes} == {sum_squared}')
This code loop through values n. For each n, it loops from 1 to n and adds the cubes of those numbers.
Next, the code adds the numbers between 1 and n and squares the result.
Finally, the method displays both results.
Using Comprehensions
It's easy enough to rewrite the second method using list comprehensions. The following code uses comprehensions to basically reduce the second method to two lines of interesting code (not counting comments and the print statement).
def check_nicomachus_theorem_comprehension(max):
'''Check Nicomachus's theorem for i = 1, 2, ..., n.'''
for n in range(1, max + 1):
# Add up cubes 1**3 + 3**3 + ... + n**3.
sum_of_cubes = sum([i**3 for i in range(1, n+1)])
# Calculate the sum of the numbers and square the result.
sum_squared = sum([i for i in range(1, n + 1)]) ** 2
# Print the result.
print(f'{n:3}: {sum_of_cubes} == {sum_squared}')
I can't think of an obvious way to use a comprehension to let the first add up the next run of odd numbers. If you can think of something, post a comment.
Also note that the sum of the first N odd numbers equals N2. For example, 1 + 3 + 5 + 7 + 9 + 11 = 36 = 62. You can write a method (and a comprehension version) to check this results if you like.
For more information on Nicomachus's theorem and related ides in number theory, see the Wikipedia post Squared triangular number.
Download the example to see additional details.
|