The Museum of HP Calculators

HP Forum Archive 19

[ Return to Index | Top of Index ]

Small Sunday Afternoon Mini-Challenge
Message #1 Posted by Gerson W. Barbosa on 2 May 2010, 4:22 p.m.

Let's define a factorial-like function, and for lack of a better name let's denote it x?:

x? = 1 * 1.5 * 2 * 2.5 * 3 * 3.5 * ... * (n - 0.5) * n

Thus,

  2? = 1 * 1.5 * 2 = 3

3.5? = 1 * 1.5 * 2 * 2.5 * 3 * 3.5 = 78.75

Write a small program to compute this function on your HP calculator of choice. Smallest programs win (least size in bytes or number of steps) for each calculator. RPN programs should not destroy register X. So far I have a 9-step program for the 12C and most scientific RPN calculators (LBL and RTN and final GTO 00 don't count) and a 16.5-byte program for the 32SII, but I hope you can improve this. Searching through the internet for reference is allowed and encouraged.

The function can be generalized for real arguments other than those ending in 0.5. So,

3.333333? = 51.0575911879

(2 * pi)? = 668840.999380 (bold digits not correct)

(the HP-12C program will not handle these).

Gerson.

      
Re: Small Sunday Afternoon Mini-Challenge
Message #2 Posted by Patrice on 2 May 2010, 7:20 p.m.,
in response to message #1 by Gerson W. Barbosa

Hi all,

Here is my quick and dirty answer.

My little analyse gives:

x? = 1 * 1.5 * 2 * 2.5 * 3 * 3.5 * ... * (x - 0.5) * x
transforms to
x? = 1 * 2/2 * 3/2 * 4/2 * 5/2 * 6/2 * 7/2 * ... * (2x - 1)/2 * 2x/2
x? = 2 * 2x! / 2^2x

and the program on my 41C gives:

01 LBL A
02 2
03 *
04 FACT
05 2
06 LASTX
07 Y^X
08 /
09 2
10 *
11 RTN

Without the GAMMA function, generalizing the program is not very easy.

Patrice

            
Re: Small Sunday Afternoon Mini-Challenge
Message #3 Posted by hugh steers on 2 May 2010, 7:35 p.m.,
in response to message #2 by Patrice

Patrice, sorry im not copying you. i got the same answer :-)

            
Re: Small Sunday Afternoon Mini-Challenge
Message #4 Posted by PeterP on 2 May 2010, 7:48 p.m.,
in response to message #2 by Patrice

I think you can actually save 2 steps hence beating Gerson's 9 step program. However I was not sure what he meant by 'the x register should stay undisturbed' (which this version does not do and I was not able to find an elegant way of preserving the stack without burning up 2 or more steps which would then be the same 9 steps or worse that Gerson had already...)

00 LBL A
01  St+ X
02  FACT
03  2
04  LASTX
05  Y^X
06  /
07  St+ X
08 END

                  
Re: Small Sunday Afternoon Mini-Challenge
Message #5 Posted by Gerson W. Barbosa on 2 May 2010, 10:41 p.m.,
in response to message #4 by PeterP

Quote:
However I was not sure what he meant by 'the x register should stay undisturbed'

I just wanted to preserve at least the X register so that chained calculations could be done more easily, for instance, in order to obtain

2.2? * 3.3? = 198.171558303

I do on my 33s

2.2 XEQ Y 3.3 XEQ Y *

I am glad you and others were able to find a solution so soon. I didn't think of using the HP-41. I think an even smaller program is possible on it, but I'll leave that for you.

Interestingly on the 33s my 9-step solution is 69 bytes long while the 51-byte one is 11 steps long, but that has to do with the number of bytes required by numeric constants.

Regards,

Gerson.

Edited to correct a typo

Edited: 2 May 2010, 10:46 p.m.

                  
Re: Small Sunday Afternoon Mini-Challenge
Message #6 Posted by Paul Dale on 2 May 2010, 11:37 p.m.,
in response to message #4 by PeterP

