Rebol3 Code Examplex


Taxicab numbers

Find the smallest integers expressible as the sum of two positive cubes in 𝑛 distinct ways.

Rebol [
    title: "Rosetta code: Taxicab numbers"
    file:  %Taxicab_numbers.r3
    url:   https://rosettacode.org/wiki/Taxicab_numbers
]

e: 1200
sums: make map! 700'000             ;; sum-of-two-cubes -> block of [a b] pairs producing it
repeat a (e - 2) [
    a3: a * a * a
    for b a (e - 1) 1 [
        b3: b * b * b
        s3: a3 + b3
        either pairs: sums/:s3 [
            repend pairs [a b]      ;; another way to reach this sum
        ][  sums/:s3: reduce [a b]] ;; first way seen for this sum
    ]
]

i: 0
foreach s3 sort keys-of sums [
    ab: select sums s3
    if 4 > length? ab [continue]                        ;; skip sums reached only one way
    i: i + 1
    if all [4 == length? ab i > 25 i < 2000] [continue] ;; thin out the common two-way cases
    if i > 2006 [break]
    prin ajoin [pad i -5 ": " as-green pad s3 10]
    foreach [a b] ab [
        prin ajoin [" = " pad a -3 "³ + " pad b -4 "³"]
    ]
    print ""
]

Output:

    1: 1729       =   1³ +   12³ =   9³ +   10³
    2: 4104       =   2³ +   16³ =   9³ +   15³
    3: 13832      =   2³ +   24³ =  18³ +   20³
    4: 20683      =  10³ +   27³ =  19³ +   24³
    5: 32832      =   4³ +   32³ =  18³ +   30³
    6: 39312      =   2³ +   34³ =  15³ +   33³
    7: 40033      =   9³ +   34³ =  16³ +   33³
    8: 46683      =   3³ +   36³ =  27³ +   30³
    9: 64232      =  17³ +   39³ =  26³ +   36³
   10: 65728      =  12³ +   40³ =  31³ +   33³
   11: 110656     =   4³ +   48³ =  36³ +   40³
   12: 110808     =   6³ +   48³ =  27³ +   45³
   13: 134379     =  12³ +   51³ =  38³ +   43³
   14: 149389     =   8³ +   53³ =  29³ +   50³
   15: 165464     =  20³ +   54³ =  38³ +   48³
   16: 171288     =  17³ +   55³ =  24³ +   54³
   17: 195841     =   9³ +   58³ =  22³ +   57³
   18: 216027     =   3³ +   60³ =  22³ +   59³
   19: 216125     =   5³ +   60³ =  45³ +   50³
   20: 262656     =   8³ +   64³ =  36³ +   60³
   21: 314496     =   4³ +   68³ =  30³ +   66³
   22: 320264     =  18³ +   68³ =  32³ +   66³
   23: 327763     =  30³ +   67³ =  51³ +   58³
   24: 373464     =   6³ +   72³ =  54³ +   60³
   25: 402597     =  42³ +   69³ =  56³ +   61³
  455: 87539319   = 167³ +  436³ = 228³ +  423³ = 255³ +  414³
  535: 119824488  =  11³ +  493³ =  90³ +  492³ = 346³ +  428³
  588: 143604279  = 111³ +  522³ = 359³ +  460³ = 408³ +  423³
  655: 175959000  =  70³ +  560³ = 198³ +  552³ = 315³ +  525³
  888: 327763000  = 300³ +  670³ = 339³ +  661³ = 510³ +  580³
 1299: 700314552  = 334³ +  872³ = 456³ +  846³ = 510³ +  828³
 1398: 804360375  =  15³ +  930³ = 198³ +  927³ = 295³ +  920³
 1515: 958595904  =  22³ +  986³ = 180³ +  984³ = 692³ +  856³
 1660: 1148834232 = 222³ + 1044³ = 718³ +  920³ = 816³ +  846³
 1837: 1407672000 = 140³ + 1120³ = 396³ + 1104³ = 630³ + 1050³
 2000: 1671816384 = 428³ + 1168³ = 940³ +  944³
 2001: 1672470592 =  29³ + 1187³ = 632³ + 1124³
 2002: 1673170856 = 458³ + 1164³ = 828³ + 1034³
 2003: 1675045225 = 522³ + 1153³ = 744³ + 1081³
 2004: 1675958167 = 492³ + 1159³ = 711³ + 1096³
 2005: 1676926719 =  63³ + 1188³ = 714³ + 1095³
 2006: 1677646971 =  99³ + 1188³ = 891³ +  990³