How to resolve the algorithm Taxicab numbers step by step in the jq programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Taxicab numbers step by step in the jq programming language
Table of Contents
Problem Statement
A taxicab number (the definition that is being used here) is a positive integer that can be expressed as the sum of two positive cubes in more than one way.
The first taxicab number is 1729, which is:
Taxicab numbers are also known as:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Taxicab numbers step by step in the jq programming language
Source code in the jq programming language
# 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.
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,
# where t is a taxicab number, and d1 and d2 are distinct
# decompositions [i,j] with i^3 + j^3 == t.
# The stream includes each taxicab number once only.
#
def taxicabs0:
sum_of_two_cubes as $sums
| range(1;$sums|length) as $i
| if $sums[$i][0] == $sums[$i-1][0]
and ($i==1 or $sums[$i][0] != $sums[$i-2][0])
then [$sums[$i][0], $sums[$i-1][1], $sums[$i][1]]
else empty
end;
# Output a stream of $n taxicab triples: [t, d1, d2] as described above,
# without repeating t.
def taxicabs:
# If your jq includes until/2 then the following definition
# can be omitted:
def until(cond; next):
def _until: if cond then . else (next|_until) end; _until;
. as $n
| [10, ($n / 10 | floor)] | max as $increment
| [20, ($n / 2 | floor)] | max
| [ ., [taxicabs0] ]
| until( .[1] | length >= $m; (.[0] + $increment) | [., [taxicabs0]] )
| .[1][0:$n] ;
2006 | taxicabs as $t
| (range(0;25), range(1999;2006)) as $i
| "\($i+1): \($t[$i][0]) ~ \($t[$i][1]) and \($t[$i][2])"
$ jq -n -r -f Taxicab_numbers.jq
1: 1729 ~ [1,12] and [9,10]
2: 4104 ~ [2,16] and [9,15]
3: 13832 ~ [2,24] and [18,20]
4: 20683 ~ [10,27] and [19,24]
5: 32832 ~ [4,32] and [18,30]
6: 39312 ~ [2,34] and [15,33]
7: 40033 ~ [9,34] and [16,33]
8: 46683 ~ [3,36] and [27,30]
9: 64232 ~ [17,39] and [26,36]
10: 65728 ~ [12,40] and [31,33]
11: 110656 ~ [4,48] and [36,40]
12: 110808 ~ [6,48] and [27,45]
13: 134379 ~ [12,51] and [38,43]
14: 149389 ~ [8,53] and [29,50]
15: 165464 ~ [20,54] and [38,48]
16: 171288 ~ [17,55] and [24,54]
17: 195841 ~ [9,58] and [22,57]
18: 216027 ~ [3,60] and [22,59]
19: 216125 ~ [5,60] and [45,50]
20: 262656 ~ [8,64] and [36,60]
21: 314496 ~ [4,68] and [30,66]
22: 320264 ~ [18,68] and [32,66]
23: 327763 ~ [30,67] and [51,58]
24: 373464 ~ [6,72] and [54,60]
25: 402597 ~ [42,69] and [56,61]
2000: 1671816384 ~ [428,1168] and [940,944]
2001: 1672470592 ~ [29,1187] and [632,1124]
2002: 1673170856 ~ [458,1164] and [828,1034]
2003: 1675045225 ~ [522,1153] and [744,1081]
2004: 1675958167 ~ [492,1159] and [711,1096]
2005: 1676926719 ~ [63,1188] and [714,1095]
2006: 1677646971 ~ [99,1188] and [891,990]
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the SQL programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Kdf9 Usercode programming language
You may also check:How to resolve the algorithm Tupper's self-referential formula step by step in the Java programming language
You may also check:How to resolve the algorithm Additive primes step by step in the APL programming language
You may also check:How to resolve the algorithm Biorhythms step by step in the Amazing Hopper programming language