Post Reply 
WP 34S mini-challenge
01-21-2014, 02:02 AM (This post was last modified: 01-21-2014 02:09 AM by Gerson W. Barbosa.)
Post: #1
WP 34S mini-challenge
Consider the following HP-15C program:

[Image: Gauss_HP-15C_zpsec34727a.png]

(this listing courtesy of Torsten's HP-15C Simulator)

It computes the sum of the first n natural numbers using Gauss's formula. For instance,

100000 GSB A --> 5000050000

Write an equivalent WP 34S program using the same number of steps at most (5, if we don't count LBL and RTN) and preserving the stack (that is, Y, Z and T registers must not be affected by the program). The default settings must be kept unchanged at program termination.
Find all posts by this user
Quote this message in a reply
01-21-2014, 07:29 AM
Post: #2
RE: WP 34S mini-challenge
How about:

Code:
        t<> I
        2
        INC Y
        COMB
        t<> I

This doesn't preserve the stack in SSIZE 8 mode and can't easily be modified to do so. For an extra step, allocate a local and use that instead of I.

- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 07:59 AM
Post: #3
RE: WP 34S mini-challenge
I've got a two-step program doing what is requested. Well, not counting LBL and RTN it's a zero-step program in fact Smile

Code:

LBL A
RTN

Enter 100,000 [g] [Σ] [A] and wait some seconds and you'll get 5,000,050,000.

d:-)
Find all posts by this user
Quote this message in a reply
01-21-2014, 08:24 AM (This post was last modified: 01-21-2014 08:25 AM by Paul Dale.)
Post: #4
RE: WP 34S mini-challenge
Waler, the RTN is implied resulting in a single step solution:

Code:
LBL A

Which would come down to -1 steps once the RTN and LBL are taken off. I somehow don't think this is the intended answer and it fails hopelessly for large inputs: 1E15. Okay, it doesn't fail for that input but your batteries will Smile


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 08:26 AM (This post was last modified: 01-21-2014 08:40 AM by Didier Lachieze.)
Post: #5
RE: WP 34S mini-challenge
Here is my solution:

Code:
01 LBL A
02 x²
03 RCL+ L
04 BASE 02
05 SR 01
06 DECM
07 RTN

There are at least two other variants that fit in the same number of steps and provide the same result while not disturbing the stack either 4 or 8 levels. Apart from X, the only register that is modified is L, but it doesn't hold the initial value of X.
Find all posts by this user
Quote this message in a reply
01-21-2014, 08:26 AM
Post: #6
RE: WP 34S mini-challenge
(01-21-2014 07:59 AM)walter b Wrote:  I've got a two-step program doing what is requested. Well, not counting LBL and RTN it's a zero-step program in fact Smile

Code:

LBL A
RTN

Enter 100,000 [g] [Σ] [A] and wait some seconds and you'll get 5,000,050,000.

d:-)

HA HA! Big Grin

Greetings,
    Massimo

-+×÷ ↔ left is right and right is wrong
Visit this user's website Find all posts by this user
Quote this message in a reply
01-21-2014, 08:26 AM
Post: #7
RE: WP 34S mini-challenge
(01-21-2014 07:59 AM)walter b Wrote:  I've got a two-step program doing what is requested.

Alas, that's not true - it doesn't preserve the stack. :( So I had to add some administrative steps:

Code:

LBL B
STOS 10
Σ A
STO 00
RCLS 10
DROP
RCL 00
RTN
LBL A
RTN

Enter 100,000 [XEQ] [B] and wait some seconds and you'll get 5,000,050,000. And the stack is also preserved for 8 levels. But - not counting LBL and RTN - it's one step too much now.

d:-/
Find all posts by this user
Quote this message in a reply
01-21-2014, 10:38 AM
Post: #8
RE: WP 34S mini-challenge
(01-21-2014 07:59 AM)walter b Wrote:  I've got a two-step program doing what is requested. Well, not counting LBL and RTN it's a zero-step program in fact Smile

Code:

LBL A
RTN

Enter 100,000 [g] [Σ] [A] and wait some seconds and you'll get 5,000,050,000.

d:-)
How many seconds? I'd tried this zero-step program for 100 and remember it took a while before showing the answer. That's why I multiplied the usual example by 1000 :-)
Find all posts by this user
Quote this message in a reply
01-21-2014, 10:44 AM
Post: #9
RE: WP 34S mini-challenge
(01-21-2014 07:29 AM)Paul Dale Wrote:  How about:

