Sum of first n cubes: Difference between revisions

Added Easylang
(Add MAD)
(Added Easylang)
 
(58 intermediate revisions by 22 users not shown)
Line 4:
Find and show sum of first &nbsp; '''n''' &nbsp; cubes, &nbsp; where '''n < 50''' (ie show 50 entries for n=0..49)
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">V s = 0
L(n) 50
s += n * n * n
print(String(s).rjust(7), end' I (n + 1) % 10 == 0 {"\n"} E ‘ ’)</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|Action!}}==
{{libheader|Action! Tool Kit}}
<syntaxhighlight lang="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</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Sum_of_first_n_cubes.png Screenshot from Atari 8-bit computer]
<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|Ada}}==
<syntaxhighlight lang="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;</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|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.
<syntaxhighlight lang ="algol68">BEGIN # show the sums of the first n cubes where 0 <= n < 50 #
FOR i FROM 0 TO 49 DO
INT max number = 49;
FORINT sum = ( i * ( i FROM+ 01 TO) max) numberOVER DO2;
print( ( INTwhole( sum = ( i * (sum, i-8 + 1) ) ) OVER 2;
IF i MOD 10 = 9 THEN print( ( whole( sum * sum, -8newline ) ) );FI
OD</syntaxhighlight>
IF i MOD 10 = 9 THEN print( ( newline ) ) FI
OD
END</lang>
{{out}}
<pre>
Line 25 ⟶ 105:
 
=={{header|ALGOL W}}==
<langsyntaxhighlight lang="algolw">begin % show the sums of the cubes of n for 0 <= n < 50 %
integer cubeSum;
cubeSum := 0;
Line 33 ⟶ 113:
if n rem 10 = 9 then write()
end for_n
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 44 ⟶ 124:
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">10 5⍴+\0,(⍳49)*3</langsyntaxhighlight>
{{out}}
<pre> 0 1 9 36 100
Line 56 ⟶ 136:
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625</pre>
=={{header|AppleScript}}==
<syntaxhighlight lang="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</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|Arturo}}==
 
<langsyntaxhighlight lang="rebol">sumCubes: 0
loop split.every: 10 map 0..49 => [sumCubes: <= sumCubes + & ^ 3] 'a ->
print map a => [pad to :string & 7]</langsyntaxhighlight>
 
{{out}}
Line 69 ⟶ 374:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>
 
=={{header|AutoHotkey}}==
<syntaxhighlight lang="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</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|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f SUM_OF_FIRST_N_CUBES.AWK
BEGIN {
Line 83 ⟶ 403:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 98 ⟶ 418:
==={{header|BASIC256}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="basic256">
<lang BASIC256>
fila = 0
lenCubos = 49
Line 118 ⟶ 438:
print chr(13) + "Encontrados " & fila & " cubos."
end
</syntaxhighlight>
</lang>
 
==={{header|FreeBASIC}}===
<langsyntaxhighlight lang="freebasic">
Dim As Integer fila = 0, lenCubos = 49, sumCubos
 
Line 140 ⟶ 460:
Print !"\nEncontrados " & fila & " cubos."
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 159 ⟶ 479:
</pre>
 
==={{header|QBASICGW-BASIC}}===
<syntaxhighlight lang="gwbasic">10 FOR N=0 TO 49
20 C#=C#+N^3
30 PRINT C#;
40 NEXT N</syntaxhighlight>
{{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
164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 5490
81 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 138297
6 1500625</pre>
 
==={{header|QB64}}===
<syntaxhighlight lang="qbasic">For n%% = 0 To 49
c& = c& + n%% ^ 3
Print Using " ####### "; c&;
If n%% Mod 5 = 4 Then Print
Next n%%</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|QBasic}}===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="qbasic">DEFLNG A-Z
DEFLNG A-Z
 
fila = 0
Line 181 ⟶ 531:
 
PRINT CHR$(13) + "Encontrados"; fila; "cubos."
END</syntaxhighlight>
END
 
</lang>
==={{header|Tiny BASIC}}===
{{works with|TinyBasic}}
Limited to 0 through 19 because integers only go up to 32767.
<syntaxhighlight lang="basic">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</syntaxhighlight>
 
==={{header|Yabasic}}===
{{trans|FreeBASIC}}
<syntaxhighlight lang="yabasic">
<lang Yabasic>
fila = 0
lenCubos = 49
Line 205 ⟶ 565:
print "\nEncontrados ", fila, " cubos.\n"
end
</syntaxhighlight>
</lang>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">∘‿5⥊+`(↕50)⋆3</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|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
int main() {
Line 216 ⟶ 592:
}
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 234 ⟶ 610:
=={{header|C#|CSharp}}==
No multiplication or exponentiation, just addition.
<langsyntaxhighlight lang="csharp">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":" "); } }</langsyntaxhighlight>
{{out}}
<pre>0 1 9 36 100
Line 251 ⟶ 627:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <array>
#include <cstdio>
#include <numeric>
Line 278 ⟶ 654:
std::transform_inclusive_scan(a.begin(), a.end(), a.begin(), std::plus{}, cube);
PrintContainer(a);
}</langsyntaxhighlight>
{{out}}
<pre>
Line 287 ⟶ 663:
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
</pre>
 
=={{header|CLU}}==
<syntaxhighlight lang="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</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|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. SUM-OF-CUBES.
 
Line 318 ⟶ 718:
IF OUT-PTR IS EQUAL TO 41,
DISPLAY OUT-LINE,
MOVE 1 TO OUT-PTR.</langsyntaxhighlight>
{{out}}
<pre> 0 1 9 36 100
Line 330 ⟶ 730:
672400 741321 815409 894916 980100
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}}==
<syntaxhighlight lang="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;</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|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}}==
Line 339 ⟶ 864:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">SUMNCUBES
=LAMBDA(n,
BINCOEFF(1 + n)(2) ^ 2
Line 352 ⟶ 877:
)
)
)</langsyntaxhighlight>
 
The single formula in cell B2 below defines a dynamic array which populates the whole B2:K6 grid:
Line 445 ⟶ 970:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// 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 ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 456 ⟶ 981:
=={{header|Factor}}==
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: grouping math math.functions prettyprint sequences ;
 
50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.</langsyntaxhighlight>
{{out}}
<pre>
Line 473 ⟶ 998:
 
{{works with|Factor|0.99 2021-02-05}}
<langsyntaxhighlight lang="factor">USING: grouping kernel math prettyprint sequences ;
 
: triangular ( n -- m ) dup 1 + * 2/ ;
 
50 <iota> [ triangular sq ] map 10 group simple-table.</langsyntaxhighlight>
{{out}}
As above.
 
=={{header|Fermat}}==
<syntaxhighlight lang="fermat">c:=0
for n = 0 to 49 do
c:=c+n^3;
!(c,' ');
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>
 
=={{header|Forth}}==
{{works with|Gforth}}
<langsyntaxhighlight lang="forth">: sum-cubes ( n -- )
0 swap 0 do
i i i * * + dup 7 .r
Line 490 ⟶ 1,023:
 
50 sum-cubes
bye</langsyntaxhighlight>
 
{{out}}
Line 504 ⟶ 1,037:
672400 741321 815409 894916 980100
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>
 
Line 509 ⟶ 1,061:
{{trans|Wren}}
{{libheader|Go-rcu}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 527 ⟶ 1,079:
}
fmt.Println()
</syntaxhighlight>
</lang>
 
{{out}}
Line 540 ⟶ 1,092:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
Line 581 ⟶ 1,133:
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows</langsyntaxhighlight>
{{Out}}
<pre> 0 1 9 36 100 225 441 784 1296 2025
Line 588 ⟶ 1,140:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>
 
 
Or, in terms of scanl:
<syntaxhighlight lang="haskell">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</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|J}}==
<langsyntaxhighlight Jlang="j">10 5$+/\(i.^3:)50x</langsyntaxhighlight>
{{out}}
<pre> 0 1 9 36 100
Line 602 ⟶ 1,194:
672400 741321 815409 894916 980100
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}}==
<syntaxhighlight lang="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();
})();</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|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
<syntaxhighlight lang="jq">
<lang jq>
# For the sake of stream-processing efficiency:
def add(s): reduce s as $x (0; . + $x);
 