The 20b scintific load out can do this in one step less and one stack level less:

001: LBL B
002:  STO+ X
003:  x!
004:  LASTx
005:  2^x
006:  /
007:  STO+ X
008: RTN

Unfortunately, I don't see how to do any better.

- Pauli

            
Re: Small Sunday Afternoon Mini-Challenge
Message #7 Posted by Gerson W. Barbosa on 2 May 2010, 11:18 p.m.,
in response to message #2 by Patrice

Hello Patrice and all,

Congratulations to all of you. Now let's wait and see if one step or two can be saved (keep the X register).

Quote:

Quote:
Without the GAMMA function, generalizing the program is not very easy.

You're right. The HP-42s has GAMMA and other scientific HP calculators have Gamma(x+1), that is, x! or PI(x), which I prefer. On the 15C just replace FACT with x! and you're done (see Hugh's program). By the way, those who like pi might like the following triple pi expression:-)

18*PI(PI(pi)) = 132850.000038

Regards,

Gerson

                  
Re: Small Sunday Afternoon Mini-Challenge
Message #8 Posted by Angel Martin on 3 May 2010, 12:45 a.m.,
in response to message #7 by Gerson W. Barbosa

There's a Gamma function available on the SandMath-5 Module for the 41 platform.

Cheers, AM.

                        
Re: Small Sunday Afternoon Mini-Challenge
Message #9 Posted by Gerson W. Barbosa on 3 May 2010, 7:14 p.m.,
in response to message #8 by Angel Martin

¡Hola Ángel!

Then this should work like it does on the 42s:

01 LBL "?"
02 STO+ ST X
03 1
04 +
05 GAMMA
06 2
07 STO- ST L
08 LASTX
09 Y^X
10 /
11 RTN

Regards,

Gerson.

Edited to save one step: STO+ ST X instead of 2 * :-)

Edited: 3 May 2010, 7:23 p.m.

                              
Re: Small Sunday Afternoon Mini-Challenge
Message #10 Posted by Ángel Martin on 5 May 2010, 10:59 a.m.,
in response to message #9 by Gerson W. Barbosa

It works like a charm :)

Best, ÁM

      
Re: Small Sunday Afternoon Mini-Challenge
Message #11 Posted by hugh steers on 2 May 2010, 7:32 p.m.,
in response to message #1 by Gerson W. Barbosa

ok. 15C

LBL A
2
*
STO 0
x!
2
RCL 0
1
-
Y^X
/
RTN

that's 10 without the bumpers.

also, using this (might help others)

function moo(n)
   return math.factorial(2*n)/math.pow(2,2*n-1)
end

i get

> =moo(3.333333)
51.05759118800984966953616
> =moo(2*math.pi)
668840.9993288121626620536

      
Re: Small Sunday Afternoon Mini-Challenge
Message #12 Posted by Gerson W. Barbosa on 2 May 2010, 11:48 p.m.,
in response to message #1 by Gerson W. Barbosa

Here are mine:

0001 LBL A
0002 2
0003 *
0004 x!
0005 2
0006 LASTx
0007 y^y
0008 2
0009 /
0010 /
0011 RTN

[pre] 0001 LBL Z 0002 x! 0003 LASTx 0004 0.5 0005 - 0006 LASTx 0007 x! 0008 1/x 0009 x<>y 0010 x! 0011 * 0012 * 0013 RTN

33s length & checksum table:

LN CK

Y 69 9F3C

Z 51 70C0

Gerson.

      
Re: Small Sunday Afternoon Mini-Challenge
Message #13 Posted by Namir on 3 May 2010, 10:33 a.m.,
in response to message #1 by Gerson W. Barbosa

Gerson,

Does the function you are proposing in the challenge have any interesting properties? Does it solve any particular kind of equations?

