Here's a recursive solution:
define method sumSquaresFromOne(n :: <integer>) if (n = 1) 1 else n *n + sumSquaresFromOne(n - 1) end end method;
(Obviously, this could benefit from some verification for n <1.)
To run the method, you must run the command:
format-out("%d", sumSquaresFromOne(5))
The output of which will be "55" (1 + 4 + 9 + 16 + 25).
You can create the main method as follows:
define method main(appname, #rest arguments) format-out("Input an integer number n:") let n = read-line(*standard-input*) format-out("Sum of squares from t to %d is %d\n", n, sumSquaresFromOne(n)) exit(exit-code: 0); end method;
source share