in reply to Re: How do I write a program that calculates the sum of all numbers to a number, n?in thread How do I write a program that calculates the sum of all numbers to a number, n?
We told you guys; it’s fun! Though I’m not positive the answer shouldn’t have been 91 instead… to v through.
~>perl6 > $n = 13 13 > say [+] ^$n 78 [download]
Though I’m not positive the answer shouldn’t have been 91 instead
^$n means 0 .. $n - 1; so try:
say [+] ^$n+1; [download]
Not sure about precedence, so it might need brackets. Or maybe:
say [+] ^++$n; [download]
More fun! Thanks. Parens work. Probably another way that I don’t know too.
> say [+] ^1+$n 14 > say [+] ^++$n+1 15 > say [+] ^($n+1) 91 > say [+] ^++$n 105 > say [+] ^$n++ 105 [download]
I read "to" in the OP as TO, not UPTO AND INCLUDING.
The OP also did not specify what language to give the solution in, hence the short perl6.
"...mojo?"
Or Voodoo? Perhaps a servant of the spirits?
Best regards, Karl
«The Crux of the Biscuit is the Apostrophe»