Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleSee if a number is expressible as a sum of two cubes (and calculate Taxicab numbers) in Visual Basic 2005
DescriptionThis example shows how to see if a number is expressible as a sum of two cubes (and calculate Taxicab numbers) in Visual Basic 2005.
Keywordssums of cubes, cubes, numeric algorithms, numeric, taxicab number, Visual Basic 2005, VB.NET
CategoriesAlgorithms, VB.NET
 
I was watching "Futurama" reruns thue 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 = B^3 + C^3, then either B^3 or C^3 must be no more than A/2. Assume B < C. Then B^3 <= 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 Visual Basic, you can calculate the cube root or a number by taking the log of the number, dividing by 3, and then taking the exponent of the result.)

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

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

 
Private Sub btnGo_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnGo.Click
    txtResults.Text = ""
    Me.Cursor = Cursors.WaitCursor
    Application.DoEvents()

    Dim A As Decimal = Decimal.Parse(txtNumber.Text)
    Dim max_B As Decimal = CDec(Exp(Log(A / 2) / 3))
    If max_B * max_B * max_B > A Then max_B -= 1

    ' Try numbers up to max_B.
    For B As Decimal = 0 To max_B
        Dim remainder As Decimal = A - (B * B * B)
        Dim C As Decimal = CLng(Exp(Log(remainder) / 3))
        If C * C * C = remainder Then
            txtResults.Text = txtResults.Text & _
                B & "^3 + " & _
                C & "^3 = " & _
                (B * B * B) & " + " & (C * C * C) & vbCrLf
            txtResults.Select(0, 0)
            Application.DoEvents()
        End If
    Next B

    Beep()
    Me.Cursor = Cursors.Default
End Sub
 
The program found that Flexo's serial number 3370318 = 119^3 + 119^3 = 1685159 + 1685159.

Much to my surprise, Bender's serial number is not expressible as a sum of two positive cubes. However, Bender's serial number 2716057 = (-951) ^ 3 + 952 ^ 3 = -860085351 + 862801408.

If you allow negative numbers, it seems as if anything might be possible.

Notes:

  • The smallest number expressible as the sum of N cubes is called Taxicab(N).
  • Taxicab(2) = 1729 is the smallest number expressible in two different ways as the sum of two (positive) cubes 1^3 + 12^3 and 9^3 + 10^3
  • Taxicab(3) = 87539319
  • Taxicab(4) = 6963472309248
  • Taxicab(5) = 48988659276962496
  • It is not known whether Taxicab(6) = 24153319581254312065344, but that is a candidate (it is expressible in 6 ways as a sum of two cubes)
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated