Taxicab numbers: Difference between revisions

m (Added Delphi reference to Pascal code)
Line 2,246:
(x == 1096 && y == 711) || (x == 1159 && y == 492),
(x == 1095 && y == 714) || (x == 1188 && y == 63)}</pre>
 
=={{header|Nim}}==
{{trans|Python}}
This is a translation of the Python version which uses a heap.
 
Python generators have been translated as Nim iterators. We used inline iterators as code expansion is not a problem in this case and performances are better. We formatted the output as in the D version.
 
Execution time is excellent: about 45 ms on our laptop (I5).
 
<lang Nim>import heapqueue, strformat
 
type
 
CubeSum = tuple[x, y, value: int]
 
# Comparison function needed for the heap queues.
proc `<`(c1, c2: CubeSum): bool = c1.value < c2.value
 
template cube(n: int): int = n * n * n
 
 
iterator cubesum(): CubeSum =
var queue: HeapQueue[CubeSum]
var n = 1
while true:
while queue.len == 0 or queue[0].value > cube(n):
queue.push (n, 1, cube(n) + 1)
inc n
var s = queue.pop()
yield s
inc s.y
if s.y < s.x: queue.push (s.x, s.y, cube(s.x) + cube(s.y))
 
 
iterator taxis(): seq[CubeSum] =
var result: seq[CubeSum] = @[(0, 0, 0)]
for s in cubesum():
if s.value == result[^1].value:
result.add s
else:
if result.len > 1: yield result
result.setLen(0)
result.add s # These two statements are faster than the single result = @[s].
 
 
var n = 0
for t in taxis():
inc n
if n > 2006: break
if n <= 25 or n >= 2000:
stdout.write &"{n:4}: {t[0].value:10}"
for s in t:
stdout.write &" = {s.x:4}^3 + {s.y:4}^3"
echo()</lang>
 
{{out}}
<pre> 1: 1729 = 10^3 + 9^3 = 12^3 + 1^3
2: 4104 = 15^3 + 9^3 = 16^3 + 2^3
3: 13832 = 20^3 + 18^3 = 24^3 + 2^3
4: 20683 = 24^3 + 19^3 = 27^3 + 10^3
5: 32832 = 30^3 + 18^3 = 32^3 + 4^3
6: 39312 = 33^3 + 15^3 = 34^3 + 2^3
7: 40033 = 34^3 + 9^3 = 33^3 + 16^3
8: 46683 = 30^3 + 27^3 = 36^3 + 3^3
9: 64232 = 39^3 + 17^3 = 36^3 + 26^3
10: 65728 = 33^3 + 31^3 = 40^3 + 12^3
11: 110656 = 40^3 + 36^3 = 48^3 + 4^3
12: 110808 = 45^3 + 27^3 = 48^3 + 6^3
13: 134379 = 43^3 + 38^3 = 51^3 + 12^3
14: 149389 = 50^3 + 29^3 = 53^3 + 8^3
15: 165464 = 54^3 + 20^3 = 48^3 + 38^3
16: 171288 = 54^3 + 24^3 = 55^3 + 17^3
17: 195841 = 58^3 + 9^3 = 57^3 + 22^3
18: 216027 = 59^3 + 22^3 = 60^3 + 3^3
19: 216125 = 50^3 + 45^3 = 60^3 + 5^3
20: 262656 = 60^3 + 36^3 = 64^3 + 8^3
21: 314496 = 66^3 + 30^3 = 68^3 + 4^3
22: 320264 = 68^3 + 18^3 = 66^3 + 32^3
23: 327763 = 67^3 + 30^3 = 58^3 + 51^3
24: 373464 = 60^3 + 54^3 = 72^3 + 6^3
25: 402597 = 61^3 + 56^3 = 69^3 + 42^3
2000: 1671816384 = 944^3 + 940^3 = 1168^3 + 428^3
2001: 1672470592 = 1124^3 + 632^3 = 1187^3 + 29^3
2002: 1673170856 = 1034^3 + 828^3 = 1164^3 + 458^3
2003: 1675045225 = 1153^3 + 522^3 = 1081^3 + 744^3
2004: 1675958167 = 1096^3 + 711^3 = 1159^3 + 492^3
2005: 1676926719 = 1095^3 + 714^3 = 1188^3 + 63^3
2006: 1677646971 = 1188^3 + 99^3 = 990^3 + 891^3</pre>
 
=={{header|PARI/GP}}==
Anonymous user