Programming Exercise (HP-15C, 15C LE - and others)
03-22-2014, 09:47 PM
Post: #21
 Werner Senior Member Posts: 328 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
42S:

Code:
00 { 20-Byte Prgm }
01>LBL "S"
02 4
03 10^X
04 ENTER
05 CLX
06>LBL 00
07 RCL ST Y
08 1/X
09 X<>Y
10 -
11 DSE ST Y
12 GTO 00
13 END

Cheers, Werner
03-22-2014, 10:19 PM (This post was last modified: 03-22-2014 10:20 PM by Dave Britten.)
Post: #22
 Dave Britten Senior Member Posts: 966 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
Got a TI-95 in the mail today, and (naturally) had a go at this with the "new" machine:

Code:
LBL 00
10000
STO N
1
STO S
0
STO X
LBL 01
RCL N
1/x
*
RCL S
+/-
STO S
=
ST+ X
DSZ N
GTL 01
RCL X
HLT

This ran in just a hair under 13 minutes. (Make sure you ASM it.)

Some observations:
The TI-95 sort of reminds me of an algebraic HP-41C. The ASM instruction is interesting in that it translates all label branches (GTL, SBL) into absolute addresses (GTO, SBR) in order to improve speed in programs that do a lot of tight looping and branching. You can reverse the process with INV ASM to make editing easier. The STO/RCL operations are one byte smaller when using the alpha-named registers, as opposed to their numeric names, but there doesn't seem to be any meaningful difference in execution speed.
03-23-2014, 12:36 AM (This post was last modified: 04-01-2014 02:48 AM by Gerson W. Barbosa.)
Post: #23
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-22-2014 09:47 PM)Werner Wrote:  42S:

Code:
00 { 20-Byte Prgm }
01>LBL "S"
02 4
03 10^X
04 ENTER
05 CLX
06>LBL 00
07 RCL ST Y
08 1/X
09 X<>Y
10 -
11 DSE ST Y
12 GTO 00
13 END

14 minutes 59 seconds on my HP-42S! The faster running time on the HP-20S can be explained by the higher perfomance index of the latter, according to this benchmark.

The WP 34S version is about only 200 milliseconds slower than the fastest one so far:
Code:

LBL A                         LBL A
TICKS                         TICKS
EEX                           EEX
4                             4
ENTER^                        #000
#000                          #001
RCL Y                         +/-
1/x                           RCL/ Z
x<> Y                         STO+ Y
-                             x<> L
DSE Y                         DSE Z
BACK 005                      BACK 005
TICKS                         R^
RCL- T                        TICKS
END                           RCL- Y
END

166.2 (avg. of 5 runs)        163.8 (avg. of 5 runs)

16.6 seconds                  16.4 seconds

Cheers,

Gerson.

Edited to correct an error: 14 minutes 59 seconds, not 14.59 seconds.
03-23-2014, 01:31 AM (This post was last modified: 03-23-2014 04:31 AM by Gerson W. Barbosa.)
Post: #24
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-22-2014 10:19 PM)Dave Britten Wrote:  Got a TI-95 in the mail today, and (naturally) had a go at this with the "new" machine:

Code:
LBL 00
10000
STO N
1
STO S
0
STO X
LBL 01
RCL N
1/x
*
RCL S
+/-
STO S
=
ST+ X
DSZ N
GTL 01
RCL X
HLT

This ran in just a hair under 13 minutes. (Make sure you ASM it.)

This runs on the TI-57 with only minor modifications:

Code:

00 LBL 0
01 STO 0
02 1
03 STO 2
04 0
05 STO 3
06 LBL 1
07 RCL 0
08 1/x
09 *
10 RCL 2
11 +/-
12 STO 2
13 +
14 RCL 3
15 =
16 STO 3
17 DSZ
18 GTO 1
19 RCL 3
20 R/S

Alas, no ASM instruction. 100 SBR 0 takes exactly 100 seconds and returns 0.6881722 (0.6881721794), or about 2 hours and 46 minutes for 10000 iterations.

Regards,

Gerson.

----------------------

0.6930972 (0.6930971831), after 2 hours and 51 minutes.
03-23-2014, 08:54 AM (This post was last modified: 03-23-2014 08:58 AM by RMollov.)
Post: #25
 RMollov Member Posts: 199 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-22-2014 12:47 PM)Gerson W. Barbosa Wrote:  - it takes too long: 4 min 6 sec.

