I'm working on modernizing Rosetta Code's infrastructure. Starting with communications. Please accept this time-limited open invite to RC's Slack.. --Michael Mol (talk) 20:59, 30 May 2020 (UTC)

Sum of first n cubes

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

Find and show sum of first   n   cubes,   where n < 50 (ie show 50 entries for n=0..49)

ALGOL 68[edit]

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.

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

begin % show the sums of the cubes of n for 0 <= n < 50 %
integer cubeSum;
cubeSum := 0;
for n := 0 until 49 do begin
cubeSum := cubeSum + ( n * n * n );
writeon( i_w := 8, s_w := 0, cubeSum );
if n rem 10 = 9 then write()
end for_n
end.
Output:
       0       1       9      36     100     225     441     784    1296    2025
    3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
   44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
  216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
  672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

APL[edit]

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

Arturo[edit]

sumCubes: 0
loop split.every: 10 map 0..49 => [sumCubes: <= sumCubes + & ^ 3] 'a ->
print map a => [pad to :string & 7]
Output:
      0       1       9      36     100     225     441     784    1296    2025 
   3025    4356    6084    8281   11025   14400   18496   23409   29241   36100 
  44100   53361   64009   76176   90000  105625  123201  142884  164836  189225 
 216225  246016  278784  314721  354025  396900  443556  494209  549081  608400 
 672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

AWK[edit]

 
# syntax: GAWK -f SUM_OF_FIRST_N_CUBES.AWK
BEGIN {
start = 0
stop = 49
for (i=start; i<=stop; i++) {
sum += i * i * i
printf("%7d%1s",sum,++count%10?"":"\n")
}
printf("\nSum of cubes %d-%d: %d\n",start,stop,count)
exit(0)
}
 
Output:
      0       1       9      36     100     225     441     784    1296    2025
   3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

Sum of cubes 0-49: 50

BASIC[edit]

BASIC256[edit]

Translation of: FreeBASIC
 
fila = 0
lenCubos = 49
 
cls
print "Suma de N cubos para n = [0..49]" + chr(10)
 
for n = 0 to lenCubos
sumCubos = 0
for m = 1 to n
sumCubos += int(m ^ 3)
next m
fila += 1
print "" + sumCubos + " ";
#Print Using " ####### "; sumCubos;
if (fila % 5) = 0 then print
next n
 
print chr(13) + "Encontrados " & fila & " cubos."
end
 

FreeBASIC[edit]

 
Dim As Integer fila = 0, lenCubos = 49, sumCubos
 
CLs
Print !"Suma de N cubos para n = [0..49]\n"
 
For n As Integer = 0 To lenCubos
sumCubos = 0
For m As Integer = 1 To n
sumCubos += (m ^3)
Next m
fila += 1
'Print "" & sumCubos & " ";
Print Using " ####### "; sumCubos;
If fila Mod 5 = 0 Then Print
Next n
 
Print !"\nEncontrados " & fila & " cubos."
Sleep
 
Output:
Suma de N cubos para n = [0..49]

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

Encontrados 50 cubos.

QBASIC[edit]

Translation of: FreeBASIC
 
DEFLNG A-Z
 
fila = 0
lenCubos = 49
 
CLS
PRINT "Suma de N cubos para n = [0..49]" + CHR$(10)
 
FOR n = 0 TO lenCubos
sumCubos = 0
FOR m = 1 TO n
sumCubos = sumCubos + (m ^ 3)
NEXT m
fila = fila + 1
PRINT USING " ####### "; sumCubos;
IF fila MOD 5 = 0 THEN PRINT
NEXT n
 
PRINT CHR$(13) + "Encontrados"; fila; "cubos."
END
 

Yabasic[edit]

Translation of: FreeBASIC
 
fila = 0
lenCubos = 49
 
clear screen
print "Suma de N cubos para n = [0..49]\n"
 
for n = 0 to lenCubos
sumCubos = 0
for m = 1 to n
sumCubos = sumCubos + (m ^3)
next m
fila = fila + 1
print "", sumCubos, " ";
if mod(fila, 5) = 0 then print : fi
next n
 
