Sum of first n cubes: Difference between revisions

From Rosetta Code
Content added Content deleted
m (→‎{{header|R}}: Syntax highlighting.)
(Added Easylang)
 
(18 intermediate revisions by 12 users not shown)
Line 8: Line 8:
{{trans|Nim}}
{{trans|Nim}}


<lang 11l>V s = 0
<syntaxhighlight lang="11l">V s = 0
L(n) 50
L(n) 50
s += n * n * n
s += n * n * n
print(String(s).rjust(7), end' I (n + 1) % 10 == 0 {"\n"} E ‘ ’)</lang>
print(String(s).rjust(7), end' I (n + 1) % 10 == 0 {"\n"} E ‘ ’)</syntaxhighlight>


{{out}}
{{out}}
Line 24: Line 24:
=={{header|Action!}}==
=={{header|Action!}}==
{{libheader|Action! Tool Kit}}
{{libheader|Action! Tool Kit}}
<lang Action!>INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit
<syntaxhighlight lang="action!">INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit


PROC Main()
PROC Main()
Line 44: Line 44:
FI
FI
OD
OD
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_first_n_cubes.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_first_n_cubes.png Screenshot from Atari 8-bit computer]
Line 57: Line 57:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_Io;
<syntaxhighlight lang="ada">with Ada.Text_Io;


procedure Sum_Of_First_N_Cubes is
procedure Sum_Of_First_N_Cubes is
Line 77: Line 77:
end loop;
end loop;
New_Line;
New_Line;
end Sum_Of_First_N_Cubes;</lang>
end Sum_Of_First_N_Cubes;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 89: Line 89:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
As noted in the second factor example, the sum of the cubes to n is (n(n + 1))/2)^2, i.e. the square of the sum of the numbers to n.
As noted in the second factor example, the sum of the cubes to n is (n(n + 1))/2)^2, i.e. the square of the sum of the numbers to n.
<lang algol68># show the sums of the first n cubes where 0 <= n < 50 #
<syntaxhighlight lang="algol68"># show the sums of the first n cubes where 0 <= n < 50 #
FOR i FROM 0 TO 49 DO
FOR i FROM 0 TO 49 DO
INT sum = ( i * ( i + 1 ) ) OVER 2;
INT sum = ( i * ( i + 1 ) ) OVER 2;
print( ( whole( sum * sum, -8 ) ) );
print( ( whole( sum * sum, -8 ) ) );
IF i MOD 10 = 9 THEN print( ( newline ) ) FI
IF i MOD 10 = 9 THEN print( ( newline ) ) FI
OD</lang>
OD</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 105: Line 105:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % show the sums of the cubes of n for 0 <= n < 50 %
<syntaxhighlight lang="algolw">begin % show the sums of the cubes of n for 0 <= n < 50 %
integer cubeSum;
integer cubeSum;
cubeSum := 0;
cubeSum := 0;
Line 113: Line 113:
if n rem 10 = 9 then write()
if n rem 10 = 9 then write()
end for_n
end for_n
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 124: Line 124:


=={{header|APL}}==
=={{header|APL}}==
<lang APL>10 5⍴+\0,(⍳49)*3</lang>
<syntaxhighlight lang="apl">10 5⍴+\0,(⍳49)*3</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 137: Line 137:
1071225 1168561 1272384 1382976 1500625</pre>
1071225 1168561 1272384 1382976 1500625</pre>
=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang applescript>------------------- SUM OF FIRST N CUBES -----------------
<syntaxhighlight lang="applescript">------------------- SUM OF FIRST N CUBES -----------------


-- sumsOfFirstNCubes :: Int -> [Int]
-- sumsOfFirstNCubes :: Int -> [Int]
Line 348: Line 348:
set my text item delimiters to dlm
set my text item delimiters to dlm
return s
return s
end unwords</lang>
end unwords</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 363: Line 363:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>sumCubes: 0
<syntaxhighlight lang="rebol">sumCubes: 0
loop split.every: 10 map 0..49 => [sumCubes: <= sumCubes + & ^ 3] 'a ->
loop split.every: 10 map 0..49 => [sumCubes: <= sumCubes + & ^ 3] 'a ->
print map a => [pad to :string & 7]</lang>
print map a => [pad to :string & 7]</syntaxhighlight>


{{out}}
{{out}}
Line 376: Line 376:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>pn := 0, result := ""
<syntaxhighlight lang="autohotkey">pn := 0, result := ""
loop 50 {
loop 50 {
n := SubStr(" " ((A_Index-1)**3 + pn), -6)
n := SubStr(" " ((A_Index-1)**3 + pn), -6)
Line 382: Line 382:
pn := n
pn := n
}
}
MsgBox % result</lang>
MsgBox % result</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100 225 441 784 1296 2025
<pre> 0 1 9 36 100 225 441 784 1296 2025
Line 391: Line 391:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_FIRST_N_CUBES.AWK
# syntax: GAWK -f SUM_OF_FIRST_N_CUBES.AWK
BEGIN {
BEGIN {
Line 403: Line 403:
exit(0)
exit(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 418: Line 418:
==={{header|BASIC256}}===
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
fila = 0
fila = 0
lenCubos = 49
lenCubos = 49
Line 438: Line 438:
print chr(13) + "Encontrados " & fila & " cubos."
print chr(13) + "Encontrados " & fila & " cubos."
end
end
</syntaxhighlight>
</lang>


==={{header|FreeBASIC}}===
==={{header|FreeBASIC}}===
<lang freebasic>
<syntaxhighlight lang="freebasic">
Dim As Integer fila = 0, lenCubos = 49, sumCubos
Dim As Integer fila = 0, lenCubos = 49, sumCubos


Line 460: Line 460:
Print !"\nEncontrados " & fila & " cubos."
Print !"\nEncontrados " & fila & " cubos."
Sleep
Sleep
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 480: Line 480:


==={{header|GW-BASIC}}===
==={{header|GW-BASIC}}===
<lang gwbasic>10 FOR N=0 TO 49
<syntaxhighlight lang="gwbasic">10 FOR N=0 TO 49
20 C#=C#+N^3
20 C#=C#+N^3
30 PRINT C#;
30 PRINT C#;
40 NEXT N</lang>
40 NEXT N</syntaxhighlight>
{{out}}<pre> 0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 1
{{out}}<pre> 0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 1
8496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884
8496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884
Line 491: Line 491:


==={{header|QB64}}===
==={{header|QB64}}===
<lang qbasic>For n%% = 0 To 49
<syntaxhighlight lang="qbasic">For n%% = 0 To 49
c& = c& + n%% ^ 3
c& = c& + n%% ^ 3
Print Using " ####### "; c&;
Print Using " ####### "; c&;
If n%% Mod 5 = 4 Then Print
If n%% Mod 5 = 4 Then Print
Next n%%</lang>
Next n%%</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 512: Line 512:
==={{header|QBasic}}===
==={{header|QBasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang qbasic>DEFLNG A-Z
<syntaxhighlight lang="qbasic">DEFLNG A-Z


fila = 0
fila = 0
Line 531: Line 531:


PRINT CHR$(13) + "Encontrados"; fila; "cubos."
PRINT CHR$(13) + "Encontrados"; fila; "cubos."
END</lang>
END</syntaxhighlight>


==={{header|Tiny BASIC}}===
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
Limited to 0 through 19 because integers only go up to 32767.
Limited to 0 through 19 because integers only go up to 32767.
<lang tinybasic>10 LET C = 0
<syntaxhighlight lang="basic">10 LET C = 0
20 LET N = 0
20 LET N = 0
30 LET C = C + N*N*N
30 LET C = C + N*N*N
Line 541: Line 542:
50 LET N = N + 1
50 LET N = N + 1
60 IF N = 19 THEN END
60 IF N = 19 THEN END
70 GOTO 30</lang>
70 GOTO 30</syntaxhighlight>


==={{header|Yabasic}}===
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
fila = 0
fila = 0
lenCubos = 49
lenCubos = 49
Line 564: Line 565:
print "\nEncontrados ", fila, " cubos.\n"
print "\nEncontrados ", fila, " cubos.\n"
end
end
</syntaxhighlight>
</lang>


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>∘‿5⥊+`(↕50)⋆3</lang>
<syntaxhighlight lang="bqn">∘‿5⥊+`(↕50)⋆3</syntaxhighlight>
{{out}}
{{out}}
<pre>┌─
<pre>┌─
Line 583: Line 584:


=={{header|C}}==
=={{header|C}}==
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


int main() {
int main() {
Line 591: Line 592:
}
}
return 0;
return 0;
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 609: Line 610:
=={{header|C#|CSharp}}==
=={{header|C#|CSharp}}==
No multiplication or exponentiation, just addition.
No multiplication or exponentiation, just addition.
<lang csharp>using System; using static System.Console;
<syntaxhighlight lang="csharp">using System; using static System.Console;
class Program { static void Main(string[] args) {
class Program { static void Main(string[] args) {
for (int i=0,j=-6,k=1,c=0,s=0;s<1600000;s+=c+=k+=j+=6)
for (int i=0,j=-6,k=1,c=0,s=0;s<1600000;s+=c+=k+=j+=6)
Write("{0,-7}{1}",s, (i+=i==3?-4:1)==0?"\n":" "); } }</lang>
Write("{0,-7}{1}",s, (i+=i==3?-4:1)==0?"\n":" "); } }</syntaxhighlight>
{{out}}
{{out}}
<pre>0 1 9 36 100
<pre>0 1 9 36 100
Line 626: Line 627:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <array>
<syntaxhighlight lang="cpp">#include <array>
#include <cstdio>
#include <cstdio>
#include <numeric>
#include <numeric>
Line 653: Line 654:
std::transform_inclusive_scan(a.begin(), a.end(), a.begin(), std::plus{}, cube);
std::transform_inclusive_scan(a.begin(), a.end(), a.begin(), std::plus{}, cube);
PrintContainer(a);
PrintContainer(a);
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 664: Line 665:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>start_up = proc ()
<syntaxhighlight lang="clu">start_up = proc ()
amount = 50
amount = 50
po: stream := stream$primary_output()
po: stream := stream$primary_output()
Line 674: Line 675:
if i//5 = 4 then stream$putc(po, '\n') end
if i//5 = 4 then stream$putc(po, '\n') end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 688: Line 689:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SUM-OF-CUBES.
PROGRAM-ID. SUM-OF-CUBES.


Line 717: Line 718:
IF OUT-PTR IS EQUAL TO 41,
IF OUT-PTR IS EQUAL TO 41,
DISPLAY OUT-LINE,
DISPLAY OUT-LINE,
MOVE 1 TO OUT-PTR.</lang>
MOVE 1 TO OUT-PTR.</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 729: Line 730:
672400 741321 815409 894916 980100
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625</pre>
1071225 1168561 1272384 1382976 1500625</pre>

=={{header|Comal}}==
<syntaxhighlight lang="comal">0010 ZONE 8
0020 sum:=0
0030 FOR cube:=0 TO 49 DO
0040 sum:+cube^3
0050 PRINT sum,
0060 IF cube MOD 5=4 THEN PRINT
0070 ENDFOR cube
0080 END</syntaxhighlight>
{{out}}
<pre>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</pre>


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub cube(n: uint32): (r: uint32) is
sub cube(n: uint32): (r: uint32) is
Line 748: Line 770:
print_char(' ');
print_char(' ');
end if;
end if;
end loop;</lang>
end loop;</syntaxhighlight>
{{out}}
{{out}}
<pre>0 1 9 36 100 225 441 784 1296 2025
<pre>0 1 9 36 100 225 441 784 1296 2025
Line 755: Line 777:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>

=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}


<syntaxhighlight lang="Delphi">

procedure ShowSumFirst50Cubes(Memo: TMemo);
var I,Sum: integer;
var S: string;
begin
S:='';
Sum:=0;
for I:=0 to 50-1 do
begin
Sum:=Sum+I*I*I;
S:=S+Format('%11.0n',[Sum+0.0]);
if (I mod 5)=4 then S:=S+CRLF;
end;
Memo.Lines.Add(S);
end;


</syntaxhighlight>
{{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


Elapsed Time: 1.428 ms.

</pre>


=={{header|Draco}}==
<syntaxhighlight lang="draco">proc nonrec cube(ulong n) ulong:
n * n * n
corp

proc nonrec main() void:
ulong sum;
byte n;
sum := 0;
for n from 0 upto 49 do
sum := sum + cube(n);
write(sum:8);
if n % 5 = 4 then writeln() fi
od
corp</syntaxhighlight>
{{out}}
<pre> 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</pre>

=={{header|EasyLang}}==
<syntaxhighlight>
for i = 0 to 49
sum += i * i * i
write sum & " "
.
</syntaxhighlight>


=={{header|Excel}}==
=={{header|Excel}}==
Line 764: Line 864:


{{Works with|Office 365 betas 2021}}
{{Works with|Office 365 betas 2021}}
<lang lisp>SUMNCUBES
<syntaxhighlight lang="lisp">SUMNCUBES
=LAMBDA(n,
=LAMBDA(n,
BINCOEFF(1 + n)(2) ^ 2
BINCOEFF(1 + n)(2) ^ 2
Line 777: Line 877:
)
)
)
)
)</lang>
)</syntaxhighlight>


The single formula in cell B2 below defines a dynamic array which populates the whole B2:K6 grid:
The single formula in cell B2 below defines a dynamic array which populates the whole B2:K6 grid:
Line 870: Line 970:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Sum of cubes: Nigel Galloway. May 20th., 2021
// Sum of cubes: Nigel Galloway. May 20th., 2021
let fN g=g*g*g in Seq.initInfinite((+)1>>fN)|>Seq.take 49|>Seq.scan((+))(0)|>Seq.iter(printf "%d "); printfn ""
let fN g=g*g*g in Seq.initInfinite((+)1>>fN)|>Seq.take 49|>Seq.scan((+))(0)|>Seq.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 881: Line 981:
=={{header|Factor}}==
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: grouping math math.functions prettyprint sequences ;
<syntaxhighlight lang="factor">USING: grouping math math.functions prettyprint sequences ;


50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.</lang>
50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 898: Line 998:


{{works with|Factor|0.99 2021-02-05}}
{{works with|Factor|0.99 2021-02-05}}
<lang factor>USING: grouping kernel math prettyprint sequences ;
<syntaxhighlight lang="factor">USING: grouping kernel math prettyprint sequences ;


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


50 <iota> [ triangular sq ] map 10 group simple-table.</lang>
50 <iota> [ triangular sq ] map 10 group simple-table.</syntaxhighlight>
{{out}}
{{out}}
As above.
As above.


=={{header|Fermat}}==
=={{header|Fermat}}==
<lang fermat>c:=0
<syntaxhighlight lang="fermat">c:=0
for n = 0 to 49 do
for n = 0 to 49 do
c:=c+n^3;
c:=c+n^3;
!(c,' ');
!(c,' ');
od;</lang>
od;</syntaxhighlight>
{{out}}<pre> 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</pre>
{{out}}<pre> 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</pre>


=={{header|Forth}}==
=={{header|Forth}}==
{{works with|Gforth}}
{{works with|Gforth}}
<lang forth>: sum-cubes ( n -- )
<syntaxhighlight lang="forth">: sum-cubes ( n -- )
0 swap 0 do
0 swap 0 do
i i i * * + dup 7 .r
i i i * * + dup 7 .r
Line 923: Line 1,023:


50 sum-cubes
50 sum-cubes
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 937: Line 1,037:
672400 741321 815409 894916 980100
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625
1071225 1168561 1272384 1382976 1500625
</pre>

=={{header|Frink}}==
<syntaxhighlight lang="frink">sum = 0
result = new array
for n = 0 to 49
{
sum = sum + n^3
result.push[sum]
}

println[formatTable[columnize[result, 10], "right"]]</syntaxhighlight>
{{out}}
<pre>
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
</pre>
</pre>


Line 942: Line 1,061:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 960: Line 1,079:
}
}
fmt.Println()
fmt.Println()
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 973: Line 1,092:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate, transpose)
<syntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
import Text.Printf (printf)
Line 1,014: Line 1,133:
let ws = maximum . fmap length <$> transpose rows
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100 225 441 784 1296 2025
<pre> 0 1 9 36 100 225 441 784 1296 2025
Line 1,024: Line 1,143:


Or, in terms of scanl:
Or, in terms of scanl:
<lang haskell>import Data.List (intercalate, scanl, transpose)
<syntaxhighlight lang="haskell">import Data.List (intercalate, scanl, transpose)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
import Text.Printf (printf)
Line 1,033: Line 1,152:
sumsOfFirstNCubes n =
sumsOfFirstNCubes n =
scanl
scanl
(\a x -> a + x ^ 3)
(\a -> (a +) . (^ 3))
0
0
[1 .. pred n]
[1 .. pred n]
Line 1,049: Line 1,168:
let ws = maximum . fmap length <$> transpose rows
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</lang>
in unlines $ intercalate gap . zipWith pw ws <$> rows</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,063: Line 1,182:


=={{header|J}}==
=={{header|J}}==
<lang J>10 5$+/\(i.^3:)50x</lang>
<syntaxhighlight lang="j">10 5$+/\(i.^3:)50x</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,075: Line 1,194:
672400 741321 815409 894916 980100
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625</pre>
1071225 1168561 1272384 1382976 1500625</pre>

If we wanted specifically the sum of positive integer cubes up through <code>n</code> it should be faster to use <code>0 0 1r4 1r2 1r4&p.</code>. For example:<syntaxhighlight lang="j"> 0 0 1r4 1r2 1r4&p. 49
1500625</syntaxhighlight>


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
"use strict";
"use strict";


Line 1,177: Line 1,299:
s.padStart(n, c)
s.padStart(n, c)
) : "";
) : "";


// maximum :: Ord a => [a] -> a
const maximum = xs => (
// The largest value in a non-empty list.
ys => 0 < ys.length ? (
ys.slice(1).reduce(
(a, y) => y > a ? (
y
) : a, ys[0]
)
) : undefined
)(xs);




Line 1,203: Line 1,312:
const
const
colWidths = transpose(rows).map(
colWidths = transpose(rows).map(
row => maximum(row.map(x => x.length))
row => Math.max(
...row.map(x => x.length)
)
);
);


Line 1,260: Line 1,371:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,276: Line 1,387:
{{works with|jq}}
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
# For the sake of stream-processing efficiency:
# For the sake of stream-processing efficiency:
def add(s): reduce s as $x (0; . + $x);
def add(s): reduce s as $x (0; . + $x);


def sum_of_cubes: add(range(0;.) | .*.*.);</lang>
def sum_of_cubes: add(range(0;.) | .*.*.);</syntaxhighlight>
'''The task'''
'''The task'''
<syntaxhighlight lang="jq">
<lang jq>
range(0;50) | sum_of_cubes as $sum
range(0;50) | sum_of_cubes as $sum
| "\(.) => \($sum)"</lang>
| "\(.) => \($sum)"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,306: Line 1,417:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))
<syntaxhighlight lang="julia">cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))


