Sum of first n cubes: Difference between revisions
m (→{{header|REXX}}: added a REXX stub.) |
m (→{{header|REXX}}: added the computer programming language REXX.) |
||
Line 2: | Line 2: | ||
;Task: |
;Task: |
||
Find and show sum of first n cubes, where '''n < 51''' |
Find and show sum of first '''n''' cubes, where '''n < 51''' |
||
<br><br> |
<br><br> |
||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
<lang rexx>/*REXX program finds and displays a number of sums of the first N cubes. */ |
|||
<lang rexx></lang> |
|||
parse arg lo hi cols . /*obtain optional argument from the CL.*/ |
|||
if hi=='' | hi=="," then hi= 50 /*Not specified? Then use the default.*/ |
|||
<ore> |
|||
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> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
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 |
|||
───────┴─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── |
|||
</pre> |
</pre> |
||
Revision as of 07:49, 19 May 2021
- Task
Find and show sum of first n cubes, where n < 51
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:
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 = 50
for n = 1 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: 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 1625625 Found 50 cubes done...