Taxicab numbers: Difference between revisions
Content added Content deleted
(added FreeBASIC) |
|||
Line 873: | Line 873: | ||
2006 : {1677646971, [{891, 990}, {99, 1188}]} |
2006 : {1677646971, [{891, 990}, {99, 1188}]} |
||
</pre> |
</pre> |
||
=={{header|FreeBASIC}}== |
|||
<lang freebasic>' version 11-10-2016 |
|||
' compile with: fbc -s console |
|||
' Brute force |
|||
' adopted from "Sorting algorithms/Shell" sort Task |
|||
Sub shellsort(s() As String) |
|||
' sort from lower bound to the highter bound |
|||
Dim As UInteger lb = LBound(s) |
|||
Dim As UInteger ub = UBound(s) |
|||
Dim As Integer done, i, inc = ub - lb |
|||
Do |
|||
inc = inc / 2.2 |
|||
If inc < 1 Then inc = 1 |
|||
Do |
|||
done = 0 |
|||
For i = lb To ub - inc |
|||
If s(i) > s(i + inc) Then |
|||
Swap s(i), s(i + inc) |
|||
done = 1 |
|||
End If |
|||
Next |
|||
Loop Until done = 0 |
|||
Loop Until inc = 1 |
|||
End Sub |
|||
' ------=< MAIN >=------ |
|||
Dim As UInteger x, y, count, c, sum |
|||
Dim As UInteger cube(1290) |
|||
Dim As String result(), str1, str2, str3 |
|||
Dim As String buf11 = Space(11), buf5 = Space(5) |
|||
ReDim result(900000) ' ~1291*1291\2 |
|||
' set up the cubes |
|||
Print : Print " Calculate cubes" |
|||
For x = 1 To 1290 |
|||
cube(x) = x*x*x |
|||
Next |
|||
' combine and store |
|||
Print : Print " Combine cubes" |
|||
For x = 1 To 1290 |
|||
For y = x To 1290 |
|||
sum = cube(x)+cube(y) |
|||
RSet buf11, Str(sum) : str1 = buf11 |
|||
RSet buf5, Str(x) : str2 = buf5 |
|||
RSet buf5, Str(y) : Str3 = buf5 |
|||
result(count)=buf11 + " = " + str2 + " ^ 3 + " + str3 + " ^ 3" |
|||
count = count +1 |
|||
Next |
|||
Next |
|||
count= count -1 |
|||
ReDim Preserve result(count) ' trim the array |
|||
Print : Print " Sort (takes some time)" |
|||
shellsort(result()) ' sort |
|||
Print : Print " Find the Taxicab numbers" |
|||
c = 1 ' start at index 1 |
|||
For x = 0 To count -1 |
|||
' find sums that match |
|||
If Left(result(x), 11) = Left(result(x + 1), 11) Then |
|||
result(c) = result(x) |
|||
y = x +1 |
|||
Do ' merge the other solution(s) |
|||
result(c) = result(c) + Mid(result(y), 12) |
|||
y = y +1 |
|||
Loop Until Left(result(x), 11) <> Left(result(y), 11) |
|||
x = y -1 ' let x point to last match result |
|||
c = c +1 |
|||
End If |
|||
Next |
|||
c = c -1 |
|||
Print : Print " "; c; " Taxicab numbers found" |
|||
ReDim Preserve result(c) ' trim the array again |
|||
cls |
|||
Print : Print " Print first 25 numbers" : Print |
|||
For x = 1 To 25 |
|||
Print result(x) |
|||
Next |
|||
Print : Print " The 2000th to the 2006th" : Print |
|||
For x = 2000 To 2006 |
|||
Print result(x) |
|||
Next |
|||
' empty keyboard buffer |
|||
While Inkey <> "" : Wend |
|||
Print : Print "hit any key to end program" |
|||
Sleep |
|||
End</lang> |
|||
{{out}} |
|||
<pre> Print first 25 numbers |
|||
1729 = 1 ^ 3 + 12 ^ 3 = 9 ^ 3 + 10 ^ 3 |
|||
4104 = 2 ^ 3 + 16 ^ 3 = 9 ^ 3 + 15 ^ 3 |
|||
13832 = 2 ^ 3 + 24 ^ 3 = 18 ^ 3 + 20 ^ 3 |
|||
20683 = 10 ^ 3 + 27 ^ 3 = 19 ^ 3 + 24 ^ 3 |
|||
32832 = 4 ^ 3 + 32 ^ 3 = 18 ^ 3 + 30 ^ 3 |
|||
39312 = 2 ^ 3 + 34 ^ 3 = 15 ^ 3 + 33 ^ 3 |
|||
40033 = 9 ^ 3 + 34 ^ 3 = 16 ^ 3 + 33 ^ 3 |
|||
46683 = 3 ^ 3 + 36 ^ 3 = 27 ^ 3 + 30 ^ 3 |
|||
64232 = 17 ^ 3 + 39 ^ 3 = 26 ^ 3 + 36 ^ 3 |
|||
65728 = 12 ^ 3 + 40 ^ 3 = 31 ^ 3 + 33 ^ 3 |
|||
110656 = 4 ^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