Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #53258

Re: A function to return the sum of the numbers less than n

From anton@mips.complang.tuwien.ac.at (Anton Ertl)
Newsgroups comp.lang.forth
Subject Re: A function to return the sum of the numbers less than n
Date 2016-12-10 12:48 +0000
Organization Institut fuer Computersprachen, Technische Universitaet Wien
Message-ID <2016Dec10.134851@mips.complang.tuwien.ac.at> (permalink)
References <o2gmg2$1gk5$1@gioia.aioe.org> <d2bc014f-916d-4f94-9459-0718e388da92@googlegroups.com>

Show all headers | View raw


Julian Fondren <julian.fondren@gmail.com> writes:
>On Saturday, December 10, 2016 at 4:47:33 AM UTC-6, Robert L. wrote:
>> Ruby:
>> 
>> def sum n
>>   (0...n).reduce(0, :+)
>> end
>> 
>> sum 0
>>     ==>0
>> sum 1
>>     ==>0
>> sum 22_000
>>     ==>241989000
>> 
>
># /usr/bin/time ruby -e 'def sum n; (0...n).reduce(0, :+) end; puts sum 1_000_000_000'
>499999999500000000
>45.99user 0.01system 0:46.38elapsed 99%CPU (0avgtext+0avgdata 8692maxresident)k
>2680inputs+0outputs (1major+1171minor)pagefaults 0swaps
>
># /usr/bin/time sf ': sum ( n -- d ) 0. rot 0 do i m+ loop ; 1,000,000,000 drop  sum d. bye'
>499999999500000000 
>2.56user 0.00system 0:02.59elapsed 98%CPU (0avgtext+0avgdata 2020maxresident)k
>0inputs+16outputs (0major+162minor)pagefaults 0swaps

# /usr/bin/time gforth -e ": sum ( u1 -- u2 ) dup 1- * 2/ ; 1000000000 sum . bye"
499999999500000000 0.00user 0.00system 0:00.00elapsed 66%CPU (0avgtext+0avgdata 2780maxresident)k
0inputs+0outputs (0major+334minor)pagefaults 0swaps

(supposedly discovered by Gauss when he was 9 years old).

-- 
M. Anton Ertl  http://www.complang.tuwien.ac.at/anton/home.html
comp.lang.forth FAQs: http://www.complang.tuwien.ac.at/forth/faq/toc.html
     New standard: http://www.forth200x.org/forth200x.html
   EuroForth 2016: http://www.euroforth.org/ef16/

Back to comp.lang.forth | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

A function to return the sum of the numbers less than n "Robert L." <No_spamming@noWhere_7073.org> - 2016-12-10 10:47 +0000
  Re: A function to return the sum of the numbers less than n Julian Fondren <julian.fondren@gmail.com> - 2016-12-10 03:19 -0800
    Re: A function to return the sum of the numbers less than n foxaudioresearch@gmail.com - 2016-12-10 03:58 -0800
    Re: A function to return the sum of the numbers less than n anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-10 12:48 +0000
      Re: A function to return the sum of the numbers less than n Julian Fondren <julian.fondren@gmail.com> - 2016-12-10 05:29 -0800
        Re: A function to return the sum of the numbers less than n pahihu <pahihu@gmail.com> - 2016-12-10 06:37 -0800
        Re: A function to return the sum of the numbers less than n anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2016-12-10 17:08 +0000
  Re: A function to return the sum of the numbers less than n jean-pierre@buechi-schmitt.ch - 2016-12-10 07:04 -0800
    Re: A function to return the sum of the numbers less than n awegel@arcor.de (Alexander Wegel) - 2016-12-10 22:59 +0100

csiph-web