Wednesday, June 15, 2016

Sums of Three Cubes (Python)

We've got triplets!!


This is a continuation of a previous post on the blog, which I wrote in Java.

There is a phenomenon between mathematicians and computer scientists to find patterns in everything. In this post, we will explore a particular pattern that has perplexed maths geniuses for some time now. The most exciting thing is that you, the reader and an enthusiast, can run these scripts at home to find solutions. In fact, one guy has already found a previously-unknown solution to the pattern!


So what is the pattern? It reads something like: for any given integer, there exists a triplet of integers whose cubes sum to that integer. For example, a solution for the integer 3 could be 1, 1, and 1. This is because 1³ + 1³ + 1³ = 3. There are some integers, however, which we have proven do not have solutions, and this has been done with complex math that even I cannot explain.


So do you think we can write code to find some of our own solutions? The first trick in test-driven development is to write some test cases to ensure we are looking for the correct output.



xformulae
-10-4^3 + 3^3 + 3^3
20^3 + 1^3 + 1^3
5No matches!
100-6^3 + -3^3 + 7^3
......



In our Python script, I check every single integer across double the range of a number. This number can be changed, but the larger it is, the longer the computations will take. Luckily, a sweet-spot for performance and result is the number 50, which would enable the program to check numbers between -50 and 50. Below is the source code. Try it for yourself! It's over 100000 iterations per run...



sssssssss