Code:
        t<> I
        2
        INC Y
        COMB
        t<> I

This doesn't preserve the stack in SSIZE 8 mode and can't easily be modified to do so. For an extra step, allocate a local and use that instead of I.

- Pauli

Nice solution! There's no need no worry about the 8-level stack since the calculator will have to be at the default 4-level stack when the program terminates.
Find all posts by this user
Quote this message in a reply
01-21-2014, 10:51 AM
Post: #10
RE: WP 34S mini-challenge
(01-21-2014 08:26 AM)Didier Lachieze Wrote:  Here is my solution:

Code:
01 LBL A
02 x²
03 RCL+ L
04 BASE 02
05 SR 01
06 DECM
07 RTN

There are at least two other variants that fit in the same number of steps and provide the same result while not disturbing the stack either 4 or 8 levels. Apart from X, the only register that is modified is L, but it doesn't hold the initial value of X.
Another great solution! Like Pauli's it gives exact answers. Mine has a difference of some units in the last digit of the mantissa for the given example. I should have mentioned it, but that might give a hint of what I was using. Mine preserves the 8-level stack as well, but that's not mandatory since we're keeping the default settings.
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:08 AM
Post: #11
RE: WP 34S mini-challenge
(01-21-2014 10:38 AM)Gerson W. Barbosa Wrote:  
(01-21-2014 07:59 AM)walter b Wrote:  I've got a two-step program doing what is requested. Well, not counting LBL and RTN it's a zero-step program in fact Smile

Code:

LBL A
RTN

Enter 100,000 [g] [Σ] [A] and wait some seconds and you'll get 5,000,050,000.

How many seconds? I'd tried this zero-step program for 100 and remember it took a while before showing the answer. That's why I multiplied the usual example by 1000

My calculator took 35 s for calculating the sum of 1000 numbers. I admit I used my PC for your original challenge.

d;-)
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:29 AM
Post: #12
RE: WP 34S mini-challenge
I considered an integer mode solution too. The shortest I could come up with is:

Code:
        x²
        RCL+ L
        SR 01

Which is three steps. However, I figured decimal mode was wanted and proposed the alternative above which works for non-integer inputs. Sadly, I had an extra hour to think about this problem on my drive home today after an accident on the freeway Sad

I wouldn't be at all surprised if a four (or less) step solution was possible. The 34S has an (almost) insane number of functions and people on this forum are better at utilising the calculator than I. I consider this a good thing, of course Smile


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:34 AM
Post: #13
RE: WP 34S mini-challenge
Walter's stack preserving solution can be improved:

Code:
        LBL A
        STOS 00
        Σ 00
        STO 00
        RCLS 00
        LBL 00
        RTN

Which meets the five steps not counting the initial LBL and RTN.

I still wouldn't want to wait for the answer to a large input value Smile


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:34 AM
Post: #14
RE: WP 34S mini-challenge
(01-21-2014 08:26 AM)walter b Wrote:  
(01-21-2014 07:59 AM)walter b Wrote:  I've got a two-step program doing what is requested.

Alas, that's not true - it doesn't preserve the stack. Sad So I had to add some administrative steps:

Code:

LBL B
STOS 10
Σ A
STO 00
RCLS 10
DROP
RCL 00
RTN
LBL A
RTN

Enter 100,000 [XEQ] [B] and wait some seconds and you'll get 5,000,050,000. And the stack is also preserved for 8 levels. But - not counting LBL and RTN - it's one step too much now.

