Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

.Re: Rosetta code binomial coefficient

64 views
Skip to first unread message

Robert L.

unread,
Jul 18, 2017, 9:56:08 PM7/18/17
to
Krishna wrote:

> The Forth entry on Rosetta Code for
> computing the binomial coefficient is
> beautiful. Compare its compactness of
> implementation with those in other
> languages. This is a very nice code example
> in Forth. Although it meets the requirements
> of the entry, single integers for 32 bit
> word size quickly lead to failure, e.g. "60
> 30 choose". The implementation is better
> suited for 64-bit word size.
>
> : choose ( n k -- nCk ) 1 swap 0 ?do over i - i 1+ */ loop nip ;
>
> Good job, whoever submitted this to Rosetta Code!

Not compact enough.

choose=->n,r{r<1?1:(n-r+1)*choose[n,r-1]/r}

choose[8,3]
==>56
choose[22,9]
==>497420
choose[99,50]
==>50445672272782096667406248628
choose[200,100]
==>90548514656103281165404177077484163874504589675413336841320
choose[220,120]
==>36582387035876932314460963486550871907252887002282595295412093820

--
I wrote until my fingers were blue, over and over: "Political
Correctness is a religion." I proved it and showed you why it is so
important that Political Correctness is a religion.
--- Bob Whitaker

crc

unread,
Jul 19, 2017, 4:04:35 PM7/19/17
to
On Tuesday, July 18, 2017 at 9:56:08 PM UTC-4, Robert L. wrote:
> Krishna wrote:
>
> > The Forth entry on Rosetta Code for
> > computing the binomial coefficient is
> > beautiful. Compare its compactness of
> > implementation with those in other
> > languages. This is a very nice code example
> > in Forth. Although it meets the requirements
> > of the entry, single integers for 32 bit
> > word size quickly lead to failure, e.g. "60
> > 30 choose". The implementation is better
> > suited for 64-bit word size.
> >
> > : choose ( n k -- nCk ) 1 swap 0 ?do over i - i 1+ */ loop nip ;
> >
> > Good job, whoever submitted this to Rosetta Code!
>
> Not compact enough.
>
> choose=->n,r{r<1?1:(n-r+1)*choose[n,r-1]/r}

Six characters (removing the stack comment) makes it too long? (I note here that you also left out all whitespace to make yours shorter.)

Anyway, here's one in my Forth, using `!` as factorial, in 8 characters less than yours.

~~~
:choose dup-pair - [ ! ] tri@ * / ;
~~~

Rudy Velthuis

unread,
Jul 19, 2017, 5:59:37 PM7/19/17
to
Robert L. wrote:

> Krishna wrote:
>
> > The Forth entry on Rosetta Code for
> > computing the binomial coefficient is
> > beautiful. Compare its compactness of
> > implementation with those in other
> > languages. This is a very nice code example
> > in Forth. Although it meets the requirements
> > of the entry, single integers for 32 bit
> > word size quickly lead to failure, e.g. "60
> > 30 choose". The implementation is better
> > suited for 64-bit word size.
> >
> > : choose ( n k -- nCk ) 1 swap 0 ?do over i - i 1+ */ loop nip ;
> >
> > Good job, whoever submitted this to Rosetta Code!
>
> Not compact enough.
>
> choose=->n,r{r<1?1:(n-r+1)*choose[n,r-1]/r}295412093820

Rosetta code is not about compactness. I prefer readability, even if
that is slightly less compact.

--
Rudy Velthuis http://www.rvelthuis.de

"Blessed is the man, who having nothing to say, abstains from
giving wordy evidence of the fact."
-- George Eliot (1819-1880)

The Beez

unread,
Jul 24, 2017, 5:55:11 AM7/24/17
to
On Wednesday, July 19, 2017 at 3:56:08 AM UTC+2, Robert L. wrote:
> Krishna wrote:
>
> > The Forth entry on Rosetta Code for
> > computing the binomial coefficient is
> > beautiful. Compare its compactness of
> > implementation with those in other
> > languages. This is a very nice code example
> > in Forth. Although it meets the requirements
> > of the entry, single integers for 32 bit
> > word size quickly lead to failure, e.g. "60
> > 30 choose". The implementation is better
> > suited for 64-bit word size.
For those who wonder, it is written by Ian Osborne - according to the Rosetta Code Changelogs.

Hans Bezemer
0 new messages