Small Sunday Afternoon MiniChallenge  Printable Version + HP Forums (https://archived.hpcalc.org/museumforum) + Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum1.html) + Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum2.html) + Thread: Small Sunday Afternoon MiniChallenge (/thread166727.html) 
Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05022010 Let's define a factoriallike 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 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 9step program for the 12C and most scientific RPN calculators (LBL and RTN and final GTO 00 don't count) and a 16.5byte 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 (the HP12C program will not handle these). Gerson.
Re: Small Sunday Afternoon MiniChallenge  Patrice  05022010 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) * xtransforms to x? = 1 * 2/2 * 3/2 * 4/2 * 5/2 * 6/2 * 7/2 * ... * (2x  1)/2 * 2x/2 and the program on my 41C gives:
01 LBL A Without the GAMMA function, generalizing the program is not very easy.
Patrice
Re: Small Sunday Afternoon MiniChallenge  hugh steers  05022010 ok. 15C
LBL A that's 10 without the bumpers. also, using this (might help others)
function moo(n) i get
> =moo(3.333333)
Re: Small Sunday Afternoon MiniChallenge  hugh steers  05022010 Patrice, sorry im not copying you. i got the same answer :)
Re: Small Sunday Afternoon MiniChallenge  PeterP  05022010 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
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05022010 Quote: 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 HP41. I think an even smaller program is possible on it, but I'll leave that for you. Interestingly on the 33s my 9step solution is 69 bytes long while the 51byte 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 MiniChallenge  Gerson W. Barbosa  05022010 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: You're right. The HP42s 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
Gerson
Re: Small Sunday Afternoon MiniChallenge  Paul Dale  05022010 The 20b scintific load out can do this in one step less and one stack level less:
001: LBL B Unfortunately, I don't see how to do any better.
 Pauli
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05022010 Here are mine:
0001 LBL A
Re: Small Sunday Afternoon MiniChallenge  Ángel Martin  05032010 There's a Gamma function available on the SandMath5 Module for the 41 platform.
Cheers, AM.
Re: Small Sunday Afternoon MiniChallenge  Namir  05032010 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 MiniChallenge  David Hayden  05032010 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 MiniChallenge  Namir  05032010 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 MiniChallenge  Paul Dale  05032010 For the 20b scientific firmware:
001: LBL B Not as short or neat but handles a larger range of positive inputs.
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05032010
Quote: I think its properties are similar to those of x!. For instance,
(1/2)! = 1/2*sqrt(pi) (1/4)? = sqrt(pi/2)
Quote: None that I am aware of. Regards,
Gerson.
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05032010 ¡Hola Ángel! Then this should work like it does on the 42s:
01 LBL "?" 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 MiniChallenge  Ángel Martin  05052010 It works like a charm :)
Best, Re: Small Sunday Afternoon MiniChallenge  C.Ret  05052010 Quote:
By definition
Patrice shows that 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 : 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
I was able to obtained result up to 41? Without any overflow error.
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05052010 (5/8)¡ = 5/4*sqrt(pi/4)Anyone?
Edited: 6 May 2010, 11:38 a.m.
Re: Small Sunday Afternoon MiniChallenge  Paul Dale  05062010 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 . ((n2)/8)¡
Re: Small Sunday Afternoon MiniChallenge  Gerson W. Barbosa  05082010 Hi Paul, You have noticed
16384 * 13/8 * 3465/2048 = 45045 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 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.
