See if a number is expressible as a sum of two cubes (and calculate Taxicab numbers) in C#

I was watching "Futurama" reruns the other day and in the episode "Lesser of Two Evils" it comes out that Flexo's and Bender's serial numbers are 3370318 and 2716057 respectively. Bender also says that both are expressible as a sum of two cubes.

So of course I had to write a program to find the cubes.

If A = B3 + C3, then either B3 or C3 must be no more than A/2. Assume B < C. Then B3 ≤ A/2 so B ≤ cuberoot(A/2).

When you enter a number and click the Go button, the program loops variable B from 0 to cuberoot(A/2). (In C# you can calculate the cube root of a number by raising it to the 1/3 power.)

For each possible value B, the code calculates A - B3 and takes the integer cube root to see what C would be. If C3 exactly equals A - B3, then we have a solution.

The program continues for other values of B to see if there are other solutions.

private void btnGo_Click(object sender, EventArgs e)
{
txtResult.Clear();
this.Cursor = Cursors.WaitCursor;

long A = long.Parse(txtNumber.Text);
long max_B = (long)Math.Pow(A / 2.0, 1.0 / 3.0);
if (2 * max_B * max_B * max_B < A) max_B++;

// Try numbers up to the cube root of A.
for (int B = 0; B <= max_B; B++)
{
long remainder = A - (B * B * ;
long C = (long)Math.Round(Math.Pow(remainder, 1.0 / 3.0));
if (C * C * C == remainder)
{
txtResult.Text +=
B + "^3 + " +
C + "^3 = " +
(B * B * + " + " + (C * C * C) + "\r\n";
txtResult.Select(0, 0);
Application.DoEvents();
}
}

System.Media.SystemSounds.Beep.Play();
this.Cursor = Cursors.Default;
}

The program found that Flexo's serial number 3370318 = 1193 + 1193 = 1685159 + 1685159.

Much to my surprise (because Futurama's nerd humor is usually correct), Bender's serial number is not expressible as a sum of two positive cubes. However, it is 2716057 = (-951) 3 + 952 3 = -860085351 + 862801408. If you allow negative numbers it works, but it also it seems like anything might be possible.

Notes:

  • The smallest number expressible in N different ways as the sum of 2 positive cubes is called Taxicab(N).
  • Taxicab(2) = 1729 is expressible as 13 + 123 and 93 + 103
  • Taxicab(3) = 87539319
  • Taxicab(4) = 6963472309248
  • Taxicab(5) = 48988659276962496 (although this number is too big to fit in Visual Basic 6's Currency data type)
  • It is not known whether Taxicab(6) = 24153319581254312065344. It is expressible in 6 ways as a sum of two cubes but it's not known whether that is the smallest such number.

   

 

What did you think of this article?




Trackbacks
  • No trackbacks exist for this post.
Comments
  • No comments exist for this post.
Leave a comment

Submitted comments are subject to moderation before being displayed.

 Name

 Email (will not be published)

 Website

Your comment is 0 characters limited to 3000 characters.