Looks great, but the WP34s struggles. Mine turned itself off several times until eventually got to the end. I have Bit's Y register showing version on and wonder if that causes problems. It also shows result as 0.69309718306 but takes forever.
iPhone emulator solves it in 11 secs.
Interesting, after turning off I turn it on and R/S keeps it going. There is something dodgy with estimating batteries charge IMO.
03-23-2014, 08:59 AM
Post: #26
 Thomas Klemm Senior Member Posts: 1,145 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-22-2014 03:02 AM)Gerson W. Barbosa Wrote:  But we can easily break the 20-second barrier on the WP 34S

Yet another idea is to combine two terms: $$\frac{1}{n}-\frac{1}{n+1}=\frac{1}{n(n+1)}$$
Code:
LBL A
0
RCL Y
DEC Z
RCL* Z
1/x
+
DSZ Y
BACK 006
END
10000
XEQ A

Code:
LBL B
INC X
*
1/x
END
9999.00102
$$\sum$$ B

We don't have to deal with the alternating sign and it should be both faster and more accurate.

Cheers
Thomas
03-23-2014, 11:26 AM
Post: #27
 Marcus von Cube Senior Member Posts: 754 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-23-2014 08:54 AM)RMollov Wrote:  Looks great, but the WP34s struggles. ...There is something dodgy with estimating batteries charge IMO.

Maybe the internal resistance of the batteries is a bit high so that the voltage drops under load to a point where the calculator shuts down. Try again with fresh batteries or switch the calculator to SLOW mode which limits the clock frequency to about half the maximum. This should remove some strain from the batteries.

Marcus von Cube
Wehrheim, Germany
http://www.mvcsys.de
http://wp34s.sf.net
http://mvcsys.de/doc/basic-compare.html
03-23-2014, 01:49 PM (This post was last modified: 03-23-2014 05:59 PM by Gerson W. Barbosa.)
Post: #28
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-23-2014 08:59 AM)Thomas Klemm Wrote:
(03-22-2014 03:02 AM)Gerson W. Barbosa Wrote:  But we can easily break the 20-second barrier on the WP 34S

Yet another idea is to combine two terms: $$\frac{1}{n}-\frac{1}{n+1}=\frac{1}{n(n+1)}$$
Code:
LBL A
0
RCL Y
DEC Z
RCL* Z
1/x
+
DSZ Y
BACK 006
END
10000
XEQ A

Code:
LBL B
INC X
*
1/x
END
9999.00102
$$\sum$$ B

We don't have to deal with the alternating sign and it should be both faster and more accurate.

Cheers
Thomas
You've moved the time barrier to the 10-second line! 10.8 seconds, perhaps a few milliseconds less. The summation now takes about half the previous time, but still disappointly long: 2 min 5 sec.

Cheers,

Gerson.

P.S.: Well, not item (b) of the exercise anymore, but if all we want is just computing the sum of the first ten thousand terms of the series then we can do it in one tenth of a second (one TICK) and eleven steps at most (LBL and END included), using information available in this thread. The result will be accurate to 20 digits in double precision mode.
03-25-2014, 11:27 AM
Post: #29
 Dave Britten Senior Member Posts: 966 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
I ran this through my Casio fx9860G Slim:

Code:

0\->X
For 10000\->C To 1 Step -1
X+C\-1*(-1)^(C+1)\->X
Next
X

This prints 0.6930971831 in just under a minute. It's definitely one of the faster models available. Too bad the interface design is so heavily modal - it's much too cumbersome for day-to-day use. I don't know why Casio has clung to that paradigm for so long.
03-25-2014, 03:50 PM
Post: #30
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)

HP-4C, two terms per loop, but preserving the original fractions:

Code:

01 LBL 'S
02 4
03 10^X
04 0
05 LBL 00
06 RCL Y
07 1/X
08 -
09 DSE Y
10 RCL Y
11 1/X
12 +
13 DSE Y
14 GTO 00
15 END

0.6930971830, 32 min 9 sec.
03-25-2014, 09:11 PM
Post: #31
 Thomas Klemm Senior Member Posts: 1,145 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-23-2014 01:49 PM)Gerson W. Barbosa Wrote:  P.S.: Well, not item (b) of the exercise anymore, but if all we want is just computing the sum of the first ten thousand terms of the series then we can do it in one tenth of a second (one TICK) and eleven steps at most (LBL and END included), using information available in this thread. The result will be accurate to 20 digits in double precision mode.

