Python Challenge: Nicomachus’ Theorem

Nicomachus of Gerasa was an ancient Greek mathematician who lived around 60–120 AD. He is best known for his work Introduction to Arithmetic, one of the earliest surviving books on number theory.

One of the most famous results attributed to Nicomachus is a striking theorem about numbers and patterns. It shows that if you add together the cubes of the first n natural numbers, the result is exactly the same as squaring the sum of those numbers—a surprisingly elegant mathematical relationship.

In this challenge, we will write a Python program to verify Nicomachus’ theorem with any given value for n.

To do so we will first investigating one a surprising relationship between the sum of consecutive odd numbers and perfect square numbers. Effectively Nicomachus observed that:

The sum of the first n odd numbers is equal to n².

We can translate this observation into a mathematical equation:

1 + 3 + 5 + \cdots + (2n - 1) = n^2

For instance:
1 = 1^2 1 + 3 = 4 = 2^2 1 + 3 + 5 = 9 = 3^2 1 + 3 + 5 + 7 = 16 = 4^2

Now let’s use some Python code to verify this theory:

Nicomachus’s Theorem

Nicomachus’s Theorem can be defined as follows:

The sum of all the cubes of the first n natural numbers is equal to the square of the sum of those numbers.

We can translate this theorem into a mathematical equation:
1^3 + 2^3 + 3^3 + \cdots + n^3 = (1 + 2 + 3 + \cdots + n)^2

Using sigma notation, the same theorem can be expressed more compactly as:
\sum_{k=1}^{n} k^3 = \left( \sum_{k=1}^{n} k \right)^2

For instance:
1^3 = 1 = 1^2 1^3 + 2^3 = 9 = (1+2)^2 1^3 + 2^3 + 3^3 = 36 = (1 + 2 + 3)^2 1^3 + 2^3 + 3^3 +4^3 = 100 = (1 + 2 + 3 + 4)^2

Python Challenge

Your task is to reuse a similar approach as used in the above Python code to verify this theorem for any given value of n.

unlock-access

Solution...

The solution for this challenge is available to full members!
Find out how to become a member:
➤ Members' Area

Did you like this challenge?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this challenge interesting...

Follow us on social media!