def sum_of_cubes: add(range(0;.) | .*.*.);</langsyntaxhighlight>
'''The task'''
<syntaxhighlight lang="jq">
<lang jq>
range(0;50) | sum_of_cubes as $sum
| "\(.) => \($sum)"</langsyntaxhighlight>
{{out}}
<pre>
Line 634 ⟶ 1,415:
1000000 | sum_of_cubes #=> 249999500000250000000000
</pre>
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))
 
cubesumstil()
</langsyntaxhighlight>{{out}}
<pre>
0 1 9 36 100 225 441 784 1296 2025
Line 647 ⟶ 1,429:
</pre>
Alternatively, and using the REPL, note that recent versions of Julia implement the accumulate function:
<langsyntaxhighlight lang="julia">
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]
</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}}==
<langsyntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
SUM = 0
THROUGH LOOP, FOR STEP = 0, 1, STEP.GE.50
Line 660 ⟶ 1,450:
LOOP PRINT FORMAT FMT, SUM
VECTOR VALUES FMT = $I9*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'> 0
Line 712 ⟶ 1,502:
1382976
1500625</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">Accumulate[Range[0, 49]^3]</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|Nim}}==
<langsyntaxhighlight Nimlang="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: ' '</langsyntaxhighlight>
 
{{out}}
Line 727 ⟶ 1,522:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>
 
=={{header|PARI/GP}}==
<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}}==
<langsyntaxhighlight lang="perl">#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Sum_of_first_n_cubes
Line 735 ⟶ 1,551:
 
my $sum = 0;
printf "%10d%s", $sum += $_ ** 3, $_ % 5 == 4 && "\n" for 0 .. 49;</langsyntaxhighlight>
{{out}}
<pre>
Line 752 ⟶ 1,568:
=={{header|Phix}}==
The commented out line is based on the Factor entry, and is clearly a much faster way to get the same results.
<!--<langsyntaxhighlight Phixlang="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: #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: #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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 766 ⟶ 1,582:
672,400 741,321 815,409 894,916 980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625
</pre>
 
