Sum of first n cubes: Difference between revisions

From Rosetta Code
Content added Content deleted
(Added Wren)
Line 31: Line 31:
{{out}}
{{out}}
As above.
As above.

=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">sum_first_n_cubes</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">return</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">sq_power</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">),</span><span style="color: #000000;">3</span><span style="color: #0000FF;">))</span> <span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">49</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #000000;">sum_first_n_cubes</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">join_by</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">,{{</span><span style="color: #008000;">"%,9d"</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">}),</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)})</span>
<!--</lang>-->
{{out}}
<pre>
0 1 9 36 100 225 441 784 1,296 2,025
3,025 4,356 6,084 8,281 11,025 14,400 18,496 23,409 29,241 36,100
44,100 53,361 64,009 76,176 90,000 105,625 123,201 142,884 164,836 189,225
216,225 246,016 278,784 314,721 354,025 396,900 443,556 494,209 549,081 608,400
672,400 741,321 815,409 894,916 980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625
</pre>


=={{header|Raku}}==
=={{header|Raku}}==

Revision as of 16:07, 19 May 2021

Sum of first n cubes is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.
Task

Find and show sum of first   n   cubes,   where n < 51

Factor

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: grouping math math.functions prettyprint sequences ;

50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.</lang>

Output:
0      1      9      36     100    225     441     784     1296    2025
3025   4356   6084   8281   11025  14400   18496   23409   29241   36100
44100  53361  64009  76176  90000  105625  123201  142884  164836  189225
216225 246016 278784 314721 354025 396900  443556  494209  549081  608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625

Alternatively, this is the same as the triangular numbers squared, where the triangular numbers are given by

Tn = n(n + 1) / 2

Works with: Factor version 0.99 2021-02-05

<lang factor>USING: grouping kernel math prettyprint sequences ;

triangular ( n -- m ) dup 1 + * 2/ ;

50 <iota> [ triangular sq ] map 10 group simple-table.</lang>

Output:

As above.

Phix

function sum_first_n_cubes(integer n) return sum(sq_power(tagset(n),3)) end function
sequence res = apply(tagset(49,0),sum_first_n_cubes)
printf(1,"%s\n",{join_by(apply(true,sprintf,{{"%,9d"},res}),1,10)})
Output:
        0           1           9          36         100         225         441         784       1,296       2,025
    3,025       4,356       6,084       8,281      11,025      14,400      18,496      23,409      29,241      36,100
   44,100      53,361      64,009      76,176      90,000     105,625     123,201     142,884     164,836     189,225
  216,225     246,016     278,784     314,721     354,025     396,900     443,556     494,209     549,081     608,400
  672,400     741,321     815,409     894,916     980,100   1,071,225   1,168,561   1,272,384   1,382,976   1,500,625

Raku

<lang perl6>my @sums_of_all_cubes = [\+] ^Inf X** 3;

say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);</lang>

Output:
     0       1       9      36     100     225     441     784    1296    2025
  3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
 44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

REXX

<lang rexx>/*REXX program finds and displays a number of sums of the first N cubes. */ parse arg lo hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 50 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 12 /*width of a number in any column. */

                    @cubeSum= ' cube sums,  where  N  < '     commas(hi)

say ' index │'center(@cubeSum, 1 + cols*(w+1) ) say '───────┼'center("" , 1 + cols*(w+1), '─') found= 0; idx= 1 /*initialize the number for the index. */ $=; sum= 0 /*a list of the sum of N cubes. . */

    do j=0  for hi;       sum= sum + j**3       /*compute the sum of this cube + others*/
    found= found + 1                            /*bump the number of  sums  shown.     */
    c= commas(sum)                              /*maybe add commas to the number.      */
    $= $ right(c, max(w, length(c) ) )          /*add a sum of  N  cubes to the $ list.*/
    if found//cols\==0  then iterate            /*have we populated a line of output?  */
    say center(idx, 7)'│'  substr($, 2);   $=   /*display what we have so far  (cols). */
    idx= idx + cols                             /*bump the  index  count for the output*/
    end   /*j*/

if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(found) @cubeSum exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>

output   when using the default inputs:

(Shown at five-sixth size.)

 index │                                                    cube sums,  where  N  <  50
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │            0            1            9           36          100          225          441          784        1,296        2,025
  11   │        3,025        4,356        6,084        8,281       11,025       14,400       18,496       23,409       29,241       36,100
  21   │       44,100       53,361       64,009       76,176       90,000      105,625      123,201      142,884      164,836      189,225
  31   │      216,225      246,016      278,784      314,721      354,025      396,900      443,556      494,209      549,081      608,400
  41   │      672,400      741,321      815,409      894,916      980,100    1,071,225    1,168,561    1,272,384    1,382,976    1,500,625
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Ring

<lang ring> see "working..." + nl see "Sum of first n cubes:" + nl row = 0 lenCubes = 50

for n = 1 to lenCubes

   sumCubes = 0
   for m = 1 to n
       sumCubes = sumCubes + pow(m,3)
   next
   row = row + 1
   see "" + sumCubes + " "
   if row%5 = 0
      see nl
   ok    

next

see "Found " + row + " cubes" + nl see "done..." + nl </lang>

Output:
working...
Sum of first n cubes:
1 9 36 100 225 
441 784 1296 2025 3025 
4356 6084 8281 11025 14400 
18496 23409 29241 36100 44100 
53361 64009 76176 90000 105625 
123201 142884 164836 189225 216225 
246016 278784 314721 354025 396900 
443556 494209 549081 608400 672400 
741321 815409 894916 980100 1071225 
1168561 1272384 1382976 1500625 1625625 
Found 50 cubes
done...

Wren

<lang ecmascript>import "/fmt" for Fmt

System.print("Cumulative sums of the first 50 cubes:") var sum = 0 var sum = 0 for (n in 0..49) {

   sum = sum + n * n * n
   Fmt.write("$,9d ", sum)
   if ((n % 10) == 9) System.print()

} System.print()</lang>

Output:
Cumulative sums of the first 50 cubes:
        0         1         9        36       100       225       441       784     1,296     2,025 
    3,025     4,356     6,084     8,281    11,025    14,400    18,496    23,409    29,241    36,100 
   44,100    53,361    64,009    76,176    90,000   105,625   123,201   142,884   164,836   189,225 
  216,225   246,016   278,784   314,721   354,025   396,900   443,556   494,209   549,081   608,400 
  672,400   741,321   815,409   894,916   980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625