Sum of first n cubes
- Task
Find and show sum of first n cubes, where n < 50 (ie show 50 entries for n=0..49)
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. <lang algol68>BEGIN # show the sums of the first n cubes where 0 <= n < 50 #
INT max number = 49; FOR i FROM 0 TO max number DO INT sum = ( i * ( i + 1 ) ) OVER 2; print( ( whole( sum * sum, -8 ) ) ); IF i MOD 10 = 9 THEN print( ( newline ) ) FI OD
END</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
ALGOL W
<lang algolw>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.</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
APL
<lang APL>10 5⍴+\0,(⍳49)*3</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
BASIC
BASIC256
<lang BASIC256> 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 </lang>
FreeBASIC
<lang 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 </lang>
- 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.
QBASIC
<lang qbasic> 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 </lang>
Yabasic
<lang Yabasic> 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 </lang>
C#
No multiplication or exponentiation, just addition. <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":" "); } }</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
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)
<lang lisp>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)) ) )
)</lang>
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#
<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 "" </lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
Factor
<lang factor>USING: grouping math math.functions prettyprint sequences ;
50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
Alternatively, this is the same as the triangular numbers squared, where the triangular numbers are given by
Tn = n(n + 1) / 2
<lang factor>USING: grouping kernel math prettyprint sequences ;
- triangular ( n -- m ) dup 1 + * 2/ ;
50 <iota> [ triangular sq ] map 10 group simple-table.</lang>
- Output:
As above.
J
<lang J>10 5$+/\(i.^3:)50x</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
Julia
<lang julia>cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))
cubesumstil()
</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
Alternatively, and using the REPL, note that recent versions of Julia implement the accumulate function: <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] </lang>
Perl
<lang 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;</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
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
Python
<lang python> 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() </lang>
- 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.
Raku
<lang perl6>my @sums_of_all_cubes = [\+] ^Inf X** 3;
say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625
REXX
<lang rexx>/*REXX program finds and displays a number of sums of the first N cubes. */ parse arg lo hi cols . /*obtain optional argument from the CL.*/ if hi== | hi=="," then hi= 50 /*Not specified? Then use the default.*/ if cols== | cols=="," then cols= 10 /* " " " " " " */ w= 12 /*width of a number in any column. */
@cubeSum= ' cube sums, where N < ' commas(hi)
say ' index │'center(@cubeSum, 1 + cols*(w+1) ) say '───────┼'center("" , 1 + cols*(w+1), '─') found= 0; idx= 1 /*initialize the number for the index. */ $=; sum= 0 /*a list of the sum of N cubes. . */
do j=0 for hi; sum= sum + j**3 /*compute the sum of this cube + others*/ found= found + 1 /*bump the number of sums shown. */ c= commas(sum) /*maybe add commas to the number. */ $= $ right(c, max(w, length(c) ) ) /*add a sum of N cubes to the $ list.*/ if found//cols\==0 then iterate /*have we populated a line of output? */ say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */ idx= idx + cols /*bump the index count for the output*/ end /*j*/
if $\== then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/ say '───────┴'center("" , 1 + cols*(w+1), '─') say say 'Found ' commas(found) @cubeSum exit 0 /*stick a fork in it, we're all done. */ /*──────────────────────────────────────────────────────────────────────────────────────*/ commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?</lang>
- output when using the default inputs:
(Shown at five-sixth size.)
index │ cube sums, where N < 50 ───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 1 │ 0 1 9 36 100 225 441 784 1,296 2,025 11 │ 3,025 4,356 6,084 8,281 11,025 14,400 18,496 23,409 29,241 36,100 21 │ 44,100 53,361 64,009 76,176 90,000 105,625 123,201 142,884 164,836 189,225 31 │ 216,225 246,016 278,784 314,721 354,025 396,900 443,556 494,209 549,081 608,400 41 │ 672,400 741,321 815,409 894,916 980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625 ───────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Ring
<lang ring> see "working..." + nl see "Sum of first n cubes:" + nl row = 0 lenCubes = 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 </lang>
- 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...
Wren
<lang ecmascript>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()</lang>
- Output:
Cumulative sums of the first 50 cubes: 0 1 9 36 100 225 441 784 1,296 2,025 3,025 4,356 6,084 8,281 11,025 14,400 18,496 23,409 29,241 36,100 44,100 53,361 64,009 76,176 90,000 105,625 123,201 142,884 164,836 189,225 216,225 246,016 278,784 314,721 354,025 396,900 443,556 494,209 549,081 608,400 672,400 741,321 815,409 894,916 980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625
XPL0
<lang 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); ];
]</lang>
- Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625