Small Sunday Afternoon Mini-Challenge



#19

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.


#20

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


#21

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

#22

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


#23

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.

#24

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

#25

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


#26

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

Cheers, AM.


#27

¡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.


#28

It works like a charm :)

Best,
ÁM

#29

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

#30

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.

#31

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.


#32

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.


#33

   (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.


#34

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


#35

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.

#36

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


#37

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.

#38

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

#39

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 ).


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Download] PrimeComm: Alternative small utility to upload/download programs to the HP Prime Erwin Ried 6 2,603 11-17-2013, 10:18 AM
Last Post: Erwin Ried
  [HP PRIME] small font bug display Damien 34 10,099 11-06-2013, 04:31 AM
Last Post: Nacho
  HPCC Mini Conference 2013 hugh steers 6 2,306 09-13-2013, 04:27 PM
Last Post: BruceH
  Sunday distraction Keith Midson 0 822 08-04-2013, 12:28 AM
Last Post: Keith Midson
  Picture from the Mini-HHC (LA) Geir Isene 11 3,212 07-07-2013, 01:06 PM
Last Post: Jim Horn
  A small but colourful bouquet ... Walter B 5 1,744 05-12-2013, 06:31 PM
Last Post: Bill Carter
  Easter Sunday Basic Trigs (HP-12C) Gerson W. Barbosa 29 8,807 04-04-2013, 02:19 PM
Last Post: Gerson W. Barbosa
  My birthday, so a little commemorative mini-challenge ! Valentin Albillo 43 8,663 03-07-2013, 03:44 AM
Last Post: Walter B
  WP 34S mini-challenge (B) Gerson W. Barbosa 17 4,706 12-27-2012, 04:39 PM
Last Post: Marcus von Cube, Germany
  Mini-challenge: HHC2012 RPL programming contest with larger input David Hayden 14 3,666 10-05-2012, 10:36 PM
Last Post: David Hayden

Forum Jump: