Taxicab numbers: Difference between revisions

→‎{{header|jq}}: truncate sum_of_two_cubes to guarantee accuracy
(→‎{{header|jq}}: truncate sum_of_two_cubes to guarantee accuracy)
Line 946:
{{works with|jq|1.4}}
 
<lang jq># Output: an array of triples 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
# sorted by the sum:
# as it might not capture taxicab numbers greater than $in ^ 3.
# Only cubes of 1 to ($in-1) are considered.
def sum_of_two_cubes:
def cubed: .*.*.;
. as $in
| (cubed + 1) as $limit
| [range(1;$in) as $i | range($i;$in) as $j
 
| [ ($i|cubed) + ($j|cubed), [$i, $j] ] ] | sort;
| map( select( .[0] < $limit ) );
 
# Output a stream of triples [t, d1, d2], in order of t,
2,502

edits