in reply to Re^2: 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
  • Comment on Re^3: How do I write a program that calculates the sum of all numbers to a number, n?
  • Download Code

Replies are listed 'Best First'.
Re^4: How do I write a program that calculates the sum of all numbers to a number, n?
by BrowserUk (Patriarch) on Feb 24, 2015 at 21:57 UTC
    Though I’m not positive the answer shouldn’t have been 91 instead

    ^$n means 0 .. $n - 1; so try:

    say [+] ^$n+1;

    Not sure about precedence, so it might need brackets. Or maybe:

    say [+] ^++$n;

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". I'm with torvalds on this
    In the absence of evidence, opinion is indistinguishable from prejudice. Agile (and TDD) debunked

      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
        say [+] ^$n,$n

        (You do not want to change $n, so ++ is out of the question)

        Also note that currently $n cannot be a native int when using ++, though that will be fixed.

        $ perl6 -e'my Int $n = 10; say ++$n;' 11 $ perl6 -e'my int $n = 10; say ++$n;' Cannot assign to an immutable value in sub prefix:<++> at src/gen/m-CORE.setting:5565 in block <unit> at -e:1 $ perl6 -e'my int $n = 10; say [+] ^$n' 45 $ perl6 -e'my int $n = 10; say [+] ^$n,$n' 55

        Enjoy, Have FUN! H.Merijn
Re^4: How do I write a program that calculates the sum of all numbers to a number, n?
by Tux (Canon) on Feb 25, 2015 at 07:18 UTC

    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.


    Enjoy, Have FUN! H.Merijn