Sujet : Re: exercise in double number arithmetic
De : nobody (at) *nospam* nowhere.invalid (Marc Olschok)
Groupes : comp.lang.forthDate : 14. Jul 2024, 22:43:07
Autres entêtes
Message-ID : <v71gpb$jsug$1@solani.org>
References : 1
User-Agent : tin/2.4.4-20191224 ("Millburn") (Linux/2.6.33.4 (i686))
On Sat, 06 Jul 2024 22:20:45 Krishna Myneni wrote:
I've been working on extending the kForth-64 User's Manual and, in
particular, illustrating double length arithmetic, which, being one of
the strengths of Forth, often does not get enough exposure. Here's an
exercise which you can do using only standard Forth words.
How many different ways can you choose 42 distinct objects, 21 at a
time? This is "n choose k" or the binomial coefficent.
Yes, M*/ comes in handy for C(n,0) = 1 , C(n+1,k+1) = C(n,k)*n/k
42 21 binom d.
gives 538257874440
where
: binom ( n1 n2 -- nd ) \ n k --> C(n,k)
dup 0=
IF 2drop 1 s>d
ELSE 2dup 1- swap 1- swap binom 2swap m*/
THEN ;
Gforth SEE nicely replaced the original 'recurse' with 'binom' for
better readability.
-- M.O.