The Museum of HP Calculators

HP Forum Archive 10

[ Return to Index | Top of Index ]

A little HP-71B challenge
Message #1 Posted by Ex-PPC member on 14 Feb 2003, 8:31 a.m.

As most of you already know, the HP-71B features an extremely powerful version of BASIC, which among other things, allows recursion, i.e.: a function or subprogram may call itself.

This makes it pretty easy to write a recursive, multiline user-defined function to compute, say, the factorial function:

      n! = 1 x 2 x 3 x ... x (n-1) x n
like this one, for instance, valid for integer N greater than 0:
     10 DEF FNF(N)
     20 IF N=1 THEN FNF=1 ELSE FNF=N*FNF(N-1)
     30 END DEF

As you can see, thanks to recursion it's really easy (if inefficient) to do it using a multiline user-defined function.

The challenge is: try and define a factorial function as above, but using a single-line, single-statement user defined function.

By single-line, single-statement I mean your function must be defined like this:

10 DEF FNF(N)= ...

and it must be recursive, i.e: it must call itself. No "END DEF" nor "@" (statement concatenators) are allowed, nor can you invoke another user-defined function.

Give it yourself a try before looking elsewhere. Once you succeed, this will be a pretty confirmation that single-line user-defined functions can be recursive as well on 71B's BASIC !

      
Re: A little HP-71B challenge
Message #2 Posted by J-F Garnier (France) on 15 Feb 2003, 12:48 p.m.,
in response to message #1 by Ex-PPC member

OK, here is my very inefficient solution:

10 DEF FNF(X)=VAL("1*fnf(x-1)*x"[1,1+12*(X#0)])

My first idea was to use the ITE function, it doesn't exist on the HP-71B ...

J-F

            
Re: A little HP-71B challenge
Message #3 Posted by Katie on 15 Feb 2003, 5:05 p.m.,
in response to message #2 by J-F Garnier (France)

I independently found a similar solution after trying various TRAP solutions that don't work. Here's what I have:

DEF FNF(N)=N*VAL("FNF(N-1)*1"[(N<2)*9+1])

I'd be surprised if there are any non-string type ways of doing this. OR and AND always evaluate both arguments and there are no condition evaluation functions that I can find on the 71b.

      
Re: A little HP-71B challenge
Message #4 Posted by Gordon Dyer on 15 Feb 2003, 7:00 p.m.,
in response to message #1 by Ex-PPC member

I have been causing memory overflows and scanning through my manuals trying to solve this one, no solution yet but I have enjoyed it!
Well done Katie...


[ Return to Index | Top of Index ]

Go back to the main exhibit hall