cubesumstil()
cubesumstil()
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
0 1 9 36 100 225 441 784 1296 2025
0 1 9 36 100 225 441 784 1296 2025
Line 1,318: Line 1,429:
</pre>
</pre>
Alternatively, and using the REPL, note that recent versions of Julia implement the accumulate function:
Alternatively, and using the REPL, note that recent versions of Julia implement the accumulate function:
<lang julia>
<syntaxhighlight lang="julia">
julia> println(accumulate((x, y) -> x + y^3, 0:49))
julia> println(accumulate((x, y) -> x + y^3, 0:49))


[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]
[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]
</syntaxhighlight>
</lang>

=={{header|K}}==
{{works with|ngn/k}}<syntaxhighlight lang=K>5 10#+\{x*x*x}[!50]
(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)</syntaxhighlight>


=={{header|MAD}}==
=={{header|MAD}}==
<lang mad> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
SUM = 0
SUM = 0
THROUGH LOOP, FOR STEP = 0, 1, STEP.GE.50
THROUGH LOOP, FOR STEP = 0, 1, STEP.GE.50
Line 1,331: Line 1,450:
LOOP PRINT FORMAT FMT, SUM
LOOP PRINT FORMAT FMT, SUM
VECTOR VALUES FMT = $I9*$
VECTOR VALUES FMT = $I9*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'> 0
<pre style='height:50ex;'> 0
Line 1,385: Line 1,504:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<lang Mathematica>Accumulate[Range[0, 49]^3]</lang>
<syntaxhighlight lang="mathematica">Accumulate[Range[0, 49]^3]</syntaxhighlight>
{{out}}
{{out}}
<pre>{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}</pre>
<pre>{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}</pre>


=={{header|Nim}}==
=={{header|Nim}}==
<lang Nim>import strutils
<syntaxhighlight lang="nim">import strutils


var s = 0
var s = 0
for n in 0..49:
for n in 0..49:
s += n * n * n
s += n * n * n
stdout.write ($s).align(7), if (n + 1) mod 10 == 0: '\n' else: ' '</lang>
stdout.write ($s).align(7), if (n + 1) mod 10 == 0: '\n' else: ' '</syntaxhighlight>


{{out}}
{{out}}
Line 1,405: Line 1,524:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>c=0;for(n=0,49,c=c+n^3;print(c))</lang>
<syntaxhighlight lang="parigp">c=0;for(n=0,49,c=c+n^3;print(c))</syntaxhighlight>

=={{header|Pascal}}==
<syntaxhighlight lang="pascal">program sumOfFirstNCubes(output);
const
N = 49;
var
i: integer;
sum: integer;
begin
sum := 0;
for i := 0 to N do
begin
sum := sum + sqr(i) * i;
{ In Extended Pascal you could also write:
sum := sum + i pow 3; }
writeLn(sum)
end
end.</syntaxhighlight>


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>#!/usr/bin/perl
<syntaxhighlight lang="perl">#!/usr/bin/perl


use strict; # https://rosettacode.org/wiki/Sum_of_first_n_cubes
use strict; # https://rosettacode.org/wiki/Sum_of_first_n_cubes
Line 1,414: Line 1,551:


my $sum = 0;
my $sum = 0;
printf "%10d%s", $sum += $_ ** 3, $_ % 5 == 4 && "\n" for 0 .. 49;</lang>
printf "%10d%s", $sum += $_ ** 3, $_ % 5 == 4 && "\n" for 0 .. 49;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,431: Line 1,568:
=={{header|Phix}}==
=={{header|Phix}}==
The commented out line is based on the Factor entry, and is clearly a much faster way to get the same results.
The commented out line is based on the Factor entry, and is clearly a much faster way to get the same results.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight 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: #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: #000080;font-style:italic;">--function sum_first_n_cubes(integer n) return power(n*(n+1)/2,2) end function</span>
<span style="color: #000080;font-style:italic;">--function sum_first_n_cubes(integer n) return power(n*(n+1)/2,2) end 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: #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>
<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>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,447: Line 1,584:


=={{header|PILOT}}==
=={{header|PILOT}}==
<lang pilot>C :sum=0
<syntaxhighlight lang="pilot">C :sum=0
:n=0
:n=0
:max=50
:max=50
Line 1,456: Line 1,593:
T :#n: #sum
T :#n: #sum
J (n<max):*loop
J (n<max):*loop
E :</lang>
E :</syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'>1: 0
<pre style='height:50ex;'>1: 0
Line 1,510: Line 1,647:


=={{header|Plain English}}==
=={{header|Plain English}}==
<lang plainenglish>To run:
<syntaxhighlight lang="plainenglish">To run:
Start up.
Start up.
Show the sums of cubes given 49.
Show the sums of cubes given 49.
Line 1,523: Line 1,660:
Write the result then " " on the console without advancing.
Write the result then " " on the console without advancing.
If the counter is evenly divisible by 5, write "" on the console.
If the counter is evenly divisible by 5, write "" on the console.
Repeat.</lang>
Repeat.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,539: Line 1,676:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>cubeSum: procedure options(main);
<syntaxhighlight lang="pli">cubeSum: procedure options(main);
declare (i, csum) fixed decimal(7);
declare (i, csum) fixed decimal(7);
csum = 0;
csum = 0;
Line 1,547: Line 1,684:
if mod(i,5) = 4 then put skip;
if mod(i,5) = 4 then put skip;
end;
end;
end cubeSum;</lang>
end cubeSum;</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,562: Line 1,699:
=={{header|PL/M}}==
=={{header|PL/M}}==
The original 8080 PL/M compiler only supported 8 and 16 bit unsigned arithmetic, so this sample only shows the sums < 65536.
The original 8080 PL/M compiler only supported 8 and 16 bit unsigned arithmetic, so this sample only shows the sums < 65536.
<lang pli>100H: /* SHOW THE SUMS OF THE FIRST N CUBES, 0 <= N < 23 */
<syntaxhighlight lang="pli">100H: /* SHOW THE SUMS OF THE FIRST N CUBES, 0 <= N < 23 */


/* CP/M BDOS SYSTEM CALL */
/* CP/M BDOS SYSTEM CALL */
Line 1,592: Line 1,729:
END;
END;


EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,603: Line 1,740:
===Python :: Procedural===
===Python :: Procedural===
{{trans|FreeBASIC}}
{{trans|FreeBASIC}}
<lang python>
<syntaxhighlight lang="python">
def main():
def main():
fila = 0
fila = 0
Line 1,623: Line 1,760:


if __name__ == '__main__': main()
if __name__ == '__main__': main()
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,644: Line 1,781:
===Python :: Functional===
===Python :: Functional===


<lang python>'''Sum of first N cubes'''
<syntaxhighlight lang="python">'''Sum of first N cubes'''


from math import factorial
from math import factorial
Line 1,714: Line 1,851:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100 225 441 784 1296 2025
<pre> 0 1 9 36 100 225 441 784 1296 2025
Line 1,725: Line 1,862:
or, as a scanning accumulation:
or, as a scanning accumulation:


<lang python>'''Sum of first N cubes'''
<syntaxhighlight lang="python">'''Sum of first N cubes'''


from itertools import accumulate
from itertools import accumulate
Line 1,783: Line 1,920:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,798: Line 1,935:
=={{header|Quackery}}==
=={{header|Quackery}}==


<lang Quackery> $ "" 0
<syntaxhighlight lang="quackery"> $ "" 0
50 times
50 times
[ i^ 3 ** +
[ i^ 3 ** +
Line 1,805: Line 1,942:
space join ] ]
space join ] ]
drop
drop
nest$ 65 wrap$</lang>
nest$ 65 wrap$</syntaxhighlight>


{{out}}
{{out}}
Line 1,818: Line 1,955:
=={{header|R}}==
=={{header|R}}==
This only takes one line.
This only takes one line.
<lang rsplus>cumsum((0:49)^3)</lang>
<syntaxhighlight lang="rsplus">cumsum((0:49)^3)</syntaxhighlight>
{{out}}
{{out}}
<pre> [1] 0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400
<pre> [1] 0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400
Line 1,826: Line 1,963:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>my @sums_of_all_cubes = [\+] ^Inf X** 3;
<syntaxhighlight lang="raku" line>my @sums_of_all_cubes = [\+] ^Inf X** 3;


say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);</lang>
say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,839: Line 1,976:


=={{header|Red}}==
=={{header|Red}}==
<lang rebol>Red ["Sum of cubes"]
<syntaxhighlight lang="rebol">Red ["Sum of cubes"]


sum: 0
sum: 0
Line 1,846: Line 1,983:
prin pad sum 8
prin pad sum 8
if i % 10 = 0 [prin newline]
if i % 10 = 0 [prin newline]
]</lang>
]</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,857: Line 1,994:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program finds and displays a number of sums of the first N cubes, where N < 50 */
<syntaxhighlight lang="rexx">/*REXX program finds and displays a number of sums of the first N cubes, where N < 50 */
parse arg n cols . /*obtain optional argument from the CL.*/
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 1,882: Line 2,019:
exit 0 /*stick a fork in it, we're all done. */
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>
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}
(Shown at five-sixth size.)
(Shown at five-sixth size.)
Line 1,900: Line 2,037:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
see "working..." + nl
see "working..." + nl
see "Sum of first n cubes:" + nl
see "Sum of first n cubes:" + nl
Line 1,920: Line 2,057:
see "Found " + row + " cubes" + nl
see "Found " + row + " cubes" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,938: Line 2,075:
done...
done...
</pre>
</pre>