dUndecided
Or, if we don't mind waiting:

Code:

001  LBL A
002  DEC X
003  STO 00
004  RCL+ 00
005  DSE 00
006  BACK 002
007  INC X
008  END

One extra step, though.

Regards,

Gerson.

P.S.: My original solution uses a couple of functions whose inclusion in the WP 34S instruction set was your decision, if I am not wrong.
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:48 AM
Post: #15
RE: WP 34S mini-challenge
I am somewhat intrigued by Gerson's solution here (and I'm sure he has one). It seems like it would be an improvement on those submitted thus far even though it isn't exact -- it deals with the two different stack sizes, doesn't require an integer mode switch and doesn't take forever to execute.

Time will tell of course.


The divide by two is what is stumping me. x² RCL+L 2 ÷ is the obvious solution but it doesn't preserve the stack and saving the stack consumes more steps than are available. I can't think of a way to circumvent these issues besides saving T and using COMB. Even using the statistical Σ+ command doesn't seem to help.

Integer mode can replace the 2 ÷ by SR 01 or ASR 01 without impacting the stack usage but that is specific to integer mode. We didn't support implicit integer to real conversion for all non-real defined functions (rightfully so BTW). Likewise my proposal to include arithmetic operators with a small constant argument was vetoed, otherwise the space & stack saving ÷2 instruction would have been available.

I think I'm missing something here.....


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:51 AM
Post: #16
RE: WP 34S mini-challenge
(01-21-2014 11:34 AM)Gerson W. Barbosa Wrote:  P.S.: My original solution uses a couple of functions whose inclusion in the WP 34S instruction set was your decision, if I am not wrong.

More likely mine. Walter sensibly resisted many many functions I wanted to include Smile A voice of logical reason tempers the insanity of an over-zealous engineer.

Still, we'll likely know once your solution is published.


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 11:55 AM (This post was last modified: 01-21-2014 10:02 PM by Paul Dale.)
Post: #17
RE: WP 34S mini-challenge
(01-21-2014 08:26 AM)Didier Lachieze Wrote:  Here is my solution:
...

This solution does disturb the stack. If Y contains 2.3, it will end up containing 2 after the switch to integer mode and back. Still, a very good try.


- Pauli
Find all posts by this user
Quote this message in a reply
01-21-2014, 12:20 PM
Post: #18
RE: WP 34S mini-challenge
(01-21-2014 11:34 AM)Paul Dale Wrote:  Walter's stack preserving solution can be improved:
Code:
        LBL A
        STOS 00
        Σ 00
        STO 00
        RCLS 00
        LBL 00
        RTN
Which meets the five steps not counting the initial LBL and RTN.

Very nice idea! Thanks!

d:-)
Find all posts by this user
Quote this message in a reply
01-21-2014, 12:22 PM
Post: #19
RE: WP 34S mini-challenge
(01-21-2014 11:55 AM)Paul Dale Wrote:  
(01-21-2014 08:26 AM)Didier Lachieze Wrote:  Here is my solution:
...

This solution does disturb the stack. If Y contains 2.3, it will end up containing after the switch to integer mode and back. Still, a very good try.


- Pauli
You're right, I overlooked the integer mode impact on the stack Sad

So, what about this one:
Code:
01 LBL A
02 x²
03 STO+ L
04 CLx
05 #1/2
06 RCL* L
07 RTN
Find all posts by this user
Quote this message in a reply
01-21-2014, 12:23 PM (This post was last modified: 01-21-2014 12:48 PM by Gerson W. Barbosa.)
Post: #20
RE: WP 34S mini-challenge
(01-21-2014 11:48 AM)Paul Dale Wrote:  The divide by two is what is stumping me. x² RCL+L 2 ÷ is the obvious solution but it doesn't preserve the stack and saving the stack consumes more steps than are available.
The WP 34 does provide a way to divide by 2 in DECM mode without causing a stack lift, at the cost of only one extra step.
Find all posts by this user
Quote this message in a reply
Post Reply 




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