=={{header|PILOT}}==
<syntaxhighlight lang="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 :</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>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</pre>
 
=={{header|Plain English}}==
<langsyntaxhighlight lang="plainenglish">To run:
Start up.
Show the sums of cubes given 49.
Line 781 ⟶ 1,660:
Write the result then " " on the console without advancing.
If the counter is evenly divisible by 5, write "" on the console.
Repeat.</langsyntaxhighlight>
{{out}}
<pre>
Line 794 ⟶ 1,673:
672400 741321 815409 894916 980100
1071225 1168561 1272384 1382976 1500625
</pre>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">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;</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|PL/M}}==
The original 8080 PL/M compiler only supported 8 and 16 bit unsigned arithmetic, so this sample only shows the sums < 65536.
<syntaxhighlight lang="pli">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</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>
 
Line 799 ⟶ 1,740:
===Python :: Procedural===
{{trans|FreeBASIC}}
<langsyntaxhighlight lang="python">
def main():
fila = 0
Line 819 ⟶ 1,760:
 
if __name__ == '__main__': main()
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 840 ⟶ 1,781:
===Python :: Functional===
 
<langsyntaxhighlight lang="python">'''Sum of first N cubes'''
 
from math import factorial
Line 910 ⟶ 1,851:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre> 0 1 9 36 100 225 441 784 1296 2025
Line 917 ⟶ 1,858:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625</pre>
 
 
or, as a scanning accumulation:
 
<syntaxhighlight lang="python">'''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()</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|Quackery}}==
 
<langsyntaxhighlight Quackerylang="quackery"> $ "" 0
50 times
[ i^ 3 ** +
Line 927 ⟶ 1,942:
space join ] ]
drop
nest$ 65 wrap$</langsyntaxhighlight>
 
{{out}}
Line 937 ⟶ 1,952:
894916 980100 1071225 1168561 1272384 1382976 1500625
</pre>
 
=={{header|R}}==
This only takes one line.
<syntaxhighlight lang="rsplus">cumsum((0:49)^3)</syntaxhighlight>
{{out}}
<pre> [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</pre>
 
=={{header|Raku}}==
<syntaxhighlight lang="raku" perl6line>my @sums_of_all_cubes = [\+] ^Inf X** 3;
 
say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);</langsyntaxhighlight>
{{out}}
<pre>
Line 949 ⟶ 1,973:
216225 246016 278784 314721 354025 396900 443556 494209 549081 608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
</pre>
 
=={{header|Red}}==
<syntaxhighlight lang="rebol">Red ["Sum of cubes"]
 
sum: 0
repeat i 50 [
sum: i - 1 ** 3 + sum
prin pad sum 8
if i % 10 = 0 [prin newline]
]</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|REXX}}==
<langsyntaxhighlight 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.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 977 ⟶ 2,019:
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 ?</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
(Shown at five-sixth size.)
Line 995 ⟶ 2,037:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
see "working..." + nl
see "Sum of first n cubes:" + nl
Line 1,015 ⟶ 2,057:
see "Found " + row + " cubes" + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,033 ⟶ 2,075:
done...
</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}}==
<langsyntaxhighlight lang="rust">fn main() {
(0..50)
.map(|x| x * x * x)
.scan(0, |sum, x| {
*sum += x * x * x;
Some(*sum)
})
Line 1,051 ⟶ 2,120:
}
});
}</langsyntaxhighlight>
 
{{out}}
Line 1,068 ⟶ 2,137:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const proc: main is func
Line 1,080 ⟶ 2,149:
end if;
end for;
end func;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,095 ⟶ 2,164:
</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}}==
<langsyntaxhighlight lang="ruby">0..49 -> map { .faulhaber_sum(3) }.slices(5).each { .join(' ').say }</langsyntaxhighlight>
{{out}}
<pre>
Line 1,109 ⟶ 2,206:
672400 741321 815409 894916 980100
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>
 
=={{header|Wren}}==
<langsyntaxhighlight ecmascriptlang="wren">import "./fmt" for Fmt
 
System.print("Cumulative sums of the first 50 cubes:")
Line 1,121 ⟶ 2,239:
if ((n % 10) == 9) System.print()
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 1,134 ⟶ 2,252:
 
=={{header|X86 Assembly}}==
<langsyntaxhighlight lang="asm"> 1 ;Assemble with: tasm, tlink /t
2 0000 .model tiny
3 0000 .code
Line 1,192 ⟶ 2,310:
57 0156 C3 ret
58
59 end start</langsyntaxhighlight>
 
{{out}}
Line 1,209 ⟶ 2,327:
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">int N, S;
[S:= 0;
for N:= 0 to 49 do
Line 1,217 ⟶ 2,335:
if rem(N/5) = 4 then CrLf(0);
];
]</langsyntaxhighlight>
 
{{out}}
Line 1,232 ⟶ 2,350:
1071225 1168561 1272384 1382976 1500625
</pre>
 
=={{header|Zig}}==
<syntaxhighlight lang="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", .{});
}
}</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>
2,060

edits