Post Reply 
HP 50g Double factorial
05-01-2019, 06:26 PM
Post: #10
RE: HP 50g Double factorial
(05-01-2019 05:05 PM)Giuseppe Donnini Wrote:  Here's another solution:

Considering that  n!!  is equivalent to  n·(n-2)·(n-4)· ... ·1  for every positive integer  n ≠ 0  (odd or even), all it needs is a simple loop which calculates the next factor and multiplies it with the running product. Such a solution could do entirely without local variables and algebraic objects, requiring only stack manipulations, and very few in number:

1          @ Initialize result.
SWAP 2     @ Loop down from n to 2.
FOR f f *  @ Multiply with current factor.
-2 STEP    @ Decrement factor by 2 ; repeat until factor < 2.

Taking into account the special case where  n = 0 , we finally have:

Code:

@ NAME  : DF (Double factorial)
@ STACK : ( n --> n!! )
\<<
  1                 @ Initialize result.
  IF SWAP DUP 0 ==  @ If n = 0,
  THEN DROP         @   then n!! = 1 (just drop n, 1 already on stack).
  ELSE 2            @ Otherwise: Loop down from n to 2.
  FOR f f *         @   Multiply with current factor.
  -2 STEP           @   Decrement factor by 2 ; repeat until factor < 2.
\>>

Hi Giuseppe,

Thank you, very interesting and nice solution. I tested it (END added). Works fine, but if I use very large numbers, the program runs in an infinite loop. The other programs output the message "Integer too large". But I can easily query this.

Kind regards
Joerg
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
HP 50g Double factorial - joeres - 05-01-2019, 11:19 AM
RE: HP 50g Double factorial - Gilles - 05-01-2019, 01:40 PM
RE: HP 50g Double factorial - joeres - 05-01-2019, 03:57 PM
RE: HP 50g Double factorial - Gilles - 05-01-2019, 04:14 PM
RE: HP 50g Double factorial - joeres - 05-01-2019, 06:01 PM
RE: HP 50g Double factorial - John Keith - 05-01-2019, 07:00 PM
RE: HP 50g Double factorial - joeres - 05-02-2019, 08:33 PM
RE: HP 50g Double factorial - grsbanks - 05-01-2019, 04:59 PM
RE: HP 50g Double factorial - Gilles - 05-01-2019, 07:06 PM
RE: HP 50g Double factorial - Albert Chan - 05-01-2019, 06:17 PM
RE: HP 50g Double factorial - joeres - 05-01-2019 06:26 PM
RE: HP 50g Double factorial - joeres - 05-02-2019, 09:34 PM
RE: HP 50g Double factorial - joeres - 05-01-2019, 06:35 PM
RE: HP 50g Double factorial - FLISZT - 01-27-2024, 05:57 AM
RE: HP 50g Double factorial - DavidM - 01-27-2024, 01:50 PM
RE: HP 50g Double factorial - FLISZT - 01-27-2024, 05:51 PM
RE: HP 50g Double factorial - DavidM - 01-27-2024, 09:33 PM
RE: HP 50g Double factorial - FLISZT - 01-28-2024, 01:47 AM
RE: HP 50g Double factorial - DavidM - 01-28-2024, 02:51 PM
RE: HP 50g Double factorial - FLISZT - 01-28-2024, 07:07 PM
RE: HP 50g Double factorial - John Keith - 01-28-2024, 08:48 PM



User(s) browsing this thread: 1 Guest(s)