HP Forums
Problem with HEAD & TAIL Programmes on HP 33s - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: General Forum (/forum-4.html)
+--- Thread: Problem with HEAD & TAIL Programmes on HP 33s (/thread-18742.html)



Problem with HEAD & TAIL Programmes on HP 33s - Gerald H - 09-01-2022 03:59 PM

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?


RE: Problem with HEAD Programme on HP 33s - Thomas Klemm - 09-01-2022 04:33 PM

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



RE: Problem with HEAD Programme on HP 33s - Thomas Klemm - 09-01-2022 05:14 PM

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



RE: Problem with HEAD Programme on HP 33s - Thomas Klemm - 09-01-2022 06:33 PM

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



RE: Problem with HEAD Programme on HP 33s - Gerald H - 09-01-2022 06:45 PM

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



RE: Problem with HEAD Programme on HP 33s - Thomas Klemm - 09-01-2022 07:34 PM

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



RE: Problem with HEAD Programme on HP 33s - Gerald H - 09-02-2022 02:32 AM

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



RE: Problem with HEAD Programme on HP 33s - Gerald H - 09-02-2022 10:14 AM

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