On 07/10/2018 03:42 AM, Brian Duggan wrote:
On Tuesday, July 10, ToddAndMargo wrote:
$ echo "5" | p6 'my $N=slurp(); say $N*($N+1)/2;'
15
$ echo "6" | p6 'my $N=slurp(); say $N*($N+1)/2;'
21
$ echo "100" | p6 'my $N=slurp(); say $N*($N+1)/2;'
5050
Another cool thing is that this formula is used
in Perl 6 under the hood to calculate the sum of
the integers in a range instantly:
~ $ time perl6 -e 'say [+] 1..100'
5050
real 0m0.213s
user 0m0.250s
sys 0m0.032s
~ $ time perl6 -e 'say [+] 1..10000000000000000000'
50000000000000000005000000000000000000
real 0m0.198s
user 0m0.236s
sys 0m0.034s
Fascinating!
If I ever get bored and try to figure out
by hand how to do an inverse cosine, some
come smack me.