Post Reply 
Problem with HEAD & TAIL Programmes on HP 33s
09-01-2022, 03:59 PM (This post was last modified: 09-02-2022 10:19 AM by Gerald H.)
Post: #1
Problem with HEAD & TAIL Programmes on HP 33s
Previously I have used this programme

Code:
1.    LBL H
2.    x=0?
3.    RTN
4.    LOG
5.    FP
6.    10^x
7.    IP
8.    RTN

H: LN = 24

to return the leading digit of a natural number.

Quite correctly the programme returns a false answer for large input, eg 999,999,999,999.

The following programme solves that problem but is markedly slower

Code:
1.    LBL X
2.    STO N
3.    CLx
4.    10
5.    x<> N
6.    RCL/ N
7.    IP
8.    x≠0?
9.    GTO X
10.    CLx
11.    LASTx
12.    RCL*N
13.    RTN

X: LN = 51

Both programmes preserve the stack.

Has anyone got a better solution?
Find all posts by this user
Quote this message in a reply
09-01-2022, 04:33 PM
Post: #2
RE: Problem with HEAD Programme on HP 33s
What if you use IP instead of FP?

This program is for the HP-42S, but I assume you get the idea:
Code:
00 { 6-Byte Prgm }
01 ENTER
02 LOG
03 IP
04 10↑X
05 ÷
06 IP
07 END
Find all posts by this user
Quote this message in a reply
09-01-2022, 05:14 PM
Post: #3
RE: Problem with HEAD Programme on HP 33s
That doesn't work on a real HP-42S due to the rounding.
Maybe we can do it in two steps?

Code:
00 { 15-Byte Prgm }
01 ENTER
02 LOG
03 2
04 ÷
05 IP
06 10↑X
07 ÷
08 IP
09 ENTER
10 LOG
11 IP
12 10↑X
13 ÷
14 IP
15 END

Or then a little bit shorter:
Code:
00 { 15-Byte Prgm }
01 ENTER
02 LOG
03 2
04 ÷
05 XEQ 00
06 ENTER
07 LOG
08▸LBL 00
09 IP
10 10↑X
11 ÷
12 IP
13 END
Find all posts by this user
Quote this message in a reply
09-01-2022, 06:33 PM
Post: #4
RE: Problem with HEAD Programme on HP 33s
Another small improvement:
Code:
00 { 12-Byte Prgm }
01 ENTER
02 SQRT
03 XEQ 00
04 ENTER
05▸LBL 00
06 LOG
07 IP
08 10↑X
09 ÷
10 IP
11 END
Find all posts by this user
Quote this message in a reply
09-01-2022, 06:45 PM
Post: #5
RE: Problem with HEAD Programme on HP 33s
Thank you, Thomas, I take my hat off to you!

Your longer version is preferable on the 33s due to paucity of labels.

My adaptation below preserves the stack in order to fit in with other programmes.

Timings show your programme (in my adaptation) on 33s faster than my 2nd programme in 1st posting by a factor of 0.56, ie 17.41s against 31.18s.


Code:
1.    LBL X
2.    x=0?
3.    RTN
4.    STO N
5.    LOG
6.    2
7.    /
8.    IP
9.    ALOG
10.    x<> N
11.    RCL/ N
12.    IP
13.    STO N
14.    LOG
15.    IP
16.    ALOG
17.    x<> N
18.    RCL/ N
19.    IP
20.    RTN

X: LN = 72
Find all posts by this user
Quote this message in a reply
09-01-2022, 07:34 PM
Post: #6
RE: Problem with HEAD Programme on HP 33s
But then you can still replace:
Code:
5.    LOG
6.    2
7.    /

… by:
Code:
5.    SQRT
6.    LOG

… thus preserving the stack.

Also the following command is not really needed:
Code:
12.    IP
Find all posts by this user
Quote this message in a reply
09-02-2022, 02:32 AM
Post: #7
RE: Problem with HEAD Programme on HP 33s
Very nice of you to take the time to help me & I hope others.

Yes, my version failed to preserve stack.

Deletion of old line 12 (new line 11 in programme below) causes errors for largest numbers (>999999999988) & it sems a shame to save one line to lose the full range.

I was a little shocked to discover my original version (1st in 1st posting) gave a wrong value for input 9.

A learning experience for me.

So here's latest version that preserves stack:

Code:
1.    LBL X
2.    x=0?
3.    RTN
4.    STO N
5.    SQRT
6.    LOG
7.    IP
8.    ALOG
9.    x<> N
10.    RCL/ N
11.    IP
12.    STO N
13.    LOG
14.    IP
15.    ALOG
16.    x<> N
17.    RCL/ N
18.    IP
19.    RTN

X: LN = 57
Find all posts by this user
Quote this message in a reply
09-02-2022, 10:14 AM (This post was last modified: 09-02-2022 10:14 AM by Gerald H.)
Post: #8
RE: Problem with HEAD Programme on HP 33s
I had a similar problem with the programme TAIL, which removes the initial digit of a natural number, consistently erring for very large numbers:

Code:
1.    LBL T
2.    x=0?
3.    RTN
4.    STO N
5.    LOG
6.    IP
7.    10^x
8.    STO/ N
9.    x<> N
10.    FP
11.    RCL* N
12.    RTN

T: LN = 36

Following Thomas' suggestions in his programme, I now use the inflated programme below, which gives consistently correct answers.

I write "inflated" as the programme is more than double the size of my old programme, but perhaps it's as small as can be.

Code:
1.    LBL T
2.    x=0?
3.    RTN
4.    STO N
5.    SQRT
6.    LOG
7.    IP
8.    ALOG
9.    STO T
10.    CLx
11.    RMDR(N:T)
12.    STO R
13.    +/-
14.    RCL+ N
15.    STO N
16.    LOG
17.    IP
18.    ALOG
19.    STO L
20.    CLx
21.    RMDR(N:L)
22.    RCL+ R
23.    RTN

T: LN = 87
Find all posts by this user
Quote this message in a reply
Post Reply 




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