print "\nEncontrados ", fila, " cubos.\n"
end
 

C[edit]

#include <stdio.h>
 
int main() {
for (int i = 0, sum = 0; i < 50; ++i) {
sum += i * i * i;
printf("%7d%c", sum, (i + 1) % 5 == 0 ? '\n' : ' ');
}
return 0;
}
Output:
      0       1       9      36     100
    225     441     784    1296    2025
   3025    4356    6084    8281   11025
  14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000
 105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025
 396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100
1071225 1168561 1272384 1382976 1500625

C#[edit]

No multiplication or exponentiation, just addition.

using System; using static System.Console;
class Program { static void Main(string[] args) {
for (int i=0,j=-6,k=1,c=0,s=0;s<1600000;s+=c+=k+=j+=6)
Write("{0,-7}{1}",s, (i+=i==3?-4:1)==0?"\n":" "); } }
Output:
0       1       9       36      100    
225     441     784     1296    2025   
3025    4356    6084    8281    11025  
14400   18496   23409   29241   36100  
44100   53361   64009   76176   90000  
105625  123201  142884  164836  189225 
216225  246016  278784  314721  354025 
396900  443556  494209  549081  608400 
672400  741321  815409  894916  980100 
1071225 1168561 1272384 1382976 1500625

Excel[edit]

LAMBDA[edit]

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

(See LAMBDA: The ultimate Excel worksheet function)

SUMNCUBES
=LAMBDA(n,
BINCOEFF(1 + n)(2) ^ 2
)
 
BINCOEFF
=LAMBDA(n,
LAMBDA(k,
IF(n < k,
0,
QUOTIENT(FACT(n), FACT(k) * FACT(n - k))
)
)
)

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

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

F#[edit]

 
// Sum of cubes: Nigel Galloway. May 20th., 2021
let fN g=g*g*g in Seq.initInfinite((+)1>>fN)|>Seq.take 49|>Seq.scan((+))(0)|>Seq.iter(printf "%d "); printfn ""
 
Output:
0 1 9 36 100 225 441 784 1296 2025 3025 4356 6084 8281 11025 14400 18496 23409 29241 36100 44100 53361 64009 76176 90000 105625 123201 142884 164836 189225 216225 246016 278784 314721 354025 396900 443556 494209 549081 608400 672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625

Factor[edit]

Works with: Factor version 0.99 2021-02-05
USING: grouping math math.functions prettyprint sequences ;
 
50 <iota> 0 [ 3 ^ + ] accumulate* 10 group simple-table.
Output:
0      1      9      36     100    225     441     784     1296    2025
3025   4356   6084   8281   11025  14400   18496   23409   29241   36100
44100  53361  64009  76176  90000  105625  123201  142884  164836  189225
216225 246016 278784 314721 354025 396900  443556  494209  549081  608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625

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

Tn = n(n + 1) / 2

Works with: Factor version 0.99 2021-02-05
USING: grouping kernel math prettyprint sequences ;
 
: triangular ( n -- m ) dup 1 + * 2/ ;
 
50 <iota> [ triangular sq ] map 10 group simple-table.
Output:

As above.

Forth[edit]

Works with: Gforth
: sum-cubes ( n -- )
0 swap 0 do
i i i * * + dup 7 .r
i 1+ 5 mod 0= if cr else space then
loop drop ;
 
50 sum-cubes
bye
Output:
      0       1       9      36     100
    225     441     784    1296    2025
   3025    4356    6084    8281   11025
  14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000
 105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025
 396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100
1071225 1168561 1272384 1382976 1500625

Go[edit]

Translation of: Wren
Library: Go-rcu
package main
 
import (
"fmt"
"rcu"
)
 
