Sum of first n cubes: Difference between revisions

Content added Content deleted
m (syntax highlighting fixup automation)
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}}===
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="tinybasic">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 541:
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 564:
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 583:


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


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


{{out}}
{{out}}
Line 609: Line 609:
=={{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 626:


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <array>
<syntaxhighlight lang="cpp">#include <array>
#include <cstdio>
#include <cstdio>
#include <numeric>
#include <numeric>
Line 653: Line 653:
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 664:


=={{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 674:
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 688:


=={{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 717:
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 731: Line 731:


=={{header|Comal}}==
=={{header|Comal}}==
<lang comal>0010 ZONE 8
<syntaxhighlight lang="comal">0010 ZONE 8
0020 sum:=0
0020 sum:=0
0030 FOR cube:=0 TO 49 DO
0030 FOR cube:=0 TO 49 DO
Line 738: Line 738:
0060 IF cube MOD 5=4 THEN PRINT
0060 IF cube MOD 5=4 THEN PRINT
0070 ENDFOR cube
0070 ENDFOR cube
0080 END</lang>
0080 END</syntaxhighlight>
{{out}}
{{out}}
<pre>0 1 9 36 100
<pre>0 1 9 36 100
Line 752: Line 752:


=={{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 769: Line 769:
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 778: Line 778:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec cube(ulong n) ulong:
<syntaxhighlight lang="draco">proc nonrec cube(ulong n) ulong:
n * n * n
n * n * n
corp
corp
Line 791: Line 791:
if n % 5 = 4 then writeln() fi
if n % 5 = 4 then writeln() fi
od
od
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 812: Line 812:


{{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 825: Line 825:
)
)
)
)
)</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 918: Line 918:


=={{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 929: Line 929:
=={{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 946: Line 946:


{{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 971: Line 971:


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


{{out}}
{{out}}
Line 990: Line 990:
{{trans|Wren}}
{{trans|Wren}}
{{libheader|Go-rcu}}
{{libheader|Go-rcu}}
<lang go>package main
<syntaxhighlight lang="go">package main


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


{{out}}
{{out}}
Line 1,021: Line 1,021:


=={{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,062: Line 1,062:
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,072: Line 1,072:


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,097: Line 1,097:
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,111: Line 1,111:


=={{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,124: Line 1,124:
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:<lang J> 0 0 1r4 1r2 1r4&p. 49
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</lang>
1500625</syntaxhighlight>


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


Line 1,300: Line 1,300:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre> 0 1 9 36 100
<pre> 0 1 9 36 100
Line 1,316: Line 1,316:
{{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,346: Line 1,346:


=={{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,358: Line 1,358:
</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|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,371: Line 1,371:
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,425: Line 1,425:


=={{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,445: Line 1,445:


=={{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}}==
=={{header|Pascal}}==
<lang pascal>program sumOfFirstNCubes(output);
<syntaxhighlight lang="pascal">program sumOfFirstNCubes(output);
const
const
N = 49;
N = 49;
Line 1,463: Line 1,463:
writeLn(sum)
writeLn(sum)
end
end
end.</lang>
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,472: Line 1,472:


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,489: Line 1,489:
=={{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,505: Line 1,505:


=={{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,514: Line 1,514:
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,568: Line 1,568:


=={{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,581: Line 1,581:
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,597: Line 1,597:


=={{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,605: Line 1,605:
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,620: Line 1,620:
=={{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,650: Line 1,650:
END;
END;


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


if __name__ == '__main__': main()
if __name__ == '__main__': main()
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,702: Line 1,702:
===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,772: Line 1,772:
# 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,783: Line 1,783:
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,841: Line 1,841:
# 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,856: Line 1,856:
=={{header|Quackery}}==
=={{header|Quackery}}==


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


{{out}}
{{out}}
Line 1,876: Line 1,876:
=={{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,884: Line 1,884:


=={{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,897: Line 1,897:


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


sum: 0
sum: 0
Line 1,904: Line 1,904:
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,915: Line 1,915:


=={{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,940: Line 1,940:
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,958: Line 1,958:


=={{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,978: Line 1,978:
see "Found " + row + " cubes" + nl
see "Found " + row + " cubes" + nl
see "done..." + nl
see "done..." + nl
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,998: Line 1,998:


=={{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 2,013: Line 2,013:
}
}
});
});
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,030: Line 2,030:


=={{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 2,042: Line 2,042:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,058: Line 2,058:


=={{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,074: Line 2,074:


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


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


{{out}}
{{out}}
Line 2,096: Line 2,096:


=={{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,154: Line 2,154:
57 0156 C3 ret
57 0156 C3 ret
58
58
59 end start</lang>
59 end start</syntaxhighlight>


{{out}}
{{out}}
Line 2,171: Line 2,171:


=={{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,179: Line 2,179:
if rem(N/5) = 4 then CrLf(0);
if rem(N/5) = 4 then CrLf(0);
];
];
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 2,196: Line 2,196:


=={{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,207: Line 2,207:
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