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 6
DescriptionThis example shows how to see if a number is expressible as a sum of two cubes (and calculate Taxicab numbers) in Visual Basic 6.
Keywordssums of cubes, cubes, numeric algorithms, numeric, taxicab number, Visual Basic 6
CategoriesAlgorithms
 
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 cmdGo_Click()
Dim A As Currency, B As Currency, C As Currency
Dim max_B As Currency
Dim remainder As Currency

    txtResults.Text = ""
    Me.MousePointer = vbHourglass
    DoEvents

    A = CCur(txtNumber.Text)
    max_B = Int(Exp(Log(A / 2) / 3)) + 1
    If (max_B * max_B * max_B > A) Then max_B = max_B - 1

    ' Try numbers up to the cube root of A.
    For B = 0 To max_B
        remainder = A - (B * B * B)
        C = 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.SelStart = 0
            txtResults.SelLength = 0
            DoEvents
        End If
    Next B

    Beep
    Me.MousePointer = vbDefault
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 (although this number is too big to fit in Visual Basic 6's Currency data type)
  • 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