func main() {
fmt.Println("Cumulative sums of the first 50 cubes:")
sum := 0
for n := 0; n < 50; n++ {
sum += n * n * n
fmt.Printf("%9s ", rcu.Commatize(sum))
if n%10 == 9 {
fmt.Println()
}
}
fmt.Println()
 
Output:
Cumulative sums of the first 50 cubes:
        0         1         9        36       100       225       441       784     1,296     2,025 
    3,025     4,356     6,084     8,281    11,025    14,400    18,496    23,409    29,241    36,100 
   44,100    53,361    64,009    76,176    90,000   105,625   123,201   142,884   164,836   189,225 
  216,225   246,016   278,784   314,721   354,025   396,900   443,556   494,209   549,081   608,400 
  672,400   741,321   815,409   894,916   980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625 

Haskell[edit]

import Data.List (intercalate, transpose)
import Data.List.Split (chunksOf)
import Text.Printf (printf)
 
------------------- SUM OF FIRST N CUBES -----------------
 
sumOfFirstNCubes :: Integer -> Integer
sumOfFirstNCubes =
(^ 2)
. flip binomialCoefficient 2
. succ
 
 
--------------------------- TEST -------------------------
main :: IO ()
main =
putStrLn $
table " " $
chunksOf 10 $
show . sumOfFirstNCubes <$> [0 .. 49]
 
 
------------------------- GENERIC ------------------------
 
binomialCoefficient :: Integer -> Integer -> Integer
binomialCoefficient n k
| n < k = 0
| otherwise =
div
(factorial n)
(factorial k * factorial (n - k))
 
factorial :: Integer -> Integer
factorial = product . enumFromTo 1
 
------------------------- DISPLAY ------------------------
 
table :: String -> [[String]] -> String
table gap rows =
let ws = maximum . fmap length <$> transpose rows
pw = printf . flip intercalate ["%", "s"] . show
in unlines $ intercalate gap . zipWith pw ws <$> rows
Output:
     0      1      9     36    100     225     441     784    1296    2025
  3025   4356   6084   8281  11025   14400   18496   23409   29241   36100
 44100  53361  64009  76176  90000  105625  123201  142884  164836  189225
216225 246016 278784 314721 354025  396900  443556  494209  549081  608400
672400 741321 815409 894916 980100 1071225 1168561 1272384 1382976 1500625

J[edit]

10 5$+/\(i.^3:)50x
Output:
      0       1       9      36     100
    225     441     784    1296    2025
   3025    4356    6084    8281   11025
  14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000
 105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025
 396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100
1071225 1168561 1272384 1382976 1500625

Julia[edit]

cubesumstil(N = 49, s = 0) = (foreach(n -> print(lpad(s += n^3, 8), n % 10 == 9 ? "\n" : ""), 0:N))
 
cubesumstil()
 
Output:
       0       1       9      36     100     225     441     784    1296    2025
    3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
   44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
  216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
  672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

Alternatively, and using the REPL, note that recent versions of Julia implement the accumulate function:

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

Nim[edit]

import strutils
 
var s = 0
for n in 0..49:
s += n * n * n
stdout.write ($s).align(7), if (n + 1) mod 10 == 0: '\n' else: ' '
Output:
      0       1       9      36     100     225     441     784    1296    2025
   3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

Perl[edit]

#!/usr/bin/perl
 
use strict; # https://rosettacode.org/wiki/Sum_of_first_n_cubes
use warnings;
 
my $sum = 0;
printf "%10d%s", $sum += $_ ** 3, $_ % 5 == 4 && "\n" for 0 .. 49;
Output:
         0         1         9        36       100
       225       441       784      1296      2025
      3025      4356      6084      8281     11025
     14400     18496     23409     29241     36100
     44100     53361     64009     76176     90000
    105625    123201    142884    164836    189225
    216225    246016    278784    314721    354025
    396900    443556    494209    549081    608400
    672400    741321    815409    894916    980100
   1071225   1168561   1272384   1382976   1500625

Phix[edit]

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

Plain English[edit]

To run:
Start up.
Show the sums of cubes given 49.
Wait for the escape key.
Shut down.
 
To show the sums of cubes given a number:
If a counter is past the number, exit.
Put the counter plus 1 times the counter into a result number.
Cut the result in half.
Raise the result to 2.
Write the result then " " on the console without advancing.
If the counter is evenly divisible by 5, write "" on the console.
Repeat.
Output:
0 1 9 36 100 
225 441 784 1296 2025 
3025 4356 6084 8281 11025 
14400 18496 23409 29241 36100 
44100 53361 64009 76176 90000 
105625 123201 142884 164836 189225 
216225 246016 278784 314721 354025 
396900 443556 494209 549081 608400 
672400 741321 815409 894916 980100 
1071225 1168561 1272384 1382976 1500625 

Python[edit]

Python :: Procedural[edit]

Translation of: FreeBASIC
 
def main():
fila = 0
lenCubos = 51
 
print("Suma de N cubos para n = [0..49]\n")
 
for n in range(1, lenCubos):
sumCubos = 0
for m in range(1, n):
sumCubos = sumCubos + (m ** 3)
 
fila += 1
print(f'{sumCubos:7} ', end='')
if fila % 5 == 0:
print(" ")
 
print(f"\nEncontrados {fila} cubos.")
 
if __name__ == '__main__': main()
 
Output:
Suma de N cubos para n = [0..49]

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

Encontrados 50 cubos.

Python :: Functional[edit]

'''Sum of first N cubes'''
 
from math import factorial
 
 
# sumOfFirstNCubes :: Int -> Int
def sumOfFirstNCubes(n):
'''The sum of the first n cubes.'''
return binomialCoefficient(1 + n)(2) ** 2
 
 
# ------------------------- TEST -------------------------
# main :: IO ()
def main():
'''First fifty values (N drawn from [0 .. 49])
'''

print(
table(10)([
str(sumOfFirstNCubes(n)) for n
in range(0, 1 + 49)
])
)
 
 
# ----------------------- GENERIC ------------------------
 
# binomialCoefficient :: Int -> Int -> Int
def binomialCoefficient(n):
'''The coefficient of the term x^k in the polynomial
expansion of the binomial power (1 + x)^n
'''

def go(k):
return 0 if n < k else factorial(n) // (
factorial(k) * factorial(n - k)
)
return go
 
 
# ----------------------- DISPLAY ------------------------
 
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divisible, the final list will be shorter than n.
'''

def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
 
 
# table :: Int -> [String] -> String
def table(n):
'''A list of strings formatted as
right-justified rows of n columns.
'''

def go(xs):
w = len(xs[-1])
return '\n'.join(
' '.join(row) for row in chunksOf(n)([
s.rjust(w, ' ') for s in xs
])
)
return go
 
 
# MAIN ---
if __name__ == '__main__':
main()
Output:
      0       1       9      36     100     225     441     784    1296    2025
   3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

Raku[edit]

my @sums_of_all_cubes = [\+] ^Inf X** 3;
 
say .fmt('%7d') for @sums_of_all_cubes.head(50).batch(10);
Output:
     0       1       9      36     100     225     441     784    1296    2025
  3025    4356    6084    8281   11025   14400   18496   23409   29241   36100
 44100   53361   64009   76176   90000  105625  123201  142884  164836  189225
216225  246016  278784  314721  354025  396900  443556  494209  549081  608400
672400  741321  815409  894916  980100 1071225 1168561 1272384 1382976 1500625

REXX[edit]

/*REXX program finds and displays a number of sums of the first  N  cubes, where N < 50 */
parse arg n cols . /*obtain optional argument from the CL.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if cols=='' | cols=="," then cols= 10 /* " " " " " " */
w= 12 /*width of a number in any column. */
title= ' cube sums, where N < ' commas(n)
say ' index │'center(title, 1 + cols*(w+1) )
say '───────┼'center("" , 1 + cols*(w+1), '─')
found= 0; idx= 0 /*initialize the number for the index. */
$=; sum= 0 /*a list of the sum of N cubes. . */
do j=0 for n; sum= sum + j**3 /*compute the sum of this cube + others*/
found= found + 1 /*bump the number of sums shown. */
c= commas(sum) /*maybe add commas to the number. */
$= $ right(c, max(w, length(c) ) ) /*add a sum of N cubes to the $ list.*/
if found//cols\==0 then iterate /*have we populated a line of output? */
say center(idx, 7)'│' substr($, 2); $= /*display what we have so far (cols). */
idx= idx + cols /*bump the index count for the output*/
end /*j*/
 
if $\=='' then say center(idx, 7)"│" substr($, 2) /*possible display residual output.*/
say '───────┴'center("" , 1 + cols*(w+1), '─')
say
say 'Found ' commas(found) title
exit 0 /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg ?; do jc=length(?)-3 to 1 by -3; ?=insert(',', ?, jc); end; return ?
output   when using the default inputs:

(Shown at five-sixth size.)

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

Found  50  cube sums,  where  N  <  50

Ring[edit]

 
see "working..." + nl
see "Sum of first n cubes:" + nl
row = 0
lenCubes = 49
 
for n = 0 to lenCubes
sumCubes = 0
for m = 1 to n
sumCubes = sumCubes + pow(m,3)
next
row = row + 1
see "" + sumCubes + " "
if row%5 = 0
see nl
ok
next
 
see "Found " + row + " cubes" + nl
see "done..." + nl
 
Output:
working...
Sum of first n cubes:
0 1 9 36 100 
225 441 784 1296 2025 
3025 4356 6084 8281 11025 
14400 18496 23409 29241 36100 
44100 53361 64009 76176 90000 
105625 123201 142884 164836 189225 
216225 246016 278784 314721 354025 
396900 443556 494209 549081 608400 
672400 741321 815409 894916 980100 
1071225 1168561 1272384 1382976 1500625 
Found 50 cubes
done...

Rust[edit]

fn main() {
(0..50)
.map(|x| x * x * x)
.scan(0, |sum, x| {
*sum += x;
Some(*sum)
})
.enumerate()
.for_each(|(i, n)| {
print!("{:7}", n);
if (i + 1) % 5 == 0 {
println!();
} else {
print!(" ");
}
});
}
Output:
      0       1       9      36     100
    225     441     784    1296    2025
   3025    4356    6084    8281   11025
  14400   18496   23409   29241   36100
  44100   53361   64009   76176   90000
 105625  123201  142884  164836  189225
 216225  246016  278784  314721  354025
 396900  443556  494209  549081  608400
 672400  741321  815409  894916  980100
1071225 1168561 1272384 1382976 1500625

Wren[edit]

import "/fmt" for Fmt
 
System.print("Cumulative sums of the first 50 cubes:")
var sum = 0
for (n in 0..49) {
sum = sum + n * n * n
Fmt.write("$,9d ", sum)
if ((n % 10) == 9) System.print()
}
System.print()
Output:
Cumulative sums of the first 50 cubes:
        0         1         9        36       100       225       441       784     1,296     2,025 
    3,025     4,356     6,084     8,281    11,025    14,400    18,496    23,409    29,241    36,100 
   44,100    53,361    64,009    76,176    90,000   105,625   123,201   142,884   164,836   189,225 
  216,225   246,016   278,784   314,721   354,025   396,900   443,556   494,209   549,081   608,400 
  672,400   741,321   815,409   894,916   980,100 1,071,225 1,168,561 1,272,384 1,382,976 1,500,625 

X86 Assembly[edit]

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

XPL0[edit]

int  N, S;
[S:= 0;
for N:= 0 to 49 do
[S:= S + N*N*N;
IntOut(0, S);
ChOut(0, 9\tab\);
if rem(N/5) = 4 then CrLf(0);
];
]
Output:
0       1       9       36      100     
225     441     784     1296    2025    
3025    4356    6084    8281    11025   
14400   18496   23409   29241   36100   
44100   53361   64009   76176   90000   
105625  123201  142884  164836  189225  
216225  246016  278784  314721  354025  
396900  443556  494209  549081  608400  
672400  741321  815409  894916  980100  
1071225 1168561 1272384 1382976 1500625