Taxicab numbers

Revision as of 05:01, 14 March 2014 by rosettacode>Gerard Schildberger (added a new Rosetta Code task (taxicab numbers).)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Draft

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
  • 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 10,000th taxicab number + a half dozen more (extra credit)).

See also



REXX

<lang rexx>/*REXX program displays the first N (lowest) taxicab numbers. */ parse arg N L1 H1 L2 H2 . /*obtain the optional numbers. */ if N== | N==',' then N=25 /*N given? Then use the default.*/ if L1== | L1==',' then L1=1 /*L1 " " " " " */ if H1== | H1==',' then H1=N /*H1 " " " " " */ if L2== | L2==',' then L2=10000 /*L2 " " " " " */ if H2== | H2==',' then H2=10007 /*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. */

  1. =0; @.=0; $.=; $=; b='■'; t='**3' /*initialize some REXX variables.*/
                                      /* [↓]  generate extra taxicab #s*/
do j=1   until  zz==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. */
         leave                        /*leave the      DO  K    loop.  */
         end   /*k*/                  /* [↑]  build cubes one-at-a-time*/
$=$ z                                 /*define a   #.   taxicab number.*/
zz=words($)                           /*count the number of taxicab #s.*/
end   /*j*/                           /* [↑]  complete with overage #s.*/
                                      /*sort sub builds array, sorts it*/

list=esort(zz) /*sort taxicab nums, create list.*/

                                      /* [↓]  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

    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 input:   1 25 0 0

 
 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           32832   ────►    30**3   +    18**3,       32**3   +     4**3
 5           39312   ────►    33**3   +    15**3,       34**3   +     2**3
 6           46683   ────►    30**3   +    27**3,       36**3   +     3**3
 7           65728   ────►    33**3   +    31**3,       40**3   +    12**3
 8          110808   ────►    45**3   +    27**3,       48**3   +     6**3
 9          134379   ────►    43**3   +    38**3,       51**3   +    12**3
10          149389   ────►    50**3   +    29**3,       53**3   +     8**3
11          171288   ────►    54**3   +    24**3,       55**3   +    17**3
12          195841   ────►    57**3   +    22**3,       58**3   +     9**3
13          216027   ────►    59**3   +    22**3,       60**3   +     3**3
14          314496   ────►    66**3   +    30**3,       68**3   +     4**3
15          402597   ────►    61**3   +    56**3,       69**3   +    42**3
16          443889   ────►    73**3   +    38**3,       76**3   +    17**3
17          513000   ────►    75**3   +    45**3,       80**3   +    10**3
18          513856   ────►    72**3   +    52**3,       78**3   +    34**3
19          558441   ────►    72**3   +    57**3,       81**3   +    30**3
20          593047   ────►    70**3   +    63**3,       84**3   +     7**3
21          684019   ────►    75**3   +    64**3,       82**3   +    51**3
22          704977   ────►    86**3   +    41**3,       89**3   +     2**3
23          805688   ────►    92**3   +    30**3,       93**3   +    11**3
24          886464   ────►    90**3   +    54**3,       96**3   +    12**3
25         1016496   ────►    90**3   +    66**3,       97**3   +    47**3