Jump to content

Taxicab numbers: Difference between revisions

(→‎{{header|jq}}: truncate sum_of_two_cubes to guarantee accuracy)
(→‎{{header|Tcl}}: Added zkl)
Line 1,679:
2005: 1676926719 = 1095³ + 714³, 1676926719 = 1188³ + 63³
2006: 1677646971 = 990³ + 891³, 1677646971 = 1188³ + 99³
</pre>
 
=={{header|zkl}}==
{{trans|D}}
An array of bytes is used to hold n, where array[n³+m³]==n.
 
No extra credit, the data structure I'm using won't grow to the size needed and I'm too lazy to do something else.
<lang zkl>fcn taxiCabNumbers{
const HeapSZ=0d5_000_000;
iCubes:=[1..120].apply("pow",3);
sum2cubes:=Data(HeapSZ).fill(0); // BFheap of 1 byte zeros
taxiNums:=List();
foreach i,i3 in ([1..].zip(iCubes)){
foreach j,j3 in ([i+1..].zip(iCubes[i,*])){
ij3:=i3+j3;
if(z:=sum2cubes[ij3]){
taxiNums.append(T(ij3,
z,(ij3-z.pow(3)).toFloat().pow(1.0/3).round().toInt(),
i,j));
}
else sum2cubes[ij3]=i;
}
}
taxiNums.sort(fcn([(a,_)],[(b,_)]){ a<b })
}</lang>
<lang zkl>fcn print(n,taxiNums){
[n..].zip(taxiNums).pump(Console.println,fcn([(n,t)]){
"%4d: %10,d = %2d\u00b3 + %2d\u00b3 = %2d\u00b3 + %2d\u00b3".fmt(n,t.xplode())
})
}
taxiNums:=taxiCabNumbers(); // 63 pairs
taxiNums[0,25]:print(1,_);</lang>
{{out}}
<pre>
1: 1,729 = 1³ + 12³ = 9³ + 10³
2: 4,104 = 2³ + 16³ = 9³ + 15³
3: 13,832 = 2³ + 24³ = 18³ + 20³
4: 20,683 = 10³ + 27³ = 19³ + 24³
5: 32,832 = 4³ + 32³ = 18³ + 30³
6: 39,312 = 2³ + 34³ = 15³ + 33³
7: 40,033 = 9³ + 34³ = 16³ + 33³
8: 46,683 = 3³ + 36³ = 27³ + 30³
9: 64,232 = 17³ + 39³ = 26³ + 36³
10: 65,728 = 12³ + 40³ = 31³ + 33³
11: 110,656 = 4³ + 48³ = 36³ + 40³
12: 110,808 = 6³ + 48³ = 27³ + 45³
13: 134,379 = 12³ + 51³ = 38³ + 43³
14: 149,389 = 8³ + 53³ = 29³ + 50³
15: 165,464 = 20³ + 54³ = 38³ + 48³
16: 171,288 = 17³ + 55³ = 24³ + 54³
17: 195,841 = 9³ + 58³ = 22³ + 57³
18: 216,027 = 3³ + 60³ = 22³ + 59³
19: 216,125 = 5³ + 60³ = 45³ + 50³
20: 262,656 = 8³ + 64³ = 36³ + 60³
21: 314,496 = 4³ + 68³ = 30³ + 66³
22: 320,264 = 18³ + 68³ = 32³ + 66³
23: 327,763 = 30³ + 67³ = 51³ + 58³
24: 373,464 = 6³ + 72³ = 54³ + 60³
25: 402,597 = 42³ + 69³ = 56³ + 61³
</pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.