Taxicab numbers: Difference between revisions

m
(Added Forth entry)
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 4 users not shown)
Line 36:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V cubes = (1..1199).map(x -> Int64(x) ^ 3)
[Int64 = Int64] crev
L(x3) cubes
Line 60:
L(x1, x2) p
print(‘ = #4^3 + #4^3’.format(x1, x2), end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 99:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f TAXICAB_NUMBERS.AWK
BEGIN {
Line 127:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 162:
This is quite slow in most interpreters, although a decent compiler should allow it to complete in a matter of seconds. Regardless of the speed, though, the range in a standard Befunge-93 implementation is limited to the first 64 numbers in the series, after which the 8-bit memory cells will overflow. That range could be extended in Befunge-98, but realistically you're not likely to wait that long for the results.
 
<langsyntaxhighlight lang="befunge">v+1$$<_v#!`**::+1g42$$_v#<!`**::+1g43\g43::<<v,,.g42,<
>004p:0>1+24p:24g\:24g>>1+:34p::**24g::**+-|p>9,,,14v,
,,,"^3 + ^3= ^3 + ^3".\,,,9"= ".:\_v#g40g43<^v,,,,.g<^
5+,$$$\1+:38*`#@_\::"~"1+:24p34p0\0>14p24g04^>,04g.,,5</langsyntaxhighlight>
 
{{out}}
Line 197:
=={{header|C}}==
Using a priority queue to emit sum of two cubs in order. It's reasonably fast and doesn't use excessive amount of memory (the heap is only at 245 length upon the 2006th taxi).
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
 
Line 280:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 319:
=={{header|C++}}==
{{trans|C#}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iomanip>
#include <iostream>
Line 403:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:
Line 452:
 
=={{header|C sharp}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 527:
}
}
}</langsyntaxhighlight>
 
===Alternate Algorithm===
Based on the second Python example where only the sums are stored and sorted. Also shows the first 10 Taxicab Number triples.
<langsyntaxhighlight lang="csharp">using System; using static System.Console;
using System.Collections.Generic; using System.Linq;
 
Line 565:
dump(string.Format("\n\nFound {0} triple Taxicabs under {1}:", trips.Count, 2007), trips);
Write("\n\nElasped: {0}ms", (DateTime.Now - st).TotalMilliseconds); }
}</langsyntaxhighlight>
{{out}} (from TIO.run)
<pre>First 25 Taxicab Numbers, the 2000th, plus the next half-dozen:
Line 616:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">(ns test-project-intellij.core
(:gen-class))
 
Line 683:
(show-result n sample))
 
}</langsyntaxhighlight>
 
{{out}}
Line 724:
===High Level Version===
{{trans|Python}}
<langsyntaxhighlight lang="d">void main() /*@safe*/ {
import std.stdio, std.range, std.algorithm, std.typecons, std.string;
 
Line 742:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1729 = 1^3 + 12^3 = 9^3 + 10^3
Line 781:
===Heap-Based Version===
{{trans|Java}}
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.container;
 
struct CubeSum {
Line 845:
writeln;
}
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1729 = 10^3 + 9^3 = 12^3 + 1^3
Line 883:
===Low Level Heap-Based Version===
{{trans|C}}
<langsyntaxhighlight lang="d">struct Taxicabs {
alias CubesSumT = uint; // Or ulong.
 
Line 983:
'\n'.putchar;
}
}</langsyntaxhighlight>
{{out}}
<pre> 1: 1729 = 12^3 + 1^3 = 10^3 + 9^3
Line 1,021:
=={{header|DCL}}==
We invoke external utility SORT which I suppose technically speaking is not a formal part of the language but is darn handy at times;
<langsyntaxhighlight DCLlang="dcl">$ close /nolog sums_of_cubes
$ on control_y then $ goto clean
$ open /write sums_of_cubes sums_of_cubes.txt
Line 1,075:
$ clean:
$ close /nolog sorted_sums_of_cubes
$ close /nolog sums_of_cubes</langsyntaxhighlight>
{{out}}
<pre>$ @taxicab_numbers
Line 1,118:
=={{header|EchoLisp}}==
Using the '''heap''' library, and a heap to store the taxicab numbers. For taxi tuples - decomposition in more than two sums - we use the '''group''' function which transforms a list ( 3 5 5 6 8 ...) into ((3) (5 5) (6) ...).
<langsyntaxhighlight lang="scheme">
(require '(heap compile))
 
Line 1,167:
(for ((i (in-naturals nfrom)) (taxi (sublist taxis nfrom nto)))
(writeln (taxi->string i (first taxi)))))
</syntaxhighlight>
</lang>
 
{{out}}
<langsyntaxhighlight lang="scheme">
(define S (stack 'S)) ;; to push taxis
(define H (make-heap < )) ;; make min heap of all scubes
Line 1,214:
1660. 1148834232 = 846^3 + 816^3 = 920^3 + 718^3 = 1044^3 + 222^3
1837. 1407672000 = 1050^3 + 630^3 = 1104^3 + 396^3 = 1120^3 + 140^3
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Taxicab do
def numbers(n \\ 1200) do
(for i <- 1..n, j <- i..n, do: {i,j})
Line 1,231:
IO.puts "#{i+1} : #{inspect x}"
end
end)</langsyntaxhighlight>
 
{{out}}
Line 1,275:
{{works with|gforth|0.7.3}}
 
<langsyntaxhighlight lang="forth">variable taxicablist
variable searched-cubessum
73 constant max-constituent \ uses magic numbers
Line 1,330:
 
build-taxicablist
25 list-taxicabs</langsyntaxhighlight>
 
{{out}}
Line 1,363:
 
=={{header|Fortran}}==
<langsyntaxhighlight lang="fortran">
! A non-bruteforce approach
PROGRAM POOKA
Line 1,440:
END FUNCTION TAXICAB
</syntaxhighlight>
</lang>
{{out}}
<pre> Print first 25 numbers
Line 1,482:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">' version 11-10-2016
' compile with: fbc -s console
 
Line 1,581:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre> Print first 25 numbers
Line 1,622:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,734:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,781:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import Data.List (groupBy, sortOn, tails, transpose)
import Data.Function (on)
 
Line 1,818:
]
where
term r c l = ["(", show r, "^3=", show c, ")", l]</langsyntaxhighlight>
{{Out}}
<pre> 1. 1729 = ( 1^3= 1) + ( 12^3= 1728) or ( 9^3= 729) + ( 10^3= 1000)
Line 1,855:
=={{header|J}}==
 
<langsyntaxhighlight Jlang="j">cubes=: 3^~1+i.100 NB. first 100 cubes
triples=: /:~ ~. ,/ (+ , /:~@,)"0/~cubes NB. ordered pairs of cubes (each with their sum)
candidates=: ;({."#. <@(0&#`({.@{.(;,)<@}."1)@.(1<#))/. ])triples
Line 1,911:
├──┼──────┼────────────┼─────────────┤
│25│402597│74088 328509│175616 226981│
└──┴──────┴────────────┴─────────────┘</langsyntaxhighlight>
 
Explanation:
Line 1,929:
Extra credit:
 
<langsyntaxhighlight Jlang="j"> x:each 7 {. 1999 }. (,.~ <@>:@i.@#) ;({."#. <@(0&#`({.@{.(;,)<@}."1)@.(1<#))/. ])/:~~.,/(+,/:~@,)"0/~3^~1+i.10000
┌────┬──────────┬────────────────────┬────────────────────┬┐
│2000│1671816384│78402752 1593413632 │830584000 841232384 ││
Line 1,944:
├────┼──────────┼────────────────────┼────────────────────┼┤
│2006│1677646971│970299 1676676672 │707347971 970299000 ││
└────┴──────────┴────────────────────┴────────────────────┴┘</langsyntaxhighlight>
 
The extra blank box at the end is because when tackling this large of a data set, some sums can be achieved by three different pairs of cubes.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.List;
Line 2,023:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,061:
 
=={{header|JavaScript}}==
<langsyntaxhighlight JavaScriptlang="javascript">var n3s = [],
s3s = {}
for (var n = 1, e = 1200; n < e; n += 1) n3s[n] = n * n * n
Line 2,087:
}
document.write('<br>')
}</langsyntaxhighlight>
{{out}}
1: 1729 = 1<sup>3</sup>+12<sup>3</sup> = 9<sup>3</sup>+10<sup>3</sup>
Line 2,135:
{{works with|jq|1.4}}
 
<langsyntaxhighlight lang="jq"># Output: an array of the form [i^3 + j^3, [i, j]] sorted by the sum.
# Only cubes of 1 to ($in-1) are considered; the listing is therefore truncated
# as it might not capture taxicab numbers greater than $in ^ 3.
Line 2,173:
| [ ., [taxicabs0] ]
| until( .[1] | length >= $m; (.[0] + $increment) | [., [taxicabs0]] )
| .[1][0:$n] ;</langsyntaxhighlight>
 
'''The task'''
<langsyntaxhighlight lang="jq">2006 | taxicabs as $t
| (range(0;25), range(1999;2006)) as $i
| "\($i+1): \($t[$i][0]) ~ \($t[$i][1]) and \($t[$i][2])"</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -n -r -f Taxicab_numbers.jq
1: 1729 ~ [1,12] and [9,10]
2: 4104 ~ [2,16] and [9,15]
Line 2,212:
2004: 1675958167 ~ [492,1159] and [711,1096]
2005: 1676926719 ~ [63,1188] and [714,1095]
2006: 1677646971 ~ [99,1188] and [891,990]</langsyntaxhighlight>
 
=={{header|Julia}}==
{{trans|Python}}
<langsyntaxhighlight lang="julia">using Printf, DataStructures, IterTools
 
function findtaxinumbers(nmax::Integer)
Line 2,269:
end
 
findtaxinumbers(1200)</langsyntaxhighlight>
 
{{out}}
Line 2,340:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.0.6
 
import java.util.PriorityQueue
Line 2,400:
println()
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,439:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">sums, taxis, limit = {}, {}, 1200
for i = 1, limit do
for j = 1, i-1 do
Line 2,456:
end
end
print("* n=3")</langsyntaxhighlight>
{{out}}
<pre> 1 : 1729 = 10^3 + 9^3 = 12^3 + 1^3
Line 2,503:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">findTaxi[n_] := Sort[Keys[Select[Counts[Flatten[Table[x^3 + y^3, {x, 1, n}, {y, x, n}]]], GreaterThan[1]]]];
Take[findTaxiNumbers[100], 25]
found=findTaxiNumbers[1200][[2000 ;; 2005]]
Map[Reduce[x^3 + y^3 == # && x >= y && x > 0 && y > 0, {x, y}, Integers] &, found]</langsyntaxhighlight>
{{out}}
<pre>{1729, 4104, 13832, 20683, 32832, 39312, 40033, 46683, 64232, 65728, 110656, 110808, 134379, 149389, 165464, 171288, 195841, 216027, 216125, 262656, 314496, 320264, 327763, 373464, 402597}
Line 2,525:
Execution time is excellent: about 45 ms on our laptop (I5).
 
<langsyntaxhighlight Nimlang="nim">import heapqueue, strformat
 
type
Line 2,569:
for s in t:
stdout.write &" = {s.x:4}^3 + {s.y:4}^3"
echo()</langsyntaxhighlight>
 
{{out}}
Line 2,606:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">taxicab(n)=my(t); for(k=sqrtnint((n-1)\2,3)+1, sqrtnint(n,3), if(ispower(n-k^3, 3), if(t, return(1), t=1))); 0;
cubes(n)=my(t); for(k=sqrtnint((n-1)\2,3)+1, sqrtnint(n,3), if(ispower(n-k^3, 3, &t), print(n" = \t"k"^3\t+ "t"^3")))
select(taxicab, [1..402597])
apply(cubes, %);</langsyntaxhighlight>
{{out}}
<pre>%1 = [1729, 4104, 13832, 20683, 32832, 39312, 40033, 46683, 64232, 65728, 110656, 110808, 134379, 149389, 165464, 171288, 195841, 216027, 216125, 262656, 314496, 320264, 327763, 373464, 402597]
Line 2,673:
Its impressive, that over all one check takes ~3.5 cpu-cycles on i4330 3.5Ghz
 
<langsyntaxhighlight lang="pascal">program taxiCabNo;
uses
sysutils;
Line 2,808:
writeln('count of solutions ',AllSolHigh);
end.
</syntaxhighlight>
</lang>
<pre> 1 1729 = 12^3 + 1^3 = 10^3 + 9^3
2 4104 = 16^3 + 2^3 = 15^3 + 9^3
Line 2,827:
Uses segmentation so memory use is constrained as high values are searched for. Also has parameter to look for Ta(3) and Ta(4) numbers (which is when segmentation is really needed). By default shows the first 25 numbers; with one argument shows that many; with two arguments shows results in the range.
 
<langsyntaxhighlight lang="perl">my($beg, $end) = (@ARGV==0) ? (1,25) : (@ARGV==1) ? (1,shift) : (shift,shift);
 
my $lim = 1e14; # Ought to be dynamic as should segment size
Line 2,880:
@retlist = sort { $a->[0] <=> $b->[0] } @retlist;
return @retlist;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,921:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
{{trans|Raku}}
<span style="color: #000080;font-style:italic;">-- demo\rosetta\Taxicab_numbers.exw</span>
Uses a dictionary to map sum of cubes to either the first/only pair or an integer index into the result set.
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Turned out to be a fair bit slower (15s) than I first expected.
<span style="color: #008080;">function</span> <span style="color: #000000;">cube_sums</span><span style="color: #0000FF;">()</span>
<lang Phix>function get_taxis(integer last)
<span style="color: #000080;font-style:italic;">// create cubes and sorted list of cube sums</span>
sequence taxis = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">cubes</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">sums</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
integer c1 = 1, maxc1 = 0, c2
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">1189</span> <span style="color: #008080;">do</span>
atom c3, h3 = 0
<span style="color: #004080;">atom</span> <span style="color: #000000;">cube</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">i</span> <span style="color: #0000FF;">*</span> <span style="color: #000000;">i</span>
while maxc1=0 or c1<maxc1 do
<span style="color: #000000;">sums</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cubes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cube</span><span style="color: #0000FF;">)</span>
c3 = power(c1,3)
<span style="color: #000000;">cubes</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">cube</span>
for c2 = 1 to c1 do
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
atom this = power(c2,3)+c3
<span style="color: #000000;">sums</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- (706,266 in total)</span>
integer node = getd_index(this)
<span style="color: #008080;">return</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">cubes</span><span style="color: #0000FF;">,</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">}</span>
if node=NULL then
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
setd(this,{c2,c1})
else
if this>h3 then h3 = this end if
object data = getd_by_index(node)
if not integer(data) then
taxis = append(taxis,{this,{data}})
data = length(taxis)
setd(this,data)
if data=last then
maxc1 = ceil(power(h3,1/3))
end if
end if
taxis[data][2] &= {{c2,c1}}
end if
end for
c1 += 1
end while
destroy_dict(1,justclear:=true)
taxis = sort(taxis)
return taxis
end function
 
sequence taxis = get_taxis(2006)
constant sets = {{1,25},{2000,2006}}
for s=1 to length(sets) do
integer {first,last} = sets[s]
for i=first to last do
printf(1,"%d: %d: %s\n",{i,taxis[i][1],sprint(taxis[i][2])})
end for
end for</lang>
{{out}}
<pre>
1: 1729: {{9,10},{1,12}}
2: 4104: {{9,15},{2,16}}
3: 13832: {{18,20},{2,24}}
4: 20683: {{19,24},{10,27}}
5: 32832: {{18,30},{4,32}}
6: 39312: {{15,33},{2,34}}
7: 40033: {{16,33},{9,34}}
8: 46683: {{27,30},{3,36}}
9: 64232: {{26,36},{17,39}}
10: 65728: {{31,33},{12,40}}
11: 110656: {{36,40},{4,48}}
12: 110808: {{27,45},{6,48}}
13: 134379: {{38,43},{12,51}}
14: 149389: {{29,50},{8,53}}
15: 165464: {{38,48},{20,54}}
16: 171288: {{24,54},{17,55}}
17: 195841: {{22,57},{9,58}}
18: 216027: {{22,59},{3,60}}
19: 216125: {{45,50},{5,60}}
20: 262656: {{36,60},{8,64}}
21: 314496: {{30,66},{4,68}}
22: 320264: {{32,66},{18,68}}
23: 327763: {{51,58},{30,67}}
24: 373464: {{54,60},{6,72}}
25: 402597: {{56,61},{42,69}}
2000: 1671816384: {{940,944},{428,1168}}
2001: 1672470592: {{632,1124},{29,1187}}
2002: 1673170856: {{828,1034},{458,1164}}
2003: 1675045225: {{744,1081},{522,1153}}
2004: 1675958167: {{711,1096},{492,1159}}
2005: 1676926719: {{714,1095},{63,1188}}
2006: 1677646971: {{891,990},{99,1188}}
</pre>
 
{{trans|C}}
Using a [[Priority_queue#Phix|priority queue]], otherwise based on C, quite a bit (18.5x) faster.<br>
Copes with 40000..6, same results as Go, though that increases the runtime from 0.8s to 1min 15s.
<lang Phix>sequence cubes = {}
<span style="color: #004080;">sequence</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">cubes</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">sums</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cube_sums</span><span style="color: #0000FF;">()</span>
procedure add_cube()
integer n = length(cubes)+1
cubes = append(cubes,n*n*n)
pq_add({{n,1},cubes[n]+1})
end procedure
constant VALUE = PRIORITY
 
function next_sum()
while length(pq)<=2 or pq[1][VALUE]>=cubes[$] do add_cube() end while
sequence res = pq_pop()
integer {x,y} = res[DATA]
y += 1
if y<x then
pq_add({{x,y},cubes[x]+cubes[y]})
end if
return res
end function
function next_taxi()
sequence top
while 1 do
top = next_sum()
if pq[1][VALUE]=top[VALUE] then exit end if
end while
sequence res = {top}
atom v = top[PRIORITY]
while 1 do
top = next_sum()
res = append(res,top[DATA])
if pq[1][VALUE]!=v then exit end if
end while
return res
end function
<span style="color: #004080;">atom</span> <span style="color: #000000;">nm1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span>
for i=1 to 2006 do
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">]</span>
sequence x = next_taxi()
<span style="color: #004080;">integer</span> <span style="color: #000000;">idx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
if i<=25 or i>=2000 then
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"First 25 Taxicab Numbers, 2000..2006th, and all interim triples:\n"</span><span style="color: #0000FF;">)</span>
atom v = x[1][VALUE]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">3</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
x[1] = x[1][DATA]
<span style="color: #004080;">atom</span> <span style="color: #000000;">np1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
string y = sprintf("%11d+%-10d",sq_power(x[1],3))
<span style="color: #008080;">if</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">np1</span> <span style="color: #008080;">and</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">nm1</span> <span style="color: #008080;">then</span>
for j=2 to length(x) do
<span style="color: #008080;">if</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">25</span>
y &= sprintf(",%11d+%-10d",sq_power(x[j],3))
<span style="color: #008080;">or</span> <span style="color: #0000FF;">(</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">>=</span><span style="color: #000000;">2000</span> <span style="color: #008080;">and</span> <span style="color: #000000;">idx</span><span style="color: #0000FF;"><=</span><span style="color: #000000;">2006</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">or</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">sums</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #008080;">then</span>
printf(1,"%4d: %10d: %-23s [%s]\n",{i,v,sprint(x),y})
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
end if
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">cubes</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
end for</lang>
<span style="color: #004080;">atom</span> <span style="color: #000000;">x</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">cubes</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">y</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">-</span><span style="color: #000000;">x</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">y</span><span style="color: #0000FF;"><</span><span style="color: #000000;">x</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">ydx</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">binary_search</span><span style="color: #0000FF;">(</span><span style="color: #000000;">y</span><span style="color: #0000FF;">,</span><span style="color: #000000;">cubes</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ydx</span><span style="color: #0000FF;">></span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"(%3d^3=%9d) + (%4d^3=%10d)"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">j</span><span style="color: #0000FF;">,</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ydx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">y</span><span style="color: #0000FF;">}))</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%4d %10d = %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">idx</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" or "</span><span style="color: #0000FF;">)})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">idx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">nm1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">n</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">np1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
First 25 Taxicab Numbers, 2000..2006th, and all interim triples:
1: 1729: {{10,9},{12,1}} [ 1000+729 , 1728+1 ]
2:1 4104:1729 {{15,9},{16,2}}= ( 1^3= [ 1) + ( 12^3= 3375+729 1728) or ,( 9^3= 4096 729) +8 ( 10^3= ]1000)
3:2 13832: {{20,18},{24,4104 = ( 2}}^3= [8) + ( 16^3= 8000+5832 4096) or ( , 9^3= 13824 729) +8 ( 15^3= ]3375)
4:3 20683:13832 {{24,19},{27,10}}= ( 2^3= [ 8) 13824+6859 ( 24^3= , 13824) or ( 18^3= 5832) 19683+1000 ( 20^3= ]8000)
5:4 32832:20683 {{30,18},{32,4}}= ( 10^3= 1000) [+ ( 27^3= 27000+5832 19683) or ( ,19^3= 6859) 32768+64 ( 24^3= ]13824)
6:5 39312:32832 {{33,15},{34,2}}= ( 4^3= [ 64) + ( 35937+337532^3= 32768) ,or ( 18^3= 39304+8 5832) + ( 30^3= ]27000)
7:6 40033:39312 {{33,16},{34,9}}= ( 2^3= [ 8) + ( 35937+4096 34^3= ,39304) or ( 15^3= 39304 3375) +729 ( 33^3= ] 35937)
8:7 46683:40033 {{30,27},{36,3}}= ( 9^3= [ 729) + ( 34^3= 27000+19683 39304) ,or ( 16^3= 46656 4096) +27 ( 33^3= ]35937)
9:8 64232:46683 {{39,17},{36,26}}= ( 3^3= [ 27) + ( 59319+4913 36^3= ,46656) or ( 27^3= 46656 19683) +17576 ( 30^3= ]27000)
10: 9 65728: {{40,12},{33,31}}64232 = ( 17^3= [ 4913) + ( 39^3= 64000+1728 59319) or ,( 26^3= 17576) 35937+29791 ( 36^3= ]46656)
11:10 110656: {{40,36},{48,4}}65728 = ( 12^3= [1728) + ( 40^3= 64000+46656 64000) or ( ,31^3= 29791) 110592+64 ( 33^3= ]35937)
12:11 110808:110656 {{45,27},{48,6}}= ( 4^3= [ 64) + ( 91125+1968348^3= 110592) ,or ( 36^3= 110592 46656) +216 ( 40^3= ]64000)
13:12 134379:110808 {{51,12},{43,38}}= ( 6^3= [ 216) + ( 132651+1728 48^3= 110592) ,or ( 27^3= 7950719683) +54872 ( 45^3= ]91125)
14:13 149389:134379 {{50,29},{53,8}}= ( 12^3= 1728) [+ ( 51^3= 125000+24389 132651) or ( ,38^3= 54872) 148877+512 ( 43^3= ]79507)
15:14 165464:149389 {{48,38},{54,20}}= ( 8^3= [ 512) + ( 110592+54872 53^3= ,148877) or ( 29^3= 157464 24389) +8000 ( 50^3= ]125000)
16:15 171288:165464 {{54,24},{55,17}}= ( 20^3= [8000) + ( 54^3= 157464+13824 157464) or ,( 38^3= 16637554872) +4913 ( 48^3= ]110592)
17:16 195841:171288 {{57,22},{58,9}}= ( 17^3= 4913) [+ ( 55^3= 185193+10648 166375) or ( ,24^3= 13824) 195112+729 ( 54^3= ]157464)
18:17 216027:195841 {{59,22},{60,3}}= ( 9^3= [ 729) + ( 205379+1064858^3= 195112) ,or ( 22^3= 216000+27 10648) + ( 57^3= ]185193)
19:18 216125:216027 {{50,45},{60,5}}= ( 3^3= [ 27) + ( 125000+91125 60^3= ,216000) or ( 22^3= 216000 10648) +125 ( 59^3= ]205379)
20:19 262656:216125 {{60,36},{64,8}}= ( 5^3= [ 125) + ( 216000+4665660^3= 216000) or ( ,45^3= 91125) 262144+512 ( 50^3= ]125000)
21:20 314496:262656 {{66,30},{68,4}}= ( 8^3= [ 512) + ( 287496+2700064^3= 262144) ,or ( 36^3= 314432+64 46656) + ( 60^3= ]216000)
22:21 320264:314496 {{68,18},{66,32}}= ( 4^3= [ 64) 314432+5832 ( 68^3= , 314432) or ( 30^3= 27000) 287496+32768 ( 66^3= ] 287496)
23:22 327763:320264 {{67,30},{58,51}}= ( 18^3= [5832) + ( 68^3= 300763+27000 314432) or ,( 32^3= 19511232768) +132651 ( 66^3= ]287496)
24:23 373464:327763 {{60,54},{72,6}}= ( 30^3= 27000) + [( 67^3= 216000+157464 300763) or ,( 51^3= 132651) 373248+216 ( 58^3= ]195112)
25:24 402597:373464 {{69,42},{61,56}}= ( 6^3= [ 216) + ( 328509+74088 72^3= ,373248) or ( 54^3= 226981 157464) +175616 ( 60^3= ]216000)
25 402597 = ( 42^3= 74088) + ( 69^3= 328509) or ( 56^3= 175616) + ( 61^3= 226981)
2000: 1671816384: {{1168,428},{944,940}} [ 1593413632+78402752 , 841232384+830584000 ]
455 87539319 = (167^3= 4657463) + ( 436^3= 82881856) or (228^3= 11852352) + ( 423^3= 75686967) or (255^3= 16581375) + ( 414^3= 70957944)
2001: 1672470592: {{1124,632},{1187,29}} [ 1420034624+252435968 , 1672446203+24389 ]
535 119824488 = ( 11^3= 1331) + ( 493^3= 119823157) or ( 90^3= 729000) + ( 492^3= 119095488) or (346^3= 41421736) + ( 428^3= 78402752)
2002: 1673170856: {{1164,458},{1034,828}} [ 1577098944+96071912 , 1105507304+567663552 ]
588 143604279 = (111^3= 1367631) + ( 522^3= 142236648) or (359^3= 46268279) + ( 460^3= 97336000) or (408^3= 67917312) + ( 423^3= 75686967)
2003: 1675045225: {{1153,522},{1081,744}} [ 1532808577+142236648 , 1263214441+411830784 ]
655 175959000 = ( 70^3= 343000) + ( 560^3= 175616000) or (198^3= 7762392) + ( 552^3= 168196608) or (315^3= 31255875) + ( 525^3= 144703125)
2004: 1675958167: {{1159,492},{1096,711}} [ 1556862679+119095488 , 1316532736+359425431 ]
888 327763000 = (300^3= 27000000) + ( 670^3= 300763000) or (339^3= 38958219) + ( 661^3= 288804781) or (510^3=132651000) + ( 580^3= 195112000)
2005: 1676926719: {{1095,714},{1188,63}} [ 1312932375+363994344 , 1676676672+250047 ]
1299 700314552 = (334^3= 37259704) + ( 872^3= 663054848) or (456^3= 94818816) + ( 846^3= 605495736) or (510^3=132651000) + ( 828^3= 567663552)
2006: 1677646971: {{990,891},{1188,99}} [ 970299000+707347971 , 1676676672+970299 ]
1398 804360375 = ( 15^3= 3375) + ( 930^3= 804357000) or (198^3= 7762392) + ( 927^3= 796597983) or (295^3= 25672375) + ( 920^3= 778688000)
1515 958595904 = ( 22^3= 10648) + ( 986^3= 958585256) or (180^3= 5832000) + ( 984^3= 952763904) or (692^3=331373888) + ( 856^3= 627222016)
1660 1148834232 = (222^3= 10941048) + (1044^3=1137893184) or (718^3=370146232) + ( 920^3= 778688000) or (816^3=543338496) + ( 846^3= 605495736)
1837 1407672000 = (140^3= 2744000) + (1120^3=1404928000) or (396^3= 62099136) + (1104^3=1345572864) or (630^3=250047000) + (1050^3=1157625000)
2000 1671816384 = (428^3= 78402752) + (1168^3=1593413632) or (940^3=830584000) + ( 944^3= 841232384)
2001 1672470592 = ( 29^3= 24389) + (1187^3=1672446203) or (632^3=252435968) + (1124^3=1420034624)
2002 1673170856 = (458^3= 96071912) + (1164^3=1577098944) or (828^3=567663552) + (1034^3=1105507304)
2003 1675045225 = (522^3=142236648) + (1153^3=1532808577) or (744^3=411830784) + (1081^3=1263214441)
2004 1675958167 = (492^3=119095488) + (1159^3=1556862679) or (711^3=359425431) + (1096^3=1316532736)
2005 1676926719 = ( 63^3= 250047) + (1188^3=1676676672) or (714^3=363994344) + (1095^3=1312932375)
2006 1677646971 = ( 99^3= 970299) + (1188^3=1676676672) or (891^3=707347971) + ( 990^3= 970299000)
</pre>
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(load "@lib/simul.l")
 
(off 'B)
Line 3,105 ⟶ 3,030:
(println (car L) (caddr L)) )
(for L (head 7 (nth R 2000))
(println (car L) (caddr L)) )</langsyntaxhighlight>
 
{{out}}
Line 3,144 ⟶ 3,069:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">#MAX=1189
 
Macro q3(a,b)
Line 3,182 ⟶ 3,107:
Next
PrintN("FIN.") : Input()
EndIf</langsyntaxhighlight>{{out}}
<pre> 1: 1729= 1³ + 12³ = 9³ + 10³
2: 4104= 2³ + 16³ = 9³ + 15³
Line 3,219 ⟶ 3,144:
=={{header|Python}}==
(Magic number 1201 found by trial and error)
<langsyntaxhighlight lang="python">from collections import defaultdict
from itertools import product
from pprint import pprint as pp
Line 3,235 ⟶ 3,160:
print('...')
for t in enumerate(taxied[2000-1:2000+6], 2000):
pp(t)</langsyntaxhighlight>
 
{{out}}
Line 3,273 ⟶ 3,198:
 
Although, for this task it's simply faster to look up the cubes in the sum when we need to print them, because we can now store and sort only the sums:
<langsyntaxhighlight lang="python">cubes, crev = [x**3 for x in range(1,1200)], {}
# for cube root lookup
for x,x3 in enumerate(cubes): crev[x3] = x + 1
Line 3,292 ⟶ 3,217:
print "%4d: %10d"%(idx,n),
for x in p: print " = %4d^3 + %4d^3"%x,
print</langsyntaxhighlight>
{{out}}Output trimmed to reduce clutter.
<pre>
Line 3,308 ⟶ 3,233:
===Using heapq module===
A priority queue that holds cube sums. When consecutive sums come out with the same value, they are taxis.
<langsyntaxhighlight lang="python">from heapq import heappush, heappop
 
def cubesum():
Line 3,337 ⟶ 3,262:
if n >= 2006: break
if n <= 25 or n >= 2000:
print(n, x)</langsyntaxhighlight>
{{out}}
<pre>
Line 3,376 ⟶ 3,301:
This is the straighforward implementation, so it finds
only the first 25 values in a sensible amount of time.
<langsyntaxhighlight Racketlang="racket">#lang racket
 
(define (cube x) (* x x x))
Line 3,402 ⟶ 3,327:
(when (< p 25)
(loop (add1 p) (add1 n))))
(loop p (add1 n)))))</langsyntaxhighlight>
{{out}}
<pre>1: 1729 = 1^3 + 12^3 = 9^3 + 10^3
Line 3,434 ⟶ 3,359:
 
This uses a pretty simple search algorithm that doesn't necessarily return the Taxicab numbers in order. Assuming we want all the Taxicab numbers within some range S to N, we'll search until we find N values. When we find the Nth value, we continue to search up to the cube root of the largest Taxicab number found up to that point. That ensures we will find all of them inside the desired range without needing to search arbitrarily or use magic numbers. Defaults to returning the Taxicab numbers from 1 to 25. Pass in a different start and end value if you want some other range.
<syntaxhighlight lang="raku" perl6line>constant @cu = (^Inf).map: { .³ }
 
sub MAIN ($start = 1, $end = 25) {
Line 3,464 ⟶ 3,389:
(.value.map({ sprintf "%4d³ + %-s\³", |$_ })).join: ",\t"
for %this_stuff.grep( { $_.value.elems > 1 } ).sort( +*.key )[$start-1..$end-1];
}</langsyntaxhighlight>
{{out}}With no passed parameters (default):
<pre> 1 1729 => 9³ + 10³, 1³ + 12³
Line 3,502 ⟶ 3,427:
=={{header|REXX}}==
Programming note: &nbsp; to ensure that the taxicab numbers are in order, an extra 10% are generated.
<langsyntaxhighlight lang="rexx">/*REXX program displays the specified first (lowest) taxicab numbers (for three ranges).*/
parse arg L.1 H.1 L.2 H.2 L.3 H.3 . /*obtain optional arguments from the CL*/
if L.1=='' | L.1=="," then L.1= 1 /*L1 is the low part of 1st range. */
Line 3,549 ⟶ 3,474:
end /*forever*/
end /*i*/
end /*while h>1*/; return</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 3,592 ⟶ 3,517:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Taxicab numbers
 
Line 3,618 ⟶ 3,543:
next
see "ok" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,647 ⟶ 3,572:
25 402597 => 56³ + 61³, 42³ + 69³
ok
</pre>
 
=={{header|RPL}}==
A priority queue allows to get the first 25th taxicab numbers without being killed by the emulator timedog.
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! Code
! Comments
|-
|
{} 1 PQueue SIZE FOR j
IF OVER j ≠ THEN PQueue j GET 1 →LIST + END
NEXT
'PQueue' STO DROP
≫ 'PQPop' STO
0 {} → idx item
≪ 0 1 PQueue SIZE FOR j
PQueue j GET 1 GET
IF DUP2 <
THEN SWAP PQueue j GET 'item' STO j 'idx' STO
END DROP
NEXT
DROP idx item
≫ ≫ 'PQMax' STO
≪ → pos
1 CF PQueue SIZE WHILE DUP 1 FC? AND REPEAT
IF PQueue OVER GET pos GET 3 PICK == THEN 1 SF END
1 -
END
DROP2 1 FS?
≫ ≫ 'PQin?' STO
≪ OVER 3 ^ OVER 3 ^ + ROT ROT
3 →LIST 1 →LIST PQueue + 'PQueue' STO
≫ 'PQAdd' STO
≪ → item
≪ IF item 1 GET LastItem 1 GET == THEN
{} item 1 GET + item 2 GET item 3 GET R→C +
LastItem 2 GET LastItem 3 GET R→C + 1 →LIST
TaxiNums + 'TaxiNums' STO
END
item 'LastItem' STO
≫ ≫ 'StoreIfTwice' STO
≪ WHILE PQueue SIZE REPEAT
PQMax DUP StoreIfTwice LIST→ DROP 4 ROLL PQPop → r c
≪ IF r 1 >
THEN IF r 1 - 2 PQin? NOT
THEN r 1 - c PQAdd END END
IF c 1 > c r > AND
THEN IF c 1 - 3 PQin? NOT
THEN r c 1 - PQAdd END
END
DROP
END
≫ 'TAXI' STO
≪ → n
≪ { 0 0 0 } 'LastItem' STO
{ } DUP 'PQueue' STO 'TaxiNums' STO n n PQAdd
≫ ≫ 'INIT 'STO
|
''( j -- )''
Scan the priority queue
Copy all items except one
''( -- index { PQ_item } )''
Look for PQ item with max value
''( x pos -- boolean )''
''( r, c -- { PQ_item } )''
''( x pos -- boolean )''
PQ item = { r^3+c^3 r c }
''( -- )''
Removing max value from queue - and store it if déjà vu
can we add an item from the row above?
can we add an item from the left column and above diagonal?
''( magic_number -- )''
initialize global variables
|}
The following commands deliver what is required:
70 INIT TAXI
TaxiNums
{{out}}
<pre>
1: { { 1729 (9,10) (1,12) } { 4104 (9,15) (2,16) } { 13832 (18,20) (2,24) } { 20683 (19,24) (10,27) } { 32832 (18,30) (4,32) } { 39312 (15,33) (2,34) } { 40033 (16,33) (9,34) } { 46683 (27,30) (3,36) } { 64232 (26,36) (17,39) } { 65728 (31,33) (12,40) } { 110656 (36,40) (4,48) } { 110808 (27,45) (6,48) } { 134379 (38,43) (12,51) } { 149389 (29,50) (8,53) } { 165464 (38,48) (20,54) } { 171288 (24,54) (17,55) } { 195841 (22,57) (9,58) } { 216027 (22,59) (3,60) } { 216125 (45,50) (5,60) } { 262656 (36,60) (8,64) } { 314496 (30,66) (4,68) } { 320264 (32,66) (18,68) } { 327763 (51,58) (30,67) } { 402597 (56,61) (42,69) } }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">def taxicab_number(nmax=1200)
[*1..nmax].repeated_combination(2).group_by{|x,y| x**3 + y**3}.select{|k,v| v.size>1}.sort
end
Line 3,658 ⟶ 3,714:
[*1..25, *2000...2007].each do |i|
puts "%4d: %10d" % [i, t[i][0]] + t[i][1].map{|a| " = %4d**3 + %4d**3" % a}.join
end</langsyntaxhighlight>
{{out}}
<pre>
Line 3,696 ⟶ 3,752:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">
use std::collections::HashMap;
use itertools::Itertools;
Line 3,730 ⟶ 3,786:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 3,758 ⟶ 3,814:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">import scala.mathcollection.powMapView
import scala.math.pow
 
implicit class Pairs[A, B]( p:List[(A, B)]) {
def collectPairs: MapMapView[A, List[B]] = p.groupBy(_._1).view.mapValues(_.map(_._2)).filterNot(_._2.size<2)
}
 
Line 3,772 ⟶ 3,829:
case _ => 0 ->(0, 0)
} // Turn the list into the sum of two cubes and
// remember what we started with, eg. 28->(1,3)
.collectPairs // Only keep taxi cab numbers with a duplicate
.toList.sortBy(_._1) // Sort the results
Line 3,788 ⟶ 3,845:
case (p, a::b::Nil) => println( "%20d\t(%d\u00b3 + %d\u00b3)\t(%d\u00b3 + %d\u00b3)".format(p,a._1,a._2,b._1,b._2) )
}
}</syntaxhighlight>
}
</lang>
{{out}}
<pre>
Line 3,831 ⟶ 3,887:
{{libheader|Scheme/SRFIs}}
 
