[Purdue-pm] Perl 6

Mark Senn mark at purdue.edu
Tue Jul 10 05:56:48 PDT 2018


Carl Friedrich Gauss added the integers from 1 to 100 ca 1784
using the formula (n/2)(n+1).  See pages 12--13 of
    https://books.google.co.uk/books?id=4mwSrfxBSzkC&printsec=frontcover#v=onepage&q&f=false
for details.


Brian Duggan <bduggan2 at gmail.com> wrote on 2018-07-10 at 06:42 -04:
To: ToddAndMargo <ToddAndMargo at zoho.com>
Cc: perl6-users <perl6-users at perl.org>
Subject: Re: An interesting math formula to share
Message-ID: <20180710104250.GA22895 at localhost>
|  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:


It nice not to worry about integer overflow with Perl 6.
Perl 6 has the formula to add integers from 1 to n built-in
so adding 1 to 10^1000 doesn't take much longer than adding
1 + 2 + 3.

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..3'
6
0.39 wall clock seconds

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..10'
55
0.40 wall clock seconds

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..10**1'
55
0.40 wall clock seconds

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..100'
5050
0.40 wall clock seconds

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..10**2'
5050
0.40 wall clock seconds

$ /usr/bin/time -f '%e wall clock seconds' perl6 -e 'say [+] 1..10**1000'
50000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0.41 wall clock seconds


-mark


More information about the Purdue-pm mailing list