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

Reply via email to