Taxicab numbers: Difference between revisions
Content deleted Content added
m →{{header|REXX}}: added more whitespace. |
|||
Line 3,203: | Line 3,203: | ||
</pre> |
</pre> |
||
=={{header|VBA}}== |
|||
<lang vb>Public Type tuple |
|||
i As Variant |
|||
j As Variant |
|||
sum As Variant |
|||
End Type |
|||
Public Type tuple3 |
|||
i1 As Variant |
|||
j1 As Variant |
|||
i2 As Variant |
|||
j2 As Variant |
|||
i3 As Variant |
|||
j3 As Variant |
|||
sum As Variant |
|||
End Type |
|||
Sub taxicab_numbers() |
|||
Dim i As Variant, j As Variant |
|||
Dim k As Long |
|||
Const MAX = 2019 |
|||
Dim p(MAX) As Variant |
|||
Const bigMAX = (MAX + 1) * (MAX / 2) |
|||
Dim big(1 To bigMAX) As tuple |
|||
Const resMAX = 4400 |
|||
Dim res(1 To resMAX) As tuple3 |
|||
For i = 1 To MAX |
|||
p(i) = CDec(i * i * i) 'convert Variant to Decimal |
|||
Next i 'wich hold numbers upto 10^28 |
|||
k = 1 |
|||
For i = 1 To MAX |
|||
For j = i To MAX |
|||
big(k).i = CDec(i) |
|||
big(k).j = CDec(j) |
|||
big(k).sum = CDec(p(i) + p(j)) |
|||
k = k + 1 |
|||
Next j |
|||
Next i |
|||
n = 1 |
|||
Quicksort big, LBound(big), UBound(big) |
|||
For i = 1 To bigMAX - 1 |
|||
If big(i).sum = big(i + 1).sum Then |
|||
res(n).i1 = CStr(big(i).i) |
|||
res(n).j1 = CStr(big(i).j) |
|||
res(n).i2 = CStr(big(i + 1).i) |
|||
res(n).j2 = CStr(big(i + 1).j) |
|||
If big(i + 1).sum = big(i + 2).sum Then |
|||
res(n).i3 = CStr(big(i + 2).i) |
|||
res(n).j3 = CStr(big(i + 2).j) |
|||
i = i + 1 |
|||
End If |
|||
res(n).sum = CStr(big(i).sum) |
|||
n = n + 1 |
|||
i = i + 1 |
|||
End If |
|||
Next i |
|||
Debug.Print n - 1; " taxis" |
|||
For i = 1 To 25 |
|||
With res(i) |
|||
Debug.Print String$(4 - Len(CStr(i)), " "); i; |
|||
Debug.Print String$(11 - Len(.sum), " "); .sum; " = "; |
|||
Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = "; |
|||
Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j2), " "); .j2; "^3" |
|||
End With |
|||
Next i |
|||
Debug.Print |
|||
For i = 2000 To 2006 |
|||
With res(i) |
|||
Debug.Print String$(4 - Len(CStr(i)), " "); i; |
|||
Debug.Print String$(11 - Len(.sum), " "); .sum; " = "; |
|||
Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = "; |
|||
Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j2), " "); .j2; "^3" |
|||
End With |
|||
Next i |
|||
Debug.Print |
|||
For i = 1 To resMAX |
|||
If res(i).i3 <> "" Then |
|||
With res(i) |
|||
Debug.Print String$(4 - Len(CStr(i)), " "); i; |
|||
Debug.Print String$(11 - Len(.sum), " "); .sum; " = "; |
|||
Debug.Print String$(4 - Len(.i1), " "); .i1; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j1), " "); .j1; "^3 = "; |
|||
Debug.Print String$(4 - Len(.i2), " "); .i2; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j2), " "); .j2; "^3"; |
|||
Debug.Print String$(4 - Len(.i3), " "); .i3; "^3 +"; |
|||
Debug.Print String$(4 - Len(.j3), " "); .j3; "^3" |
|||
End With |
|||
End If |
|||
Next i |
|||
End Sub |
|||
Sub Quicksort(vArray() As tuple, arrLbound As Long, arrUbound As Long) |
|||
'https://wellsr.com/vba/2018/excel/vba-quicksort-macro-to-sort-arrays-fast/ |
|||
'Sorts a one-dimensional VBA array from smallest to largest |
|||
'using a very fast quicksort algorithm variant. |
|||
'Adapted to multidimensions/typedef |
|||
Dim pivotVal As Variant |
|||
Dim vSwap As tuple |
|||
Dim tmpLow As Long |
|||
Dim tmpHi As Long |
|||
tmpLow = arrLbound |
|||
tmpHi = arrUbound |
|||
pivotVal = vArray((arrLbound + arrUbound) \ 2).sum |
|||
While (tmpLow <= tmpHi) 'divide |
|||
While (vArray(tmpLow).sum < pivotVal And tmpLow < arrUbound) |
|||
tmpLow = tmpLow + 1 |
|||
Wend |
|||
While (pivotVal < vArray(tmpHi).sum And tmpHi > arrLbound) |
|||
tmpHi = tmpHi - 1 |
|||
Wend |
|||
If (tmpLow <= tmpHi) Then |
|||
vSwap.i = vArray(tmpLow).i |
|||
vSwap.j = vArray(tmpLow).j |
|||
vSwap.sum = vArray(tmpLow).sum |
|||
vArray(tmpLow).i = vArray(tmpHi).i |
|||
vArray(tmpLow).j = vArray(tmpHi).j |
|||
vArray(tmpLow).sum = vArray(tmpHi).sum |
|||
vArray(tmpHi).i = vSwap.i |
|||
vArray(tmpHi).j = vSwap.j |
|||
vArray(tmpHi).sum = vSwap.sum |
|||
tmpLow = tmpLow + 1 |
|||
tmpHi = tmpHi - 1 |
|||
End If |
|||
Wend |
|||
If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi 'conquer |
|||
If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 'conquer |
|||
End Sub</lang>{{out}} |
|||
<pre> 4399 taxis |
|||
1 1729 = 9^3 + 10^3 = 1^3 + 12^3 |
|||
2 4104 = 2^3 + 16^3 = 9^3 + 15^3 |
|||
3 13832 = 2^3 + 24^3 = 18^3 + 20^3 |
|||
4 20683 = 19^3 + 24^3 = 10^3 + 27^3 |
|||
5 32832 = 18^3 + 30^3 = 4^3 + 32^3 |
|||
6 39312 = 15^3 + 33^3 = 2^3 + 34^3 |
|||
7 40033 = 16^3 + 33^3 = 9^3 + 34^3 |
|||
8 46683 = 27^3 + 30^3 = 3^3 + 36^3 |
|||
9 64232 = 26^3 + 36^3 = 17^3 + 39^3 |
|||
10 65728 = 31^3 + 33^3 = 12^3 + 40^3 |
|||
11 110656 = 4^3 + 48^3 = 36^3 + 40^3 |
|||
12 110808 = 27^3 + 45^3 = 6^3 + 48^3 |
|||
13 134379 = 12^3 + 51^3 = 38^3 + 43^3 |
|||
14 149389 = 29^3 + 50^3 = 8^3 + 53^3 |
|||
15 165464 = 38^3 + 48^3 = 20^3 + 54^3 |
|||
16 171288 = 24^3 + 54^3 = 17^3 + 55^3 |
|||
17 195841 = 9^3 + 58^3 = 22^3 + 57^3 |
|||
18 216027 = 22^3 + 59^3 = 3^3 + 60^3 |
|||
19 216125 = 45^3 + 50^3 = 5^3 + 60^3 |
|||
20 262656 = 36^3 + 60^3 = 8^3 + 64^3 |
|||
21 314496 = 4^3 + 68^3 = 30^3 + 66^3 |
|||
22 320264 = 32^3 + 66^3 = 18^3 + 68^3 |
|||
23 327763 = 51^3 + 58^3 = 30^3 + 67^3 |
|||
24 373464 = 54^3 + 60^3 = 6^3 + 72^3 |
|||
25 402597 = 56^3 + 61^3 = 42^3 + 69^3 |
|||
2000 1671816384 = 940^3 + 944^3 = 428^3 +1168^3 |
|||
2001 1672470592 = 29^3 +1187^3 = 632^3 +1124^3 |
|||
2002 1673170856 = 828^3 +1034^3 = 458^3 +1164^3 |
|||
2003 1675045225 = 744^3 +1081^3 = 522^3 +1153^3 |
|||
2004 1675958167 = 492^3 +1159^3 = 711^3 +1096^3 |
|||
2005 1676926719 = 714^3 +1095^3 = 63^3 +1188^3 |
|||
2006 1677646971 = 99^3 +1188^3 = 891^3 + 990^3 |
|||
455 87539319 = 167^3 + 436^3 = 228^3 + 423^3 255^3 + 414^3 |
|||
535 119824488 = 90^3 + 492^3 = 346^3 + 428^3 11^3 + 493^3 |
|||
588 143604279 = 408^3 + 423^3 = 359^3 + 460^3 111^3 + 522^3 |
|||
655 175959000 = 70^3 + 560^3 = 315^3 + 525^3 198^3 + 552^3 |
|||
888 327763000 = 300^3 + 670^3 = 339^3 + 661^3 510^3 + 580^3 |
|||
1299 700314552 = 334^3 + 872^3 = 456^3 + 846^3 510^3 + 828^3 |
|||
1398 804360375 = 15^3 + 930^3 = 295^3 + 920^3 198^3 + 927^3 |
|||
1515 958595904 = 22^3 + 986^3 = 180^3 + 984^3 692^3 + 856^3 |
|||
1660 1148834232 = 718^3 + 920^3 = 816^3 + 846^3 222^3 +1044^3 |
|||
1837 1407672000 = 140^3 +1120^3 = 396^3 +1104^3 630^3 +1050^3 |
|||
2100 1840667192 = 681^3 +1151^3 = 372^3 +1214^3 225^3 +1223^3 |
|||
2143 1915865217 = 9^3 +1242^3 = 484^3 +1217^3 969^3 +1002^3 |
|||
2365 2363561613 = 501^3 +1308^3 = 684^3 +1269^3 765^3 +1242^3 |
|||
2480 2622104000 = 1020^3 +1160^3 = 600^3 +1340^3 678^3 +1322^3 |
|||
2670 3080802816 = 904^3 +1328^3 = 81^3 +1455^3 456^3 +1440^3 |
|||
2732 3235261176 = 33^3 +1479^3 = 270^3 +1476^31038^3 +1284^3 |
|||
2845 3499524728 = 116^3 +1518^3 = 350^3 +1512^31169^3 +1239^3 |
|||
2895 3623721192 = 348^3 +1530^3 = 761^3 +1471^31098^3 +1320^3 |
|||
2979 3877315533 = 1224^3 +1269^3 = 1077^3 +1380^3 333^3 +1566^3 |
|||
3293 4750893000 = 210^3 +1680^3 = 945^3 +1575^3 594^3 +1656^3 |
|||
3562 5544709352 = 207^3 +1769^3 = 1076^3 +1626^3 842^3 +1704^3 |
|||
3589 5602516416 = 912^3 +1692^3 = 1020^3 +1656^3 668^3 +1744^3 |
|||
3826 6434883000 = 590^3 +1840^3 = 30^3 +1860^3 396^3 +1854^3 |
|||
4162 7668767232 = 44^3 +1972^3 = 1384^3 +1712^3 360^3 +1968^3 |
|||
4359 8849601000 = 1017^3 +1983^3 = 1530^3 +1740^3 900^3 +2010^3</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
{{trans|D}} |
{{trans|D}} |