=={{header|RPL}}==
≪ { } 0 0 49 '''FOR''' n n 3 ^ + SWAP OVER + SWAP '''NEXT''' DROP ≫ EVAL
{{out}}
<pre>
1: { 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 }
</pre>
===Direct calculation===
≪ DUP 1 + * 2 / SQ ≫ ''''∑CUBE'''' STO
49 '''∑CUBE'''
{{out}}
<pre>
1: 1500625
</pre>

=={{header|Ruby}}==
<syntaxhighlight lang="ruby">sum = 0
(0...50).each do |n|
print (sum += n**3).to_s.ljust(10)
puts "" if (n+1) % 10 == 0
end
</syntaxhighlight>
{{out}}
<pre>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 </pre>


=={{header|Rust}}==
=={{header|Rust}}==
<lang rust>fn main() {
<syntaxhighlight lang="rust">fn main() {
(0..50)
(0..50)
.scan(0, |sum, x| {
.scan(0, |sum, x| {
Line 1,955: Line 2,120:
}
}
});
});
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,972: Line 2,137:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const proc: main is func
const proc: main is func
Line 1,984: Line 2,149:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,999: Line 2,164:
</pre>
</pre>


=={{header|SETL}}==
<syntaxhighlight lang="setl">program sum_of_first_cubes;
cubes := [n**3 : n in [0..49]];

