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:
(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. |
|||
01-21-2014, 07:29 AM
Post: #2
|
|||
|
|||
RE: WP 34S mini-challenge
How about:
Code: 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 |
|||
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
Code:
Enter 100,000 [g] [Σ] [A] and wait some seconds and you'll get 5,000,050,000. d:-) |
|||
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 - Pauli |
|||
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 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. |
|||
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 HA HA! Greetings, Massimo -+×÷ ↔ left is right and right is wrong |
|||
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:
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:-/ |
|||
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 factHow 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 :-) |
|||
01-21-2014, 10:44 AM
Post: #9
|
|||
|
|||
RE: WP 34S mini-challenge
(01-21-2014 07:29 AM)Paul Dale Wrote: How about: 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. |
|||
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: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. |
|||
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 My calculator took 35 s for calculating the sum of 1000 numbers. I admit I used my PC for your original challenge. d;-) |
|||
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² 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 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 - Pauli |
|||
01-21-2014, 11:34 AM
Post: #13
|
|||
|
|||
RE: WP 34S mini-challenge
Walter's stack preserving solution can be improved:
Code: LBL A 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 - Pauli |
|||
01-21-2014, 11:34 AM
Post: #14
|
|||
|
|||
RE: WP 34S mini-challenge
(01-21-2014 08:26 AM)walter b Wrote:Or, if we don't mind waiting:(01-21-2014 07:59 AM)walter b Wrote: I've got a two-step program doing what is requested. Code:
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. |
|||
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 |
|||
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 A voice of logical reason tempers the insanity of an over-zealous engineer. Still, we'll likely know once your solution is published. - Pauli |
|||
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, 12:20 PM
Post: #18
|
|||
|
|||
RE: WP 34S mini-challenge | |||
01-21-2014, 12:22 PM
Post: #19
|
|||
|
|||
RE: WP 34S mini-challenge
(01-21-2014 11:55 AM)Paul Dale Wrote:You're right, I overlooked the integer mode impact on the stack(01-21-2014 08:26 AM)Didier Lachieze Wrote: Here is my solution: So, what about this one: Code: 01 LBL A |
|||
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. |
|||
« Next Oldest | Next Newest »
|
User(s) browsing this thread: 10 Guest(s)