Maybe we can call it the Shift Factorial function, defined as:

SFact(X, s) = 1 * (1 + s) * (1 + 2d) * (1 + 3d) * ... * (1 + n*s) * X

Where s is the shift factor and x > (1 + n*s) for some integer value of n.

In the case the shift factor s = 0.5, we have the function you defined.

SFact(X,0.5) = 1 * 1.5 * 2 * 2.5 * 3 * 3.5 * ... * X

In the case s = 0.25

SFact(X,0.25) = 1 * 1.25 * 1.50 * 1.75 * 2 * 2.25 * 2.5 * ... * X

The smaller s the larger SFact is for the same value X.

Of course if s > 1, say 2 we have:

SFact(X,2) = 1 * 3 * 5 * 7 * ... * X

And when s = 1, the SFact function is equivalent to the factorial function.

Namir

Edited: 3 May 2010, 11:00 a.m.

            
Re: Small Sunday Afternoon Mini-Challenge
Message #14 Posted by Gerson W. Barbosa on 3 May 2010, 6:39 p.m.,
in response to message #13 by Namir

Quote:
Does the function you are proposing in the challenge have any interesting properties?

I think its properties are similar to those of x!. For instance,

 (1/2)! = 1/2*sqrt(pi)              (1/4)? = sqrt(pi/2)

(3/4)? = 3/4*sqrt(pi/2)

(3/2)! = 3/4*sqrt(pi) (5/4)? = 15/16*sqrt(pi/2)

(7/4)? = 105/64*sqrt(pi/2)

(5/2)! = 15/8*sqrt(pi) (9/4)? = 945/256*sqrt(pi/2)

(11/4)? = 10395/1024*sqrt(pi/2)

(7/2)! = 105/16*sqrt(pi)

and so on ...

1, 3, 15, 105, 945, ... are double factorial numbers (OEIS A001147)

Quote:
Does it solve any particular kind of equations?

None that I am aware of.

Regards,

Gerson.

                  
Re: Small Sunday Afternoon Mini-Challenge
Message #15 Posted by Gerson W. Barbosa on 5 May 2010, 10:42 p.m.,
in response to message #14 by Gerson W. Barbosa

   (5/8)¡ = 5/4*sqrt(pi/4)
   (7/8)¡ = 35/32*sqrt(pi/4) 
   (9/8)¡ = 315/256*sqrt(pi/4)
  (11/8)¡ = 3465/2048*sqrt(pi/4)
  (13/8)¡ = 45045/16384*sqrt(pi/4)

a) (15/8)¡ = ? b) How is ¡ defined?

Anyone?

Edited: 6 May 2010, 11:38 a.m.

                        
Re: Small Sunday Afternoon Mini-Challenge
Message #16 Posted by Paul Dale on 6 May 2010, 9:45 p.m.,
in response to message #15 by Gerson W. Barbosa

Since nobody has commented yet, I'll do part a:

  (15/8)¡ = 675675/131072*sqrt(pi/4)

Assuming I am correct, the fraction portion on the right is multiplied by the fraction on the left we're going to.

i.e. (n/8)¡ = n/8 . ((n-2)/8)¡

Unfortunately, part b is eluding me. I'm not in a very thinking mood at the moment but I should have been able to make more progress than I have :-( I suspect the double factorial function x!! is involved but haven't figured out how yet.

- Pauli

                              
Re: Small Sunday Afternoon Mini-Challenge
Message #17 Posted by Gerson W. Barbosa on 8 May 2010, 11:02 a.m.,
in response to message #16 by Paul Dale

Hi Paul,

You have noticed

16384 * 13/8 * 3465/2048 = 45045

131072 * 15/8 * 45045/16384 = 675675

So the next term will be

1048576 * 17/8 * 675675/131072 = 11486475

which of course is correct. Hadn't seen it.

I've found a way to express this sequence in terms of the Gamma Function:

a(n)=(2^(2+n)*Gamma(n+5/2))/(3*sqrt(pi))

Thus

a(6)=(2^(2+6)*Gamma(6+5/2))/(3*sqrt(pi)) = 675675

a(7)=(2^(2+7)*Gamma(7+5/2))/(3*sqrt(pi)) = 11486475

However, your insight is right: this can be expressed in terms of the double factorial function and the equivalent expression is more elegant and simple.

Part b is more a guessing game rather than a thinking game. If

1 * 2 * 3 * 4 * 5 * 6 ... * n produces special values at 1.5, 2.5, 3.5, 4.5 ... all involving sqrt(pi)

and

1 * 1.5 * 2 * 2.5 * 3 * 3.5 * ... * n produces special values at 1.25, 1.75, 2.25, 3.75, ... all of them involving sqrt(pi/2)

then you can guess what function will produce special values involving sqrt(pi/4) at 1.125, 1,375, 1,625, 1,875, 2,125 ...

Gerson.

      
Re: Small Sunday Afternoon Mini-Challenge
Message #18 Posted by David Hayden on 3 May 2010, 4:17 p.m.,
in response to message #1 by Gerson W. Barbosa

I believe that the solutions presented will all overflow on some inputs, even when the final result can be represented within the precision of the calculator. This is because the solutions compute a large numerator and then divide by a large denominator.

Can you come up with a solution that will always give an answer if the actual answer can be represented within the precision of the calculator? How accurate is your answer? How much time does it take to compute?

Dave

            
Re: Small Sunday Afternoon Mini-Challenge
Message #19 Posted by Namir on 3 May 2010, 4:59 p.m.,
in response to message #18 by David Hayden

Maybe we should switch to the logarithm form of the function, thus end up adding logarithms instead of multiplying numbers. This way the sum does not overflow as quickly as the product of numbers.

            
Re: Small Sunday Afternoon Mini-Challenge
Message #20 Posted by Paul Dale on 3 May 2010, 5:05 p.m.,
in response to message #18 by David Hayden

For the 20b scientific firmware:

001: LBL B
002:  STO+ X
003:  1
004:  x<>y
005:  +
006:  LNGAMMA
007:  LASTx
008:  2
009:  -
010:  2
011:  LN
012:  *
013:  -
014:  e^x
015: RTN

Not as short or neat but handles a larger range of positive inputs.

- Pauli

            
Re: Small Sunday Afternoon Mini-Challenge
Message #21 Posted by C.Ret on 5 May 2010, 5:00 p.m.,
in response to message #18 by David Hayden

Quote:
Can you come up with a solution that will always give an answer if the actual answer can be represented within the precision of the calculator?

By definition x? = 1 × 1.5 × … (x-1) × (x-.5) × x

Patrice shows that x? = 2 × (2x)! / 22x

But this formula is, as you just state it, a ration of two large values and may not produce a correct answer even if actual result is within the precision of the calculator.

But Patrice give the correct recurrent formula : x? = 1 × 2 / 2 × 3 / 2 × 4 / 2 × 5 / 2 × 6 / 2 × 7 / 2 × ... × (2x - 1) / 2 * (2x) / 2

The trick is to divide by 2 after each multiplication from 2x down to 1.

This is my attempt for a RPN code (for HP97)

000 
001     21 11   *LBL A
002        02     2   
003       -35     ×   
004     35 46    STO I
005  16 25 46  f DSZ I
006     21 09   *LBL 9
007        02     2   
008       -24     /   
009     36 46    RCl I
010       -35     ×   
011  16 25 46  f DSZ I
012     22 09    GTO 9
013        51    RTN  

I was able to obtained result up to 41? Without any overflow error. This is not so bad, when considering that 70! Is already out of scope ( > 9.999999E+99 ).


[ Return to Index | Top of Index ]

Go back to the main exhibit hall