loop for i in [2..#cubes] do
cubes(i) +:= cubes(i-1);
end loop;

printtab(cubes, 5, 10);

proc printtab(list, cols, width);
lines := [list(k..cols+k-1) : k in [1, cols+1..#list]];
loop for line in lines do
print(+/[lpad(str item, width+1) : item in line]);
end loop;
end proc;
end program;</syntaxhighlight>
{{out}}
<pre> 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</pre>
=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>0..49 -> map { .faulhaber_sum(3) }.slices(5).each { .join(' ').say }</lang>
<syntaxhighlight lang="ruby">0..49 -> map { .faulhaber_sum(3) }.slices(5).each { .join(' ').say }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,013: Line 2,206:
672400 741321 815409 894916 980100
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625
1071225 1168561 1272384 1382976 1500625
</pre>

=={{header|VTL-2}}==
Based on the TinyBASIC sample but with multiple sums per line and showing the first 23 values as VTL-2 has unsigned 16-bit integers.
<syntaxhighlight lang="vtl2">
10 C=0
20 N=0
30 C=N*N*N+C
40 ?=C
50 $=9
60 N=N+1
70 #=N/6*0+%>1*90
80 ?=""
90 #=N<23*30
</syntaxhighlight>
{{out}}
<pre>
0 1 9 36 100 225
441 784 1296 2025 3025 4356
6084 8281 11025 14400 18496 23409
29241 36100 44100 53361 64009
</pre>
</pre>


=={{header|Wren}}==
=={{header|Wren}}==
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="wren">import "./fmt" for Fmt


System.print("Cumulative sums of the first 50 cubes:")
System.print("Cumulative sums of the first 50 cubes:")
Line 2,025: Line 2,239:
if ((n % 10) == 9) System.print()
if ((n % 10) == 9) System.print()
}
}
System.print()</lang>
System.print()</syntaxhighlight>


{{out}}
{{out}}
Line 2,038: Line 2,252:


=={{header|X86 Assembly}}==
=={{header|X86 Assembly}}==
<lang asm> 1 ;Assemble with: tasm, tlink /t
<syntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
2 0000 .model tiny
3 0000 .code
3 0000 .code
Line 2,096: Line 2,310:
57 0156 C3 ret
57 0156 C3 ret
58
58
59 end start</lang>
59 end start</syntaxhighlight>


{{out}}
{{out}}
Line 2,113: Line 2,327:


=={{header|XPL0}}==
=={{header|XPL0}}==
<lang XPL0>int N, S;
<syntaxhighlight lang="xpl0">int N, S;
[S:= 0;
[S:= 0;
for N:= 0 to 49 do
for N:= 0 to 49 do
Line 2,121: Line 2,335:
if rem(N/5) = 4 then CrLf(0);
if rem(N/5) = 4 then CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,138: Line 2,352:


=={{header|Zig}}==
=={{header|Zig}}==
<lang zig>pub fn main() !void {
<syntaxhighlight lang="zig">pub fn main() !void {
const stdout = @import("std").io.getStdOut().writer();
const stdout = @import("std").io.getStdOut().writer();
Line 2,149: Line 2,363:
if (i % 5 == 4) try stdout.print("\n", .{});
if (i % 5 == 4) try stdout.print("\n", .{});
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100

Latest revision as of 22:07, 17 February 2024

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 < 50 (ie show 50 entries for n=0..49)

11l

Translation of: Nim
V s = 0
L(n) 50
   s += n * n * n
   print(String(s).rjust(7), end' I (n + 1) % 10 == 0 {"\n"} E ‘ ’)
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

Action!

INCLUDE "D2:REAL.ACT" ;from the Action! Tool Kit

PROC Main()
  BYTE i
  REAL sum,r3,tmp1,tmp2

  Put(125) PutE() ;clear the screen
  IntToReal(0,sum)
  IntToReal(3,r3)
  FOR i=0 TO 49
  DO
    IntToReal(i,tmp1)
    RealMult(tmp1,tmp1,tmp2)
    RealMult(tmp1,tmp2,tmp2)
    RealAdd(sum,tmp2,sum)
    PrintR(sum) Put(32)
    IF i MOD 4=3 THEN
      PutE()
    FI
  OD
RETURN
Output:

Screenshot from Atari 8-bit computer

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

Ada

with Ada.Text_Io;

procedure Sum_Of_First_N_Cubes is

   Columns : constant := 10;
   Width   : constant := 8;

   package Natural_Io is new Ada.Text_Io.Integer_Io (Natural);
   use Ada.Text_Io, Natural_Io;

   Sum : Natural := 0;
begin
   for N in 0 .. 49 loop
      Sum := Sum + N ** 3;
      Put (Sum, Width => Width);
      if N mod Columns = Columns - 1 then
         New_Line;
      end if;
   end loop;
   New_Line;
end Sum_Of_First_N_Cubes;
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

ALGOL 68

As noted in the second factor example, the sum of the cubes to n is (n(n + 1))/2)^2, i.e. the square of the sum of the numbers to n.

# show the sums of the first n cubes where 0 <= n < 50 #
FOR i FROM 0 TO 49 DO
    INT sum = ( i * ( i + 1 ) ) OVER 2;
    print( ( whole( sum * sum, -8 ) ) );
    IF i MOD 10 = 9 THEN print( ( newline ) ) FI
OD
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

ALGOL W

begin % show the sums of the cubes of n for 0 <= n < 50 %
    integer cubeSum;
    cubeSum := 0;
    for n := 0 until 49 do begin
        cubeSum := cubeSum + ( n * n * n );
        writeon( i_w := 8, s_w := 0, cubeSum );
        if n rem 10 = 9 then write()
    end for_n
end.
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

APL

10 5⍴+\0,(49)*3
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

AppleScript

------------------- SUM OF FIRST N CUBES -----------------

-- sumsOfFirstNCubes :: Int -> [Int]
on sumsOfFirstNCubes(n)
    script go
        on |λ|(a, x)
            a + (x ^ 3) as integer
        end |λ|
    end script
    
    scanl(go, 0, enumFromTo(1, n - 1))
end sumsOfFirstNCubes


--------------------------- TEST -------------------------
on run
    
    table(5, sumsOfFirstNCubes(50))
    
end run


------------------------- GENERIC ------------------------

-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
    if m  n then
        set lst to {}
        repeat with i from m to n
            set end of lst to i
        end repeat
        lst
    else
        {}
    end if
end enumFromTo


-- scanl :: (b -> a -> b) -> b -> [a] -> [b]
on scanl(f, startValue, xs)
    tell mReturn(f)
        set v to startValue
        set lng to length of xs
        set lst to {startValue}
        repeat with i from 1 to lng
            set v to |λ|(v, item i of xs, i, xs)
            set end of lst to v
        end repeat
        return lst
    end tell
end scanl


-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
    -- 2nd class handler function lifted into 1st class script wrapper. 
    if script is class of f then
        f
    else
        script
            property |λ| : f
        end script
    end if
end mReturn


------------------------ FORMATTING ----------------------

-- table :: Int -> [String] -> String
on table(n, xs)
    -- A list of strings formatted as
    -- right-justified rows of n columns.
    set vs to map(my str, xs)
    set w to length of last item of vs
    unlines(map(my unwords, ¬
        chunksOf(n, map(justifyRight(w, space), vs))))
end table


-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(k, xs)
    script
        on go(ys)
            set ab to splitAt(k, ys)
            set a to item 1 of ab
            if {}  a then
                {a} & go(item 2 of ab)
            else
                a
            end if
        end go
    end script
    result's go(xs)
end chunksOf


-- justifyRight :: Int -> Char -> String -> String
on justifyRight(n, cFiller)
    script
        on |λ|(txt)
            if n > length of txt then
                text -n thru -1 of ((replicate(n, cFiller) as text) & txt)
            else
                txt
            end if
        end |λ|
    end script
end justifyRight


-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
    -- The list obtained by applying f
    -- to each element of xs.
    tell mReturn(f)
        set lng to length of xs
        set lst to {}
        repeat with i from 1 to lng
            set end of lst to |λ|(item i of xs, i, xs)
        end repeat
        return lst
    end tell
end map


-- Egyptian multiplication - progressively doubling a list, appending
-- stages of doubling to an accumulator where needed for binary 
-- assembly of a target length
-- replicate :: Int -> String -> String
on replicate(n, s)
    -- Egyptian multiplication - progressively doubling a list, 
    -- appending stages of doubling to an accumulator where needed 
    -- for binary assembly of a target length
    script p
        on |λ|({n})
            n  1
        end |λ|
    end script
    
    script f
        on |λ|({n, dbl, out})
            if (n mod 2) > 0 then
                set d to out & dbl
            else
                set d to out
            end if
            {n div 2, dbl & dbl, d}
        end |λ|
    end script
    
    set xs to |until|(p, f, {n, s, ""})
    item 2 of xs & item 3 of xs
end replicate


-- splitAt :: Int -> [a] -> ([a], [a])
on splitAt(n, xs)
    if n > 0 and n < length of xs then
        if class of xs is text then
            {items 1 thru n of xs as text, ¬
                items (n + 1) thru -1 of xs as text}
        else
            {items 1 thru n of xs, items (n + 1) thru -1 of xs}
        end if
    else
        if n < 1 then
            {{}, xs}
        else
            {xs, {}}
        end if
    end if
end splitAt


-- str :: a -> String
on str(x)
    x as string
end str


-- unlines :: [String] -> String
on unlines(xs)
    -- A single string formed by the intercalation
    -- of a list of strings with the newline character.
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, linefeed}
    set s to xs as text
    set my text item delimiters to dlm
    s
end unlines


-- until :: (a -> Bool) -> (a -> a) -> a -> a
on |until|(p, f, x)
    set v to x
    set mp to mReturn(p)
    set mf to mReturn(f)
    repeat until mp's |λ|(v)
        set v to mf's |λ|(v)
    end repeat
    v
end |until|


-- unwords :: [String] -> String
on unwords(xs)
    set {dlm, my text item delimiters} to ¬
        {my text item delimiters, space}
    set s to xs as text
    set my text item delimiters to dlm
    return s
end unwords
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

Arturo

sumCubes: 0
loop split.every: 10 map 0..49 => [sumCubes: <= sumCubes + & ^ 3] 'a -> 
    print map a => [pad to :string & 7]
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

AutoHotkey

pn := 0, result := ""
loop 50 {
	n := SubStr("       " ((A_Index-1)**3 + pn), -6)
	result .= n (Mod(A_Index, 10)?"`t":"`n")
	pn := n
}
MsgBox % result
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

AWK

# syntax: GAWK -f SUM_OF_FIRST_N_CUBES.AWK
BEGIN {
    start = 0
    stop = 49
    for (i=start; i<=stop; i++) {
      sum += i * i * i
      printf("%7d%1s",sum,++count%10?"":"\n")
    }
    printf("\nSum of cubes %d-%d: %d\n",start,stop,count)
    exit(0)
}
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

Sum of cubes 0-49: 50

BASIC

BASIC256

Translation of: FreeBASIC
fila = 0
lenCubos = 49

cls
print "Suma de N cubos para n = [0..49]" + chr(10)

for n = 0 to lenCubos
	sumCubos = 0
	for m = 1 to n
		sumCubos += int(m ^ 3)
	next m
	fila += 1
	print "" + sumCubos + " ";
	#Print Using " ####### "; sumCubos;
	if (fila % 5) = 0 then print
next n

print chr(13) + "Encontrados " & fila & " cubos."
end

FreeBASIC

Dim As Integer fila = 0, lenCubos = 49, sumCubos

CLs
Print !"Suma de N cubos para n = [0..49]\n"

For n As Integer = 0 To lenCubos
    sumCubos = 0
    For m As Integer = 1 To n
        sumCubos += (m ^3)
    Next m
    fila += 1
    'Print "" & sumCubos & " ";
    Print Using " ####### "; sumCubos;
    If fila Mod 5 = 0 Then Print
Next n

Print !"\nEncontrados " & fila & " cubos."
Sleep
Output:
Suma de N cubos para n = [0..49]

       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

Encontrados 50 cubos.

GW-BASIC

10 FOR N=0 TO 49
20 C#=C#+N^3
30 PRINT C#;
40 NEXT N
Output:
 0  1  9  36  100  225  441  784  1296  2025  3025  4356  6084  8281  11025  14400  1

8496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884

164836  189225  216225  246016  278784  314721  354025  396900  443556  494209  5490

81 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 138297

6 1500625

QB64

For n%% = 0 To 49
    c& = c& + n%% ^ 3
    Print Using " ####### "; c&;
    If n%% Mod 5 = 4 Then Print
Next n%%
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

QBasic

Translation of: FreeBASIC
DEFLNG A-Z

fila = 0
lenCubos = 49

CLS
PRINT "Suma de N cubos para n = [0..49]" + CHR$(10)

FOR n = 0 TO lenCubos
    sumCubos = 0
    FOR m = 1 TO n
        sumCubos = sumCubos + (m ^ 3)
    NEXT m
    fila = fila + 1
    PRINT USING " ####### "; sumCubos;
    IF fila MOD 5 = 0 THEN PRINT
NEXT n

PRINT CHR$(13) + "Encontrados"; fila; "cubos."
END

Tiny BASIC

Works with: TinyBasic

Limited to 0 through 19 because integers only go up to 32767.

10 LET C = 0
20 LET N = 0
30 LET C = C + N*N*N
40 PRINT C
50 LET N = N + 1
60 IF N = 19 THEN END
70 GOTO 30

Yabasic

Translation of: FreeBASIC
fila = 0
lenCubos = 49

clear screen
print "Suma de N cubos para n = [0..49]\n"

for n = 0 to lenCubos
    sumCubos = 0
    for m = 1 to n
        sumCubos = sumCubos + (m ^3)
    next m
    fila = fila + 1
    print "", sumCubos, " ";
    if mod(fila, 5) = 0 then print : fi
next n

print "\nEncontrados ", fila, " cubos.\n"
end

BQN

5⥊+`(50)3
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  
                                          ┘

C

#include <stdio.h>

int main() {
    for (int i = 0, sum = 0; i < 50; ++i) {
        sum += i * i * i;
        printf("%7d%c", sum, (i + 1) % 5 == 0 ? '\n' : ' ');
    }
    return 0;
}
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

C#

No multiplication or exponentiation, just addition.

using System; using static System.Console;
class Program { static void Main(string[] args) {
    for (int i=0,j=-6,k=1,c=0,s=0;s<1600000;s+=c+=k+=j+=6)
      Write("{0,-7}{1}",s, (i+=i==3?-4:1)==0?"\n":" "); } }
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

C++

#include <array>
#include <cstdio>
#include <numeric>

void PrintContainer(const auto& vec)
{
    int count = 0;
    for(auto value : vec)
    {
        printf("%7d%c", value, ++count % 10 == 0 ? '\n' : ' ');
    }
}

int main()
{
    // define a lambda that cubes a value
    auto cube = [](auto x){return x * x * x;};

    // create an array and use iota to fill it with {0, 1, 2, ... 49}
    std::array<int, 50> a;
    std::iota(a.begin(), a.end(), 0);

    // transform_inclusive_scan will cube all of the values in the array and then
    // perform a partial sum from index 0 to n and place the result back into the
    // array at index n 
    std::transform_inclusive_scan(a.begin(), a.end(), a.begin(), std::plus{}, cube);
    PrintContainer(a);    
}
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

CLU

start_up = proc ()
    amount = 50
    po: stream := stream$primary_output()
    
    sum: int := 0
    for i: int in int$from_to(0,amount-1) do
        sum := sum + i ** 3
        stream$putright(po, int$unparse(sum), 8)
        if i//5 = 4 then stream$putc(po, '\n') end
    end
end start_up
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

COBOL

       IDENTIFICATION DIVISION.
       PROGRAM-ID.  SUM-OF-CUBES.

       DATA DIVISION.
       WORKING-STORAGE SECTION.
       01 VARIABLES.
          03 STEP         PIC 99.
          03 CUBE         PIC 9(7).
          03 CUBE-SUM     PIC 9(7) VALUE 0.
 
       01 OUTPUT-FORMAT.
          03 OUT-LINE     PIC X(40) VALUE SPACES.
          03 OUT-PTR      PIC 99 VALUE 1.
          03 OUT-NUM      PIC Z(7)9.

       PROCEDURE DIVISION.
       BEGIN.
           PERFORM ADD-CUBE VARYING STEP FROM 0 BY 1
               UNTIL STEP IS EQUAL TO 50.
           STOP RUN.

       ADD-CUBE.
           COMPUTE CUBE = STEP ** 3.
           ADD CUBE TO CUBE-SUM.
           MOVE CUBE-SUM TO OUT-NUM.
           STRING OUT-NUM DELIMITED BY SIZE INTO OUT-LINE
               WITH POINTER OUT-PTR.
           IF OUT-PTR IS EQUAL TO 41,
               DISPLAY OUT-LINE,
               MOVE 1 TO OUT-PTR.
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

Comal

0010 ZONE 8
0020 sum:=0
0030 FOR cube:=0 TO 49 DO
0040   sum:+cube^3
0050   PRINT sum,
0060   IF cube MOD 5=4 THEN PRINT
0070 ENDFOR cube
0080 END
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

Cowgol

include "cowgol.coh";

sub cube(n: uint32): (r: uint32) is
    r := n * n * n;
end sub;

var i: uint8 := 0;
var sum: uint32 := 0;
while i < 50 loop
    sum := sum + cube(i as uint32);
    print_i32(sum);
    i := i + 1;
    if i % 10 == 0 then
        print_nl();
    else
        print_char(' ');
    end if;
end loop;
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

Delphi

Works with: Delphi version 6.0


procedure ShowSumFirst50Cubes(Memo: TMemo);
var I,Sum: integer;
var S: string;
begin
S:='';
Sum:=0;
for I:=0 to 50-1 do
	begin
	Sum:=Sum+I*I*I;
	S:=S+Format('%11.0n',[Sum+0.0]);
	if (I mod 5)=4 then S:=S+CRLF;
	end;
Memo.Lines.Add(S);
end;
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


Elapsed Time: 1.428 ms.


Draco

proc nonrec cube(ulong n) ulong:
    n * n * n 
corp

proc nonrec main() void:
    ulong sum;
    byte n;
    sum := 0;
    for n from 0 upto 49 do
        sum := sum + cube(n);
        write(sum:8);
        if n % 5 = 4 then writeln() fi
    od
corp
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

EasyLang

for i = 0 to 49
   sum += i * i * i
   write sum & " "
.

Excel

LAMBDA

Binding the names SUMNCUBES and BINCOEFF to the following lambda expressions in the Name Manager of the Excel WorkBook:

(See LAMBDA: The ultimate Excel worksheet function)

SUMNCUBES
=LAMBDA(n,
    BINCOEFF(1 + n)(2) ^ 2
)

BINCOEFF
=LAMBDA(n,
    LAMBDA(k,
        IF(n < k,
            0,
            QUOTIENT(FACT(n), FACT(k) * FACT(n - k))
        )
    )
)

The single formula in cell B2 below defines a dynamic array which populates the whole B2:K6 grid:

Output:
fx =SUMNCUBES( SEQUENCE(5, 10, 0, 1) )
A B C D E F G H I J K
1 Sum of N cubes for n = [0..49]
2 0 1 9 36 100 225 441 784 1296 2025
3 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100
4 44100 53361 64009 76176 89401 105625 123201 142129 164025 189225
5 216225 245025 278784 313600 352836 396900 443556 492804 549081 608400
6 670761 739600 815409 894916 980100 1069156 1168561 1270129 1382976 1500625

F#

// Sum of cubes: Nigel Galloway. May 20th., 2021
let fN g=g*g*g in Seq.initInfinite((+)1>>fN)|>Seq.take 49|>Seq.scan((+))(0)|>Seq.iter(printf "%d "); printfn ""
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

Factor

Works with: Factor version 0.99 2021-02-05
USING: grouping math math.functions prettyprint sequences ;

50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.
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
USING: grouping kernel math prettyprint sequences ;

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

50 <iota> [ triangular sq ] map 10 group simple-table.
Output:

As above.

Fermat

c:=0
for n = 0 to 49 do
    c:=c+n^3;
    !(c,' ');
od;
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

Forth

Works with: Gforth
: sum-cubes ( n -- )
  0 swap 0 do
    i i i * * + dup 7 .r
    i 1+ 5 mod 0= if cr else space then
  loop drop ;

50 sum-cubes
bye
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

Frink

sum = 0
result = new array
for n = 0 to 49
{
   sum = sum + n^3
   result.push[sum]
}

println[formatTable[columnize[result, 10], "right"]]
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

Go

Translation of: Wren
Library: Go-rcu
package main

import (
    "fmt"
    "rcu"
)

func main() {
    fmt.Println("Cumulative sums of the first 50 cubes:")
    sum := 0
    for n := 0; n < 50; n++ {
        sum += n * n * n
        fmt.Printf("%9s ", rcu.Commatize(sum))
        if n%10 == 9 {
            fmt.Println()
        }
    }
    fmt.Println()
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 

Haskell

import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Text.Printf (printf)

------------------- SUM OF FIRST N CUBES -----------------

sumOfFirstNCubes :: Integer -> Integer
sumOfFirstNCubes =
  (^ 2)
    . flip binomialCoefficient 2
    . succ


--------------------------- TEST -------------------------
main :: IO ()
main =
  putStrLn $
    table " " $
      chunksOf 10 $
        show . sumOfFirstNCubes <$> [0 .. 49]


------------------------- GENERIC ------------------------

binomialCoefficient :: Integer -> Integer -> Integer
binomialCoefficient n k
  | n < k = 0
  | otherwise =
    div
      (factorial n)
      (factorial k * factorial (n - k))

factorial :: Integer -> Integer
factorial = product . enumFromTo 1

------------------------- DISPLAY ------------------------

table :: String -> [[String]] -> String
table gap rows =
  let ws = maximum . fmap length <$> transpose rows
      pw = printf . flip intercalate ["%", "s"] . show
   in unlines $ intercalate gap . zipWith pw ws <$> rows
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


Or, in terms of scanl:

import Data.List (intercalate, scanl, transpose)
import Data.List.Split (chunksOf)
import Text.Printf (printf)

------------------- SUM OF FIRST N CUBES -----------------

sumsOfFirstNCubes :: Int -> [Int]
sumsOfFirstNCubes n =
  scanl
    (\a -> (a +) . (^ 3))
    0
    [1 .. pred n]

--------------------------- TEST -------------------------
main :: IO ()
main =
  (putStrLn . table " " . chunksOf 5) $
    show <$> sumsOfFirstNCubes 50

------------------------- DISPLAY ------------------------

table :: String -> [[String]] -> String
table gap rows =
  let ws = maximum . fmap length <$> transpose rows
      pw = printf . flip intercalate ["%", "s"] . show
   in unlines $ intercalate gap . zipWith pw ws <$> rows
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

J

10 5$+/\(i.^3:)50x
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

If we wanted specifically the sum of positive integer cubes up through n it should be faster to use 0 0 1r4 1r2 1r4&p.. For example:

   0 0 1r4 1r2 1r4&p. 49
1500625

JavaScript

(() => {
    "use strict";

    // -------------- SUM OF FIRST N CUBES ---------------

    // sumsOfFirstNCubes :: Int -> [Int]
    const sumsOfFirstNCubes = n =>
        // Cumulative sums of first n cubes.
        scanl(
            a => x => a + (x ** 3)
        )(0)(
            enumFromTo(1)(n - 1)
        );


    // ---------------------- TEST -----------------------
    // main :: IO ()
    const main = () =>
        table("  ")(justifyRight)(
            chunksOf(5)(
                sumsOfFirstNCubes(50)
                .map(x => `${x}`)
            )
        );


    // --------------------- GENERIC ---------------------

    // enumFromTo :: Int -> Int -> [Int]
    const enumFromTo = m =>
        n => Array.from({
            length: 1 + n - m
        }, (_, i) => m + i);


    // scanl :: (b -> a -> b) -> b -> [a] -> [b]
    const scanl = f => startValue => xs =>
        // The series of interim values arising
        // from a catamorphism. Parallel to foldl.
        xs.reduce((a, x) => {
            const v = f(a[0])(x);

            return [v, a[1].concat(v)];
        }, [startValue, [startValue]])[1];


    // ------------------- FORMATTING --------------------

    // chunksOf :: Int -> [a] -> [[a]]
    const chunksOf = n => {
        // xs split into sublists of length n.
        // The last sublist will be short if n
        // does not evenly divide the length of xs .
        const go = xs => {
            const chunk = xs.slice(0, n);

            return 0 < chunk.length ? (
                [chunk].concat(
                    go(xs.slice(n))
                )
            ) : [];
        };

        return go;
    };


    // compose (<<<) :: (b -> c) -> (a -> b) -> a -> c
    const compose = (...fs) =>
        // A function defined by the right-to-left
        // composition of all the functions in fs.
        fs.reduce(
            (f, g) => x => f(g(x)),
            x => x
        );


    // flip :: (a -> b -> c) -> b -> a -> c
    const flip = op =>
        // The binary function op with
        // its arguments reversed.
        1 < op.length ? (
            (a, b) => op(b, a)
        ) : (x => y => op(y)(x));


    // intercalate :: String -> [String] -> String
    const intercalate = s =>
        // The concatenation of xs
        // interspersed with copies of s.
        xs => xs.join(s);


    // justifyRight :: Int -> Char -> String -> String
    const justifyRight = n =>
        // The string s, preceded by enough padding (with
        // the character c) to reach the string length n.
        c => s => Boolean(s) ? (
            s.padStart(n, c)
        ) : "";


    // table :: String ->
    // (Int -> Char -> String -> String) ->
    // [[String]] -> String
    const table = gap =>
        // A tabulation of rows of string values,
        // with a specified gap between columns,
        // and choice of cell alignment function
        // (justifyLeft | center | justifyRight)
        alignment => rows => {
            const
                colWidths = transpose(rows).map(
                    row => Math.max(
                        ...row.map(x => x.length)
                    )
                );

            return rows.map(
                compose(
                    intercalate(gap),
                    zipWith(
                        flip(alignment)(" ")
                    )(colWidths)
                )
            ).join("\n");
        };


    // transpose :: [[a]] -> [[a]]
    const transpose = rows => {
        // If any rows are shorter than those that follow,
        // their elements are skipped:
        // > transpose [[10,11],[20],[],[30,31,32]]
        //             == [[10,20,30],[11,31],[32]]
        const go = xss =>
            0 < xss.length ? (() => {
                const
                    h = xss[0],
                    t = xss.slice(1);

                return 0 < h.length ? [
                    [h[0]].concat(t.reduce(
                        (a, xs) => a.concat(
                            0 < xs.length ? (
                                [xs[0]]
                            ) : []
                        ),
                        []
                    ))
                ].concat(go([h.slice(1)].concat(
                    t.map(xs => xs.slice(1))
                ))) : go(t);
            })() : [];

        return go(rows);
    };


    // zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    const zipWith = f =>
        // A list constructed by zipping with a
        // custom function, rather than with the
        // default tuple constructor.
        xs => ys => xs.map(
            (x, i) => f(x)(ys[i])
        ).slice(
            0, Math.min(xs.length, ys.length)
        );

    // MAIN ---
    return main();
})();
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

jq

Works with: jq

Works with gojq, the Go implementation of jq

# For the sake of stream-processing efficiency:
def add(s): reduce s as $x (0; . + $x);

def sum_of_cubes: add(range(0;.) | .*.*.);

The task

range(0;50) | sum_of_cubes as $sum
| "\(.) => \($sum)"
Output:
0 => 0
1 => 0
2 => 1
3 => 9
4 => 36
5 => 100
...
45 => 980100
46 => 1071225
47 => 1168561
48 => 1272384
49 => 1382976

Using gojq, the Go implementation of jq, unbounded-precision integer arithmetic allows e.g.

1000000 | sum_of_cubes #=> 249999500000250000000000

Julia

cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))

cubesumstil()
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, and using the REPL, note that recent versions of Julia implement the accumulate function:

julia> println(accumulate((x, y) -> x + y^3, 0:49))

[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]

K

Works with: ngn/k
5 10#+\{x*x*x}[!50]
(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)

MAD

           NORMAL MODE IS INTEGER
           SUM = 0
           THROUGH LOOP, FOR STEP = 0, 1, STEP.GE.50
           SUM = SUM + STEP * STEP * STEP
LOOP       PRINT FORMAT FMT, SUM
           VECTOR VALUES FMT = $I9*$
           END OF PROGRAM
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

Mathematica/Wolfram Language

Accumulate[Range[0, 49]^3]
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}

Nim

import strutils

var s = 0
for n in 0..49:
  s += n * n * n
  stdout.write ($s).align(7), if (n + 1) mod 10 == 0: '\n' else: ' '
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

PARI/GP

c=0;for(n=0,49,c=c+n^3;print(c))

Pascal

program sumOfFirstNCubes(output);
const
	N = 49;
var
	i: integer;
	sum: integer;
begin
	sum := 0;
	for i := 0 to N do
	begin
		sum := sum + sqr(i) * i;
		{ In Extended Pascal you could also write:
		sum := sum + i pow 3; }
		writeLn(sum)
	end
end.

Perl

#!/usr/bin/perl

use strict; # https://rosettacode.org/wiki/Sum_of_first_n_cubes
use warnings;

my $sum = 0;
printf "%10d%s", $sum += $_ ** 3, $_ % 5 == 4 && "\n" for 0 .. 49;
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

Phix

The commented out line is based on the Factor entry, and is clearly a much faster way to get the same results.

function sum_first_n_cubes(integer n) return sum(sq_power(tagset(n),3)) end function
--function sum_first_n_cubes(integer n) return power(n*(n+1)/2,2) 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

PILOT

C :sum=0
  :n=0
  :max=50
*loop
C :cube=n*(n*#n)
  :sum=sum+cube
  :n=n+1
T :#n: #sum
J (n<max):*loop
E :
Output:
1: 0
2: 1
3: 9
4: 36
5: 100
6: 225
7: 441
8: 784
9: 1296
10: 2025
11: 3025
12: 4356
13: 6084
14: 8281
15: 11025
16: 14400
17: 18496
18: 23409
19: 29241
20: 36100
21: 44100
22: 53361
23: 64009
24: 76176
25: 90000
26: 105625
27: 123201
28: 142884
29: 164836
30: 189225
31: 216225
32: 246016
33: 278784
34: 314721
35: 354025
36: 396900
37: 443556
38: 494209
39: 549081
40: 608400
41: 672400
42: 741321
43: 815409
44: 894916
45: 980100
46: 1071225
47: 1168561
48: 1272384
49: 1382976
50: 1500625

Plain English

To run:
Start up.
Show the sums of cubes given 49.
Wait for the escape key.
Shut down.

To show the sums of cubes given a number:
If a counter is past the number, exit.
Put the counter plus 1 times the counter into a result number.
Cut the result in half.
Raise the result to 2.
Write the result then " " on the console without advancing.
If the counter is evenly divisible by 5, write "" on the console.
Repeat.
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 

PL/I

cubeSum: procedure options(main);
    declare (i, csum) fixed decimal(7);
    csum = 0;
    do i=0 to 49;
        csum = csum + i * i * i;
        put list(csum);
        if mod(i,5) = 4 then put skip;
    end;
end cubeSum;
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

PL/M

The original 8080 PL/M compiler only supported 8 and 16 bit unsigned arithmetic, so this sample only shows the sums < 65536.

100H: /* SHOW THE SUMS OF THE FIRST N CUBES, 0 <= N < 23                    */

   /* CP/M BDOS SYSTEM CALL                                                 */
   BDOS: PROCEDURE( FN, ARG ); DECLARE FN BYTE, ARG ADDRESS; GOTO 5; END;
   /* I/O ROUTINES                                                          */
   PRINT$CHAR:   PROCEDURE( C ); DECLARE C BYTE;    CALL BDOS( 2, C ); END;
   PRINT$STRING: PROCEDURE( S ); DECLARE S ADDRESS; CALL BDOS( 9, S ); END;
   PRINT$NUMBER: PROCEDURE( N );
      DECLARE N ADDRESS;
      DECLARE V ADDRESS, N$STR( 6 ) BYTE, W BYTE;
      N$STR( W := LAST( N$STR ) ) = '$';
      N$STR( W := W - 1         ) = '0' + ( ( V := N ) MOD 10 );
      DO WHILE( W > 0 );
         N$STR( W := W - 1 ) = ' ';
         IF V > 0 THEN DO;
            IF ( V := V / 10 ) > 0 THEN N$STR( W ) = '0' + ( V MOD 10 );
         END;
      END;
      CALL PRINT$STRING( .N$STR );
   END PRINT$NUMBER;

   /* SHOW SUMS OF CUBES                                                    */
   DECLARE ( I, SUM ) ADDRESS;
   DO I = 0 TO 22;
      SUM = ( I * ( I + 1 ) ) / 2;
      CALL PRINT$CHAR( ' ' );
      CALL PRINT$NUMBER( SUM * SUM );
      IF I MOD 10 = 9 THEN CALL PRINT$STRING( .( 0DH, 0AH, '$' ) );
   END;

EOF
Output:
     0     1     9    36   100   225   441   784  1296  2025
  3025  4356  6084  8281 11025 14400 18496 23409 29241 36100
 44100 53361 64009

Python

Python :: Procedural

Translation of: FreeBASIC
def main():
    fila = 0
    lenCubos = 51

    print("Suma de N cubos para n = [0..49]\n")

    for n in range(1, lenCubos):
        sumCubos = 0
        for m in range(1, n):
            sumCubos = sumCubos + (m ** 3)
            
        fila += 1
        print(f'{sumCubos:7} ', end='')
        if fila % 5 == 0:
            print(" ")

    print(f"\nEncontrados {fila} cubos.")

if __name__ == '__main__': main()
Output:
Suma de N cubos para n = [0..49]

      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  

Encontrados 50 cubos.

Python :: Functional

'''Sum of first N cubes'''

from math import factorial


# sumOfFirstNCubes :: Int -> Int
def sumOfFirstNCubes(n):
    '''The sum of the first n cubes.'''
    return binomialCoefficient(1 + n)(2) ** 2


# ------------------------- TEST -------------------------
# main :: IO ()
def main():
    '''First fifty values (N drawn from [0 .. 49])
    '''
    print(
        table(10)([
            str(sumOfFirstNCubes(n)) for n
            in range(0, 1 + 49)
        ])
    )


# ----------------------- GENERIC ------------------------

# binomialCoefficient :: Int -> Int -> Int
def binomialCoefficient(n):
    '''The coefficient of the term x^k in the polynomial
       expansion of the binomial power (1 + x)^n
    '''
    def go(k):
        return 0 if n < k else factorial(n) // (
            factorial(k) * factorial(n - k)
        )
    return go


# ----------------------- DISPLAY ------------------------

# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
    '''A series of lists of length n, subdividing the
       contents of xs. Where the length of xs is not evenly
       divisible, the final list will be shorter than n.
    '''
    def go(xs):
        return (
            xs[i:n + i] for i in range(0, len(xs), n)
        ) if 0 < n else None
    return go


# table :: Int -> [String] -> String
def table(n):
    '''A list of strings formatted as
       right-justified rows of n columns.
    '''
    def go(xs):
        w = len(xs[-1])
        return '\n'.join(
            ' '.join(row) for row in chunksOf(n)([
                s.rjust(w, ' ') for s in xs
            ])
        )
    return go


# MAIN ---
if __name__ == '__main__':
    main()
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


or, as a scanning accumulation:

'''Sum of first N cubes'''

from itertools import accumulate


# sumsOfFirstNCubes :: Int -> [Int]
def sumsOfFirstNCubes(n):
    '''Cumulative sums of the first N cubes.
    '''
    def go(a, x):
        return a + x ** 3

    return accumulate(range(0, n), go)


# ------------------------- TEST -------------------------
# main :: IO ()
def main():
    '''Cumulative sums of first 50 cubes'''
    print(
        table(5)([
            str(n) for n in sumsOfFirstNCubes(50)
        ])
    )


# ---------------------- FORMATTING ----------------------

# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
    '''A series of lists of length n, subdividing the
       contents of xs. Where the length of xs is not evenly
       divisible, the final list will be shorter than n.
    '''
    def go(xs):
        return (
            xs[i:n + i] for i in range(0, len(xs), n)
        ) if 0 < n else None
    return go


# table :: Int -> [String] -> String
def table(n):
    '''A list of strings formatted as
       right-justified rows of n columns.
    '''
    def go(xs):
        w = len(xs[-1])
        return '\n'.join(
            ' '.join(row) for row in chunksOf(n)([
                s.rjust(w, ' ') for s in xs
            ])
        )
    return go


# MAIN ---
if __name__ == '__main__':
    main()
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

Quackery

  $ "" 0 
  50 times 
    [ i^ 3 ** +
      dup dip 
        [ number$ join
          space join  ] ]
  drop 
  nest$ 65 wrap$
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

R

This only takes one line.

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

Raku

my @sums_of_all_cubes = [\+] ^Inf X** 3;

say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);
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

Red

Red ["Sum of cubes"]

sum: 0
repeat i 50 [
    sum: i - 1 ** 3 + sum
    prin pad sum 8
    if i % 10 = 0 [prin newline]
]
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

/*REXX program finds and displays a number of sums of the first  N  cubes, where N < 50 */
parse arg n cols .                               /*obtain optional argument from the CL.*/
if    n=='' |    n==","  then    n=   50         /*Not specified?  Then use the default.*/
if cols=='' | cols==","  then cols=   10         /* "      "         "   "   "     "    */
w= 12                                            /*width of a number in any column.     */
                     title= ' cube sums,  where  N  < '     commas(n)
say ' index │'center(title,  1 + cols*(w+1)     )
say '───────┼'center(""   ,  1 + cols*(w+1), '─')
found= 0;                  idx= 0                /*initialize the number for the index. */
$=;                        sum= 0                /*a list of the sum of  N  cubes.  .   */
     do j=0  for n;        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)       title
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 ?
output   when using the default inputs:

(Shown at five-sixth size.)

 index │                                                    cube sums,  where  N  <  50
───────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   0   │            0            1            9           36          100          225          441          784        1,296        2,025
  10   │        3,025        4,356        6,084        8,281       11,025       14,400       18,496       23,409       29,241       36,100
  20   │       44,100       53,361       64,009       76,176       90,000      105,625      123,201      142,884      164,836      189,225
  30   │      216,225      246,016      278,784      314,721      354,025      396,900      443,556      494,209      549,081      608,400
  40   │      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
───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Found  50  cube sums,  where  N  <  50

Ring

see "working..." + nl
see "Sum of first n cubes:" + nl
row = 0
lenCubes = 49

for n = 0 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
Output:
working...
Sum of first n cubes:
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 
Found 50 cubes
done...

RPL

≪ { } 0 0 49 FOR n n 3 ^ + SWAP OVER + SWAP NEXT DROP ≫ EVAL
Output:
1: { 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 }

Direct calculation

≪ DUP 1 + * 2 / SQ ≫ '∑CUBE' STO
49 ∑CUBE
Output:
1: 1500625

Ruby

sum = 0
(0...50).each do |n|
  print (sum += n**3).to_s.ljust(10)
  puts "" if (n+1) % 10 == 0
end
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 

Rust

fn main() {
    (0..50)
        .scan(0, |sum, x| {
            *sum += x * x * x;
            Some(*sum)
        })
        .enumerate()
        .for_each(|(i, n)| {
            print!("{:7}", n);
            if (i + 1) % 5 == 0 {
                println!();
            } else {
                print!(" ");
            }
        });
}
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

Seed7

$ include "seed7_05.s7i";

const proc: main is func
  local
    var integer: n is 0;
  begin
    for n range 0 to 49 do
      write((n * (n + 1) >> 1) ** 2 lpad 8);
      if n rem 5 = 4 then
        writeln;
      end if;
    end for;
  end func;
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

SETL

program sum_of_first_cubes;
    cubes := [n**3 : n in [0..49]];

    loop for i in [2..#cubes] do
        cubes(i) +:= cubes(i-1);
    end loop;

    printtab(cubes, 5, 10);

    proc printtab(list, cols, width);
        lines := [list(k..cols+k-1) : k in [1, cols+1..#list]];
        loop for line in lines do
            print(+/[lpad(str item, width+1) : item in line]);
        end loop;
    end proc;
end program;
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

Sidef

0..49 -> map { .faulhaber_sum(3) }.slices(5).each { .join(' ').say }
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

VTL-2

Based on the TinyBASIC sample but with multiple sums per line and showing the first 23 values as VTL-2 has unsigned 16-bit integers.

10 C=0
20 N=0
30 C=N*N*N+C
40 ?=C
50 $=9
60 N=N+1
70 #=N/6*0+%>1*90
80 ?=""
90 #=N<23*30
Output:
0       1       9       36      100     225
441     784     1296    2025    3025    4356
6084    8281    11025   14400   18496   23409
29241   36100   44100   53361   64009

Wren

import "./fmt" for Fmt

System.print("Cumulative sums of the first 50 cubes:")
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()
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 

X86 Assembly

      1                                  ;Assemble with: tasm, tlink /t
      2     0000                                 .model  tiny
      3     0000                                 .code
      4                                          .386
      5                                          org     100h            ;.com program starts here
      6
      7                                  ; eax: working register
      8                                  ; ebx: 10 for divide
      9                                  ; cx:  numout digit counter
     10                                  ; edx: divide remainder
     11                                  ; esi: Sum
     12                                  ; edi: N
     13                                  ; bp:  column position for tab
     14
     15     0100  66| 33 F6              start:  xor     esi, esi        ;Sum:= 0
     16     0103  33 ED                          xor     bp, bp          ;reset column position
     17     0105  66| 33 FF                      xor     edi, edi        ;N:= 0
     18     0108  66| 8B C7              sum:    mov     eax, edi        ;Sum:= N^3 + Sum
     19     010B  66| F7 EF                      imul    edi             ;eax:= edi^3 + esi
     20     010E  66| F7 EF                      imul    edi
     21     0111  66| 03 C6                      add     eax, esi
     22
     23     0114  66| 8B F0                      mov     esi, eax
     24     0117  66| BB 0000000A                mov     ebx, 10         ;output number in eax
     25     011D  33 C9                          xor     cx, cx          ;digit counter
     26     011F  66| 99                 no10:   cdq                     ;edx:= 0 (extend sign of eax into edx)
     27     0121  66| F7 FB                      idiv    ebx             ;(edx:eax)/ebx
     28     0124  52                             push    dx              ;save remainder
     29     0125  41                             inc     cx              ;count digit
     30     0126  66| 85 C0                      test    eax, eax        ;loop for all digits
     31     0129  75 F4                          jne     no10
     32
     33     012B  58                     no20:   pop     ax              ;get remainder
     34     012C  04 30                          add     al, '0'         ;convert to ASCII
     35     012E  CD 29                          int     29h             ;output digit
     36     0130  45                             inc     bp              ;bump column position
     37     0131  E2 F8                          loop    no20            ;loop for cx digits
     38
     39     0133  B0 20                  tab:    mov     al, 20h         ;output spaces until tab stop
     40     0135  CD 29                          int     29h
     41     0137  45                             inc     bp              ;bump column position
     42     0138  F7 C5 0007                     test    bp, 7           ;loop until it's a multiple of 8
     43     013C  75 F5                          jne     tab
     44
     45     013E  8B C7                          mov     ax, di          ;if remainder(di/5) = 4 then CR LF
     46     0140  D4 05                          aam     5               ;ah:= al/5; al:= remainder
     47     0142  3C 04                          cmp     al, 4
     48     0144  75 0A                          jne     next
     49     0146  B0 0D                           mov    al, 0Dh         ;CR
     50     0148  CD 29                           int    29h
     51     014A  33 ED                           xor    bp, bp          ;reset column position
     52     014C  B0 0A                           mov    al, 0Ah         ;LF
     53     014E  CD 29                           int    29h
     54     0150  47                     next:   inc     di              ;next N
     55     0151  83 FF 32                       cmp     di, 50          ;loop until done
     56     0154  7C B2                          jl      sum
     57     0156  C3                             ret
     58
     59                                          end     start
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 

XPL0

int  N, S;
[S:= 0;
for N:= 0 to 49 do
    [S:= S + N*N*N;
    IntOut(0, S);
    ChOut(0, 9\tab\);
    if rem(N/5) = 4 then CrLf(0);
    ];
]
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 

Zig

pub fn main() !void {
    const stdout = @import("std").io.getStdOut().writer();
    
    var sum: u32 = 0;
    var i: u32 = 0;

    while (i < 50) : (i += 1) {
        sum += i * i * i;
        try stdout.print("{d:8}", .{sum});
        if (i % 5 == 4) try stdout.print("\n", .{});
    }
}
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