Taxicab numbers: Difference between revisions
(J: show both sets of constituent cubes for each taxicab number) |
(→{{header|Python}}: Fixed forthwith! (see talk page).) |
||
Line 108: | Line 108: | ||
for c1, c2 in product(cube2n, cube2n): |
for c1, c2 in product(cube2n, cube2n): |
||
if c1 >= c2: sum2cubes[c1 + c2].add((cube2n[c1], cube2n[c2])) |
if c1 >= c2: sum2cubes[c1 + c2].add((cube2n[c1], cube2n[c2])) |
||
#sum2cubes[c1 + c2].add(tuple(sorted([cube2n[c1], cube2n[c2]]))) |
|||
taxied = sorted((k, v) for k,v in sum2cubes.items() if len(v) |
taxied = sorted((k, v) for k,v in sum2cubes.items() if len(v) >= 2) |
||
#pp(len(taxied)) # 2068 |
#pp(len(taxied)) # 2068 |
||
for t in enumerate(taxied[:25], 1): |
|||
pp(t) |
|||
⚫ | |||
print('...') |
|||
⚫ | |||
pp(t)</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre>(1, (1729, {(12, 1), (10, 9)})) |
||
(2, (4104, {(16, 2), (15, 9)})) |
|||
(3, (13832, {(20, 18), (24, 2)})) |
|||
(4, (20683, {(27, 10), (24, 19)})) |
|||
(5, (32832, {(30, 18), (32, 4)})) |
|||
(6, (39312, {(33, 15), (34, 2)})) |
|||
(7, (40033, {(33, 16), (34, 9)})) |
|||
(8, (46683, {(30, 27), (36, 3)})) |
|||
(9, (64232, {(36, 26), (39, 17)})) |
|||
(10, (65728, {(33, 31), (40, 12)})) |
|||
(11, (110656, {(48, 4), (40, 36)})) |
|||
(12, (110808, {(48, 6), (45, 27)})) |
|||
(13, (134379, {(51, 12), (43, 38)})) |
|||
(14, (149389, {(50, 29), (53, 8)})) |
|||
(15, (165464, {(54, 20), (48, 38)})) |
|||
(16, (171288, {(54, 24), (55, 17)})) |
|||
(17, (195841, {(57, 22), (58, 9)})) |
|||
(18, (216027, {(60, 3), (59, 22)})) |
|||
(19, (216125, {(60, 5), (50, 45)})) |
|||
(20, (262656, {(64, 8), (60, 36)})) |
|||
(21, (314496, {(66, 30), (68, 4)})) |
|||
(22, (320264, {(66, 32), (68, 18)})) |
|||
(23, (327763, {(58, 51), (67, 30)})) |
|||
(24, (373464, {(72, 6), (60, 54)})) |
|||
(25, (402597, {(69, 42), (61, 56)})) |
|||
... |
|||
[(1682523899, {(1187, 216), (1178, 363)}), |
|||
(2000, (1671816384, {(1168, 428), (944, 940)})) |
|||
(2001, (1672470592, {(1187, 29), (1124, 632)})) |
|||
(2002, (1673170856, {(1164, 458), (1034, 828)})) |
|||
(2003, (1675045225, {(1153, 522), (1081, 744)})) |
|||
(2004, (1675958167, {(1159, 492), (1096, 711)})) |
|||
(2005, (1676926719, {(1188, 63), (1095, 714)})) |
|||
(2006, (1677646971, {(990, 891), (1188, 99)}))</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
Revision as of 17:35, 14 March 2014
definition
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:
- 13 + 123 and
- 93 + 103.
Taxicab numbers are also known as:
- taxi numbers
- taxicab numbers
- taxi cab numbers
- taxi-cab numbers
- Hardy-Ramanujan numbers
task requirements
The task requirements are:
- compute the lowest 25 taxicab numbers.
- exactly 25 taxicab numbers are to be shown (in numeric order).
- show the taxicab numbers as well as its constituent cubes.
- show all numbers in a very readable (aligned) format.
- show the 2,000th taxicab number + a half dozen more (extra credit)).
See also
- Sequence A001235 taxicab numbers on The On-Line Encyclopedia of Integer Sequences.
- Entry Hardy-Ramanujan Number on The Eric Weisstein's World of Mathematics (TM).
- Entry taxicab number on The Eric Weisstein's World of Mathematics (TM).
- Wiki entry Hardy-Ramanujan number.
J
<lang J> 25 {. ;({."#. <@(0&#`({.@{.(;,)<@}."1)@.(1<#))/. ])/:~~.,/(+,/:~@,)"0/~3^~1+i.100 ┌──────┬────────────┬─────────────┐ │1729 │1 1728 │729 1000 │ ├──────┼────────────┼─────────────┤ │4104 │8 4096 │729 3375 │ ├──────┼────────────┼─────────────┤ │13832 │8 13824 │5832 8000 │ ├──────┼────────────┼─────────────┤ │20683 │1000 19683 │6859 13824 │ ├──────┼────────────┼─────────────┤ │32832 │64 32768 │5832 27000 │ ├──────┼────────────┼─────────────┤ │39312 │8 39304 │3375 35937 │ ├──────┼────────────┼─────────────┤ │40033 │729 39304 │4096 35937 │ ├──────┼────────────┼─────────────┤ │46683 │27 46656 │19683 27000 │ ├──────┼────────────┼─────────────┤ │64232 │4913 59319 │17576 46656 │ ├──────┼────────────┼─────────────┤ │65728 │1728 64000 │29791 35937 │ ├──────┼────────────┼─────────────┤ │110656│64 110592 │46656 64000 │ ├──────┼────────────┼─────────────┤ │110808│216 110592 │19683 91125 │ ├──────┼────────────┼─────────────┤ │134379│1728 132651 │54872 79507 │ ├──────┼────────────┼─────────────┤ │149389│512 148877 │24389 125000 │ ├──────┼────────────┼─────────────┤ │165464│8000 157464 │54872 110592 │ ├──────┼────────────┼─────────────┤ │171288│4913 166375 │13824 157464 │ ├──────┼────────────┼─────────────┤ │195841│729 195112 │10648 185193 │ ├──────┼────────────┼─────────────┤ │216027│27 216000 │10648 205379 │ ├──────┼────────────┼─────────────┤ │216125│125 216000 │91125 125000 │ ├──────┼────────────┼─────────────┤ │262656│512 262144 │46656 216000 │ ├──────┼────────────┼─────────────┤ │314496│64 314432 │27000 287496 │ ├──────┼────────────┼─────────────┤ │320264│5832 314432 │32768 287496 │ ├──────┼────────────┼─────────────┤ │327763│27000 300763│132651 195112│ ├──────┼────────────┼─────────────┤ │373464│216 373248 │157464 216000│ ├──────┼────────────┼─────────────┤ │402597│74088 328509│175616 226981│ └──────┴────────────┴─────────────┘</lang>
Explanation:
First, generate 100 cubes.
Then, form a 3 column table of unique rows: sum, small cube, large cube
Then, gather rows where the first entry is the same. Keep the ones with at least two such entries.
Note that the cube root of the 25th entry is slightly smaller than 74, so testing against the first 100 cubes is more than sufficient.
Python
(Magic number 1201 found by trial and error) <lang python>from collections import defaultdict from itertools import product from pprint import pprint as pp
cube2n = {x**3:x for x in range(1, 1201)} sum2cubes = defaultdict(set) for c1, c2 in product(cube2n, cube2n): if c1 >= c2: sum2cubes[c1 + c2].add((cube2n[c1], cube2n[c2]))
taxied = sorted((k, v) for k,v in sum2cubes.items() if len(v) >= 2)
- pp(len(taxied)) # 2068
for t in enumerate(taxied[:25], 1):
pp(t)
print('...') for t in enumerate(taxied[2000-1:2000+6], 2000):
pp(t)</lang>
- Output:
(1, (1729, {(12, 1), (10, 9)})) (2, (4104, {(16, 2), (15, 9)})) (3, (13832, {(20, 18), (24, 2)})) (4, (20683, {(27, 10), (24, 19)})) (5, (32832, {(30, 18), (32, 4)})) (6, (39312, {(33, 15), (34, 2)})) (7, (40033, {(33, 16), (34, 9)})) (8, (46683, {(30, 27), (36, 3)})) (9, (64232, {(36, 26), (39, 17)})) (10, (65728, {(33, 31), (40, 12)})) (11, (110656, {(48, 4), (40, 36)})) (12, (110808, {(48, 6), (45, 27)})) (13, (134379, {(51, 12), (43, 38)})) (14, (149389, {(50, 29), (53, 8)})) (15, (165464, {(54, 20), (48, 38)})) (16, (171288, {(54, 24), (55, 17)})) (17, (195841, {(57, 22), (58, 9)})) (18, (216027, {(60, 3), (59, 22)})) (19, (216125, {(60, 5), (50, 45)})) (20, (262656, {(64, 8), (60, 36)})) (21, (314496, {(66, 30), (68, 4)})) (22, (320264, {(66, 32), (68, 18)})) (23, (327763, {(58, 51), (67, 30)})) (24, (373464, {(72, 6), (60, 54)})) (25, (402597, {(69, 42), (61, 56)})) ... (2000, (1671816384, {(1168, 428), (944, 940)})) (2001, (1672470592, {(1187, 29), (1124, 632)})) (2002, (1673170856, {(1164, 458), (1034, 828)})) (2003, (1675045225, {(1153, 522), (1081, 744)})) (2004, (1675958167, {(1159, 492), (1096, 711)})) (2005, (1676926719, {(1188, 63), (1095, 714)})) (2006, (1677646971, {(990, 891), (1188, 99)}))
REXX
<lang rexx>/*REXX program displays the first (lowest) taxicab numbers. */ parse arg L1 H1 L2 H2 . /*obtain the optional numbers. */ if L1== | L1==',' then L1=1 /*L1 " " " " " */ if H1== | H1==',' then H1=25 /*H1 " " " " " */ if L2== | L2==',' then L2=4000 /*L2 " " " " " */ if H2== | H2==',' then H2=4007 /*H2 " " " " " */ mx=max(L1, H1, L2, H2) /*find how many taxicab #s needed*/ w=length(mx) /*width is used formatting output*/ numeric digits 20 /*prepare to use larger numbers. */
- =0; @.=0; $.=; $=; b='■'; t='**3' /*initialize some REXX variables.*/
/* [↓] generate extra taxicab #s*/ do j=1 until #>=mx /*taxicab nums won't be in order.*/ jjj=j**3 /*might as well calculate a cube.*/ z=; do k=1 for j-1; s=jjj+k**3 /*define a whole bunch of cubes. */ if @.s==0 then do /*if cube not defined, then do it*/ @.s = "────►"right(j,6,b)t"■■■+"right(k,6,b)t iterate /* ··· and then go and do another*/ end /* [↑] define one cube at a time.*/ z=s /*save cube for taxicab # list.*/ @.s=@.s","right(j,9,b)t"■■■+"right(k,6,b)t /*build the text.*/ $.s=right(s,15,b)'■■■'@.s /*define the rest of taxicab #s. */ $=$ z /*define a #. taxicab number.*/ #=words($) /*count the number of taxicab #s.*/ end /*k*/ /* [↑] build cubes one-at-a-time*/ end /*j*/ /* [↑] complete with overage #s.*/ /*esort builds array & sorts it.*/
list=esort(#) /*sort taxicab nums, create list.*/ if L1\==0 then /* [↓] list N taxicab numbers.*/
do j=L1 to H1; _=word(list,j) /*get one taxicab num at a time. */ say right(j,w) translate($._,,b) /*display taxicab # (with blanks)*/ end /*j*/ /* [↑] ■ are translated to blanks*/
say; if L2\==0 then /*display a blank separator line.*/
do j=L2 to H2; _=word(list,j) /*get one taxicab num at a time. */ say right(j,w) translate($._,,b) /*display taxicab # (with blanks)*/ end /*j*/ /* [↑] ■ are translated to blanks*/
exit /*stick a fork in it, we're done.*/ /*──────────────────────────────────ESORT subroutine────────────────────*/ esort: procedure expose #. $; parse arg N 1 h a
do j=1 for N; #.j=word($,j); end /*j*/ do while h>1; h=h%2; do i=1 for N-h; j=i; k=h+i do while #.k<#.j; t=#.j; #.j=#.k; #.k=t if h>=j then leave; j=j-h; k=k-h; end; end; end; do m=1 for N; a=a #.m; end /*m*/; return a</lang>
output using the default input:
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 36**3 + 26**3, 39**3 + 17**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 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 66**3 + 32**3, 68**3 + 18**3 23 327763 58**3 + 51**3, 67**3 + 30**3 24 373464 60**3 + 54**3, 72**3 + 6**3 25 402597 61**3 + 56**3, 69**3 + 42**3 2000 1802609991 1002**3 + 927**3, 1056**3 + 855**3 2001 1836207171 1134**3 + 723**3, 1155**3 + 666**3 2002 1876948136 1050**3 + 896**3, 1148**3 + 714**3 2003 1898380125 1090**3 + 845**3, 1125**3 + 780**3 2004 1948631048 1010**3 + 972**3, 1154**3 + 744**3 2005 2027722248 1074**3 + 924**3, 1153**3 + 791**3 2006 2080128456 1058**3 + 964**3, 1136**3 + 850**3 2007 2134551608 1052**3 + 990**3, 1124**3 + 894**3