<langsyntaxhighlight lang="scheme">
(import (scheme base)
(scheme write)
Line 3,867 ⟶ 3,923:
(iota 7 1999))
)
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,907 ⟶ 3,963:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang="ruby">var (start=1, end=25) = ARGV.map{.to_i}...
 
func display (h, start, end) {
Line 3,935 ⟶ 3,991:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,978 ⟶ 4,034:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">extension Array {
func combinations(_ k: Int) -> [[Element]] {
return Self._combinations(slice: self[startIndex...], k)
Line 4,025 ⟶ 4,081:
for taxi in res[1999..<2006] {
print(taxi)
}</langsyntaxhighlight>
 
{{out}}
Line 4,068 ⟶ 4,124:
{{works with|Tcl|8.6}}
{{trans|Python}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.6
 
proc heappush {heapName item} {
Line 4,120 ⟶ 4,176:
for {set n 2000} {$n <= 2006} {incr n} {
puts ${n}:[join [lmap t [taxi $n] {format " %d = %d$3 + %d$3" {*}$t}] ","]
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,158 ⟶ 4,214:
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">Public Type tuple
i As Variant
j As Variant
Line 4,291 ⟶ 4,347:
If (arrLbound < tmpHi) Then Quicksort vArray, arrLbound, tmpHi 'conquer
If (tmpLow < arrUbound) Then Quicksort vArray, tmpLow, arrUbound 'conquer
End Sub</langsyntaxhighlight>{{out}}
<pre> 4399 taxis
1 1729 = 9^3 + 10^3 = 1^3 + 12^3
Line 4,355 ⟶ 4,411:
=={{header|Visual Basic .NET}}==
{{trans|C#}}
<langsyntaxhighlight lang="vbnet">
Imports System.Text
 
Line 4,413 ⟶ 4,469:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre> 1 1729 = 10^3 + 9^3 = 12^3 + 1^3
Line 4,451 ⟶ 4,507:
{{libheader|Wren-sort}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight ecmascriptlang="wren">import "./sort" for Sort
import "./fmt" for Fmt
 
var cubesSum = {}
Line 4,484 ⟶ 4,540:
var t = taxicabs[i-1]
Fmt.lprint("$,5d: $,13d = $3d³ + $,5d³ = $3d³ + $,5d³", [i, t[0], t[1][0], t[1][1], t[2][0], t[2][1]])
}</langsyntaxhighlight>
 
{{out}}
Line 4,527 ⟶ 4,583:
=={{header|XPL0}}==
Slow, brute force solution.
<langsyntaxhighlight XPL0lang="xpl0">int N, I, J, SI, SJ, Count, Tally;
[Count:= 0; N:= 0;
repeat Tally:= 0;
Line 4,553 ⟶ 4,609:
N:= N+1;
until Count >= 25;
]</langsyntaxhighlight>
 
{{out}}
Line 4,587 ⟶ 4,643:
{{trans|D}}
An array of bytes is used to hold n, where array[n³+m³]==n.
<langsyntaxhighlight lang="zkl">fcn taxiCabNumbers{
const HeapSZ=0d5_000_000;
iCubes:=[1..120].apply("pow",3);
Line 4,604 ⟶ 4,660:
}
taxiNums.sort(fcn([(a,_)],[(b,_)]){ a<b })
}</langsyntaxhighlight>
<langsyntaxhighlight 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())
Line 4,611 ⟶ 4,667:
}
taxiNums:=taxiCabNumbers(); // 63 pairs
taxiNums[0,25]:print(1,_);</langsyntaxhighlight>
{{out}}
<pre>
Line 4,642 ⟶ 4,698:
{{trans|Python}}
Using a binary heap:
<langsyntaxhighlight lang="zkl">fcn cubeSum{
heap,n:=Heap(fcn([(a,_)],[(b,_)]){ a<=b }), 1; // heap cnt maxes out @ 244
while(1){
Line 4,671 ⟶ 4,727:
if(n >= 2006) break;
if(n <= 25 or n >= 2000) println(n,": ",x);
}</langsyntaxhighlight>
And a quickie heap implementation:
<langsyntaxhighlight lang="zkl">class Heap{ // binary heap
fcn init(lteqFcn='<=){
var [const, private] heap=List().pad(64,Void); // a power of 2
Line 4,710 ⟶ 4,766:
var [proxy] top=fcn { if(cnt==0) Void else heap[0] };
var [proxy] empty=fcn{ (not cnt) };
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,731 ⟶ 4,787:
You cannot fit the whole 1625-entry table of cubes (and this program on top) into the 16K ZX Spectrum. Replace all 1625s with 1200s to resolve; numerically unjustified as an exhaustive search, but we know this will be sufficient to find the 2006th number. Eventually.
 
<langsyntaxhighlight lang="zxbasic">10 DIM f(1625): REM populating a cube table at the start will be faster than computing the cubes on the fly
20 FOR x=1 TO 1625
30 LET f(x)=x*x*x: REM x*x*x rather than x^3 as the ZX Spectrum's exponentiation function is legendarily slow
Line 4,766 ⟶ 4,822:
340 NEXT n
350 NEXT m
360 NEXT x</langsyntaxhighlight>
 
{{out}}
Line 4,784 ⟶ 4,840:
This program produces the first 25 Taxicab numbers. It is written with speed in mind.
The runtime is about 45 minutes on a ZX Spectrum (3.5 Mhz).
<langsyntaxhighlight lang="zxbasic"> 10 LET T=0: DIM F(72): LET D=0: LET S=0: LET B=0: LET A=0: LET C=0
20 DIM H(50): DIM Y(50,2): FOR D=1 TO 72: LET F(D)=D*D*D: NEXT D
30 FOR A=1 TO 58: FOR B=A+1 TO 72: LET S=F(A)+F(B): FOR D=B-1 TO A STEP -1
Line 4,804 ⟶ 4,860:
151 LPRINT T;"^3+";Y(A,2)-T*65536;"^3"
160 NEXT A: PRINT
170 STOP</langsyntaxhighlight>
{{out}}
<pre>1:1729=1^3+12^3=9^3+10^3
9,476

edits