A proof can be found here:
Pi, Euler Numbers, and Asymptotic Expansions
Author(s): J. M. Borwein, P. B. Borwein, K. Dilcher

Cheers
Thomas
03-25-2014, 09:27 PM
Post: #32
 Thomas Klemm Senior Member Posts: 1,145 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-25-2014 03:50 PM)Gerson W. Barbosa Wrote:  0.6930971830, 32 min 9 sec.
Is that faster than combining the two terms: $$\frac{1}{n}-\frac{1}{n+1}=\frac{1}{n(n+1)}$$ ?
03-25-2014, 11:45 PM (This post was last modified: 03-26-2014 02:47 AM by Gerson W. Barbosa.)
Post: #33
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-25-2014 09:27 PM)Thomas Klemm Wrote:
(03-25-2014 03:50 PM)Gerson W. Barbosa Wrote:  0.6930971830, 32 min 9 sec.
Is that faster than combining the two terms: $$\frac{1}{n}-\frac{1}{n+1}=\frac{1}{n(n+1)}$$ ?
I don't think so. I haven't tested it on the HP-41C, but this is faster on the WP 34S. BTW, shouldn't it be $\frac{1}{n-1}-\frac{1}{n}=\frac{1}{n^{2}-n}$?

I just prefer the left side of the expression because it preserves the original terms:

$1-\frac{1}{2}+\frac{1}{3}-\frac{1}{4}+ \cdots +\frac{1}{9999}-\frac{1}{10000}$

If n = 4, for instance, then

$\frac{1}{3}-\frac{1}{4}\neq \frac{1}{12}$

on a 10-digit calculator ( 3 1/x 4 1/x 12 1/x - --> -3e-11 ).

Cheers,

Gerson.

--------------

P.S.:

HP-41C

Code:

01 LBL'S
02 4
03 10^X
04  0
05 LBL 00
06 RCL Y
07 DSE Z
08 RCL Z
09 *
10 1/X
11 +
12 DSE Y
13 GTO 00
14 END

0.6930971831 (28 min 28 sec)
03-25-2014, 11:46 PM
Post: #34
 churichuro Junior Member Posts: 28 Joined: Feb 2014
RE: Programming Exercise (HP-15C, 15C LE - and others)
on my HP50 with UBasic this take 5.5 Sec

Code:

suma=0
for n= 10000 to 1 step -1
suma= suma + (1 / n)* (-1)^ (n+1)
next
fix 10.9
print
print "Sum ="; suma
end

result:

UBasic v 0.2b (C)
Suma =0.693097183

press << ON >>
03-26-2014, 12:00 AM
Post: #35
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-25-2014 11:46 PM)churichuro Wrote:  on my HP50 with UBasic this take 5.5 Sec

Code:

suma=0
for n= 10000 to 1 step -1
suma= suma + (1 / n)* (-1)^ (n+1)
next
fix 10.9
print
print "Sum ="; suma
end

result:

UBasic v 0.2b (C)
Suma =0.693097183

press << ON >>

Would you try Thomas Klemm's idea, that is, evaluating two terms per loop? Thanks!

Code:

suma=0
for n= 5000 to 1 step -1
suma= suma + 1 / (n - 1) - 1 / n
next
fix 10.9
print
print "Sum ="; suma
end
03-26-2014, 02:22 AM
Post: #36
 churichuro Junior Member Posts: 28 Joined: Feb 2014
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-26-2014 12:00 AM)Gerson W. Barbosa Wrote:  Would you try Thomas Klemm's idea, that is, evaluating two terms per loop? Thanks!

Code:

suma=0
for n= 5000 to 1 step -1
suma= suma + 1 / (n - 1) - 1 / n
next
fix 10.9
print
print "Sum ="; suma
end

Do this change: for no get error "Divide by zero" (well, this error but in spanish)
for n=5000 to 2 step -1

get suma= 0.999800000 in about 3 sec

if change to
for n=10000 to 2 step -1
get suma= 0.999900000 in 5.5 sec
03-26-2014, 02:42 AM
Post: #37
 Gerson W. Barbosa Senior Member Posts: 1,056 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-26-2014 02:22 AM)churichuro Wrote:
