Taxicab numbers: Difference between revisions

Content added Content deleted
m (formatting of task description)
(Added Kotlin)
Line 1,527: Line 1,527:
2005: 1676926719 ~ [63,1188] and [714,1095]
2005: 1676926719 ~ [63,1188] and [714,1095]
2006: 1677646971 ~ [99,1188] and [891,990]</lang>
2006: 1677646971 ~ [99,1188] and [891,990]</lang>

=={{header|Kotlin}}==
{{trans|Java}}
<lang scala>// version 1.0.6

import java.util.PriorityQueue

class CubeSum(val x: Long, val y: Long) : Comparable<CubeSum> {
val value: Long = x * x * x + y * y * y

override fun toString() = String.format("%4d^3 + %3d^3", x, y)
override fun compareTo(other: CubeSum) = value.compareTo(other.value)
}

class SumIterator : Iterator<CubeSum> {
private val pq = PriorityQueue<CubeSum>()
private var n = 0L
override fun hasNext() = true

override fun next(): CubeSum {
while (pq.size == 0 || pq.peek().value >= n * n * n)
pq.add(CubeSum(++n, 1))
val s: CubeSum = pq.remove()
if (s.x > s.y + 1) pq.add(CubeSum(s.x, s.y + 1))
return s
}
}

class TaxiIterator : Iterator<MutableList<CubeSum>> {
private val sumIterator = SumIterator()
private var last: CubeSum = sumIterator.next()

override fun hasNext() = true

override fun next(): MutableList<CubeSum> {
var s: CubeSum = sumIterator.next()
val train = mutableListOf<CubeSum>()
while (s.value != last.value) {
last = s
s = sumIterator.next()
}
train.add(last)
do {
train.add(s)
s = sumIterator.next()
}
while (s.value == last.value)
last = s
return train
}
}

fun main(args: Array<String>) {
val taxi = TaxiIterator()
for (i in 1..2006) {
val t = taxi.next()
if (i in 26 until 2000) continue
print(String.format("%4d: %10d", i, t[0].value))
for (s in t) print(" = $s")
println()
}
}</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 = 33^3 + 16^3 = 34^3 + 9^3
8: 46683 = 30^3 + 27^3 = 36^3 + 3^3
9: 64232 = 39^3 + 17^3 = 36^3 + 26^3
10: 65728 = 40^3 + 12^3 = 33^3 + 31^3
11: 110656 = 40^3 + 36^3 = 48^3 + 4^3
12: 110808 = 45^3 + 27^3 = 48^3 + 6^3
13: 134379 = 51^3 + 12^3 = 43^3 + 38^3
14: 149389 = 50^3 + 29^3 = 53^3 + 8^3
15: 165464 = 48^3 + 38^3 = 54^3 + 20^3
16: 171288 = 54^3 + 24^3 = 55^3 + 17^3
17: 195841 = 57^3 + 22^3 = 58^3 + 9^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 = 69^3 + 42^3 = 61^3 + 56^3
2000: 1671816384 = 1168^3 + 428^3 = 944^3 + 940^3
2001: 1672470592 = 1124^3 + 632^3 = 1187^3 + 29^3
2002: 1673170856 = 1164^3 + 458^3 = 1034^3 + 828^3
2003: 1675045225 = 1153^3 + 522^3 = 1081^3 + 744^3
2004: 1675958167 = 1159^3 + 492^3 = 1096^3 + 711^3
2005: 1676926719 = 1095^3 + 714^3 = 1188^3 + 63^3
2006: 1677646971 = 990^3 + 891^3 = 1188^3 + 99^3
</pre>


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==