(03-26-2014 12:00 AM)Gerson W. Barbosa Wrote:  Would you try Thomas Klemm's idea, that is, evaluating two terms per loop? Thanks!

Code:

suma=0
for n= 5000 to 1 step -1
suma= suma + 1 / (n - 1) - 1 / n
next
fix 10.9
print
print "Sum ="; suma
end

Do this change: for no get error "Divide by zero" (well, this error but in spanish)
for n=5000 to 2 step -1

get suma= 0.999800000 in about 3 sec

if change to
for n=10000 to 2 step -1
get suma= 0.999900000 in 5.5 sec

Oops! this should have been
...
for n=10000 to 2 step -2
...

Sorry!
03-26-2014, 03:59 AM
Post: #38
 Thomas Klemm Senior Member Posts: 1,145 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
(03-25-2014 11:45 PM)Gerson W. Barbosa Wrote:  If n = 4, for instance, then

$\frac{1}{3}-\frac{1}{4}\neq \frac{1}{12}$

on a 10-digit calculator ( 3 1/x 4 1/x - 12 1/x - --> -3e-11 ).

Since $$\frac{1}{n}$$ and $$\frac{1}{n+1}$$ are close we experience extinction when calculating the difference. Thus using $$\frac{1}{n(n+1)}$$ is more accurate.

Cheers
Thomas
03-26-2014, 02:04 PM
Post: #39
 Dave Britten Senior Member Posts: 966 Joined: Dec 2013
RE: Programming Exercise (HP-15C, 15C LE - and others)
Alright, I've managed to implement this on what may be the weakest possible machine that can still handle the algorithm: the Sinclair Cambridge Programmable. The manual is available on Katie's site, but allow me to summarize the notable points for this barely-programmable calculator:

1. Precision is awful. Trig functions are often only accurate to 2 or 3 figures.
2. There's no built-in PI constant, but you can get about 5 digits from 2 * arccos 0 (all trig functions operate in radians). Or just key it in manually for better accuracy.
3. There's no y^x operator. You have to do e^(y * ln x), and like trig, it isn't particularly accurate.
4. You only get 36 completely unmerged program steps to work with. About half the available operations require two steps because of the down-shift key. Constant entry requires an additional 'escape' key.
5. There's only a single storage register. Thankfully, there's an operator to exchange the display with the register, otherwise I don't think this would have been possible.
6. Branching is all absolute, and requires FOUR steps (shift, go to/go if neg, two-digit step number).
7. 'go if neg' is the only conditional test available. If the display is negative, it acts like go to, otherwise skips the next two steps (the line number).

To use, enter the desired number of iterations (e.g. 10000) in the display, and press RUN.

Code:
00    sto
01    #        Escape key to begin constant entry (terminated by any operator)
02    0
03    +
04    (
05    rcl
06    -
07    #
08    1
09    x        Calculator has no operator precedence. This becomes (n - 1) * n.
10    rcl
11    /        The four operators change meaning when followed by another operator.
12    )        Division changes to 1/x from the ).
13    =
14    v        The downshift key.
15    MEx        Swap display with storage register.
16    -
17    #
18    3
19    =
20    v
21    go if neg        Exit loop if n = 2. (n - 3 < 0)
22    3
23    4
24    +
25    #
26    1
27    =
28    v
29    MEx        Put loop counter back in storage and bring back accumulator.
30    v
31    go to        Go again.
32    0
33    3
34    rcl        Get the accumulated result and stop execution.
35    stop

100 iterations (50 actual passes through the loop) takes 18.5 seconds. A full run of 10000 iterations would clock in at about 30 minutes.
03-26-2014, 06:20 PM
Post: #40
 churichuro Junior Member Posts: 28 Joined: Feb 2014
RE: Programming Exercise (HP-15C, 15C LE - and others)
version for HP PRIME:

Code:

EXPORT TEST2()
BEGIN
LOCAL suma,n;

suma:= 0;
FOR n FROM 10000 DOWNTO 1 DO
suma:= suma + (1/n)*(-1)^(n+1);
END;
MSGBOX("Suma="+suma);
END;

result in about 1 sec !
Suma=.693097183059
 « Next Oldest | Next Newest »

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