| Author | Comment | ||||||||
|---|---|---|---|---|---|---|---|---|---|
Crawl and 1OOO |
|||||||||
Crawl and 1OOO |
|||||||||
|
Here's the first program I typed into my HP35s. It uses a series to estimate Pi.
A001 LBL A A002 1 A003 STO A A004 0 A005 STO B A006 100 A007 STO C A008 RCL A A009 2 A010 x A011 1 A012 - A013 1/x A014 RCL A A015 1 A016 + A017 -1 A018 x<>y A019 y^x A020 x A021 STO+ B A022 1 A023 STO+ A A024 DSE C A025 GTO A008 A026 STOP And LN=89 CK=DFA7 After running, B has 0.78289822... Times 4 gives 3.1315929... If you added half of the next term, you'd get 3.141543... But if you added the reciprocal of the number of terms you used (100), you'd get even more accurate 3.1415929... I think that's based on the Euler number thing Wikipedia talks about for the Leibniz formula. |
|||||||||
Crawl and 1OOO |
|||||||||
|
I did a calculator challenge today. Hadn't done one in a while. It was to write a program that finds "Keith Numbers".
The real advantage to challenges like this is just that it gives me a reason to play with RPL programming and become more familiar with it. I use my HP50g a lot, but it seems I don't program with it very much. And it has a lot of functionality I need to use more, too (example, hypothesis testing). For this program, you put an integer D>=2 on the stack and run the program. It returns all Keith numbers with D digits. Even the 2 digit case takes about a minute, though. Might be better to run D>=3 on an emulator! With 2 on the stack, running the program gives 14 19 28 47 61 75 Am only now checking it with D=3; it's taking some time. (Edit: It did find 197 and 742) Since I don't use RPL enough, I forgot the RPL equivalent to the BASIC MID$ command. It's SUB. SUB takes as arguments the string, and two numbers. I think the numbers are the first entry of the string you want, to the next entry of the string you want. So, if you just want the nth character in the string, it'd be n n. I think the rest was straightforward. The gist of the program is that it stores the digits of the initial number and the running sum in a row matrix. The next program I might want to write could be one to calculate pi to an arbitrary number of digits. I already wrote that for the TI89, but lost it when I changed batteries.
%%HP: T(3)A(R)F(.);
\<< 0 0 0 0 \-> D S N J M
\<< 10 D 1 - ^ 1 + 10 D ^ 1 -
FOR N N \->STR 'S' STO 1 D
FOR J S J J SUB OBJ\->
NEXT D ROW\-> 'M' STO
DO 2 D
FOR J M J GET
NEXT M 1 GET 2 D
FOR J M J GET +
NEXT D ROW\-> 'M' STO
UNTIL M D GET N \>=
END M D GET N ==
IF
THEN N
END
NEXT
\>>
\>>
"Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon
Last Edited By: Crawl and 1OOO
09/07/09 11:22 PM.
Edited 2 times.
|
|||||||||
Crawl and 1OOO |
|||||||||
|
Going back through this thread
No idea what I meant here. Of course the 50g reduces sin(pi/3) to sqrt(3)/2. Maybe there are situations in which is doesn't do it automatically (sometimes you have to hit "EVAL") but of course it can do it. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
The HP50g apparently uses a fairly generic USB cable. I can use the USB cable for my older MP3 player with it to transfer files. I don't think the same
is true for the TI89.
I'll probably need to post more programs here with notes so I can keep progressing in RPL. It seems, however, necessary to save RPL programs in binary format to upload them to EMU48 (a graphing calculator emulator). This is a pretty simple program that displays a continually increasing counter until you hit a button. In QBASIC, I frequently use DO a$=INKEY$ LOOP WHILE a$ ="" loops to wait for key presses. I'm not quite sure this is as good a solution in RPL. For one thing, in qbasic, when the loop breaks, a$ has the value of the button pressed. On the other hand, in RPL, a number is on the stack that corresponds to the pushed key, but it's less normal. Like, if you hit 1. In qbasic, a$="1". In RPL, you hit 1 and what's left on the stack is "92." (but as a number, not a string). Hit 2, and it's "93.". This is actually the Cartesian coordinate code for the button. %%HP: T(3)A(R)F(.); \<< 1 DO DUP 1 DISP 1 + UNTIL KEY END \>> "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
This might be a little closer to what I'm trying to do. In this case, the counter only increments when you hit a button. Hitting 2 ends the program. One
nice thing about the HP50g versus qbasic, though, is that hitting the ON button also ends the program. Sometimes if you forget to put a way out of a loop in
qbasic, you have no choice but to close it all down (even though CTRL-Break is theoretically supposed to work)
%%HP: T(3)A(R)F(.);
\<< 1
DO DUP 1 DISP 1 +
DO
UNTIL KEY
END
UNTIL 93. ==
END
\>>
I think it's possible that this was the quartic solution I had for the HP49g+. All it is is the solution typed out directly. I might have not wanted to post it earlier because I wanted to make it more "robust" or something (sometimes the calculator has problems evaluating this (if that's the case, maybe removing the final EVAL could be an improvement) ), but whatever. Here it is. %%HP: T(3)A(R)F(.); \<< 0 0 \-> A4 A3 A2 A1 A0 U V \<< 'A2^2-3*A3*A1+12*A4*A0' EVAL 'U' STO '2*A2^3-9*A3*A2*A1+27*A4*A1^2+(27*A3^2-72*A4*A2)*A0' EVAL 'V' STO '-(A3/(4*A4))-1/2*\v/(A3^2/(4*A4^2)-2*A2/(3*A4)+2^(1/3)*U/(3*A4*(V+\v/(-4*U^3+V^2))^(1/3))+(V+\v/(-4*U^3+V^2))^(1/3)/(3*2^(1/3)*A4))-1/2*\v/(A3^2/(2*A4^2)-4*A2/(3*A4)-2^(1/3)*U/(3*A4*(V+\v/(-4*U^3+V^2))^(1/3))-(V+\v/(-4*U^3+V^2))^(1/3)/(3*2^(1/3)*A4)-(-(A3^3/A4^3)+4*A2*A3/A4^2-8*(A1/A4))/(4*\v/(A3^2/(4*A4^2)-2*A2/(3*A4)+2^(1/3)*U/(3*A4*(V+\v/(-4*U^3+V^2))^(1/3))+(V+\v/(-4*U^3+V^2))^(1/3)/(3*2^(1/3)*A4))))' EVAL \>> \>> "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
This program calculates pi to an arbitrary number of digits.
%%HP: T(3)A(R)F(.);
\<< -1 -1 0 0 0 0 0 0 0 \-> C1 C2 N1 N2 S1 S2 S Q D
\<<
DO D 1 + 'D' STO 0 'S1' STO 0 'S2' STO
DO 1 'C1' STO+ '(-1)^C1*16/(5^(2*C1+1)*(2*C1+1))' EVAL 'N1' STO N1 'S1' STO+
UNTIL N1 \->NUM ABS 10. D \->NUM NEG ^ <
END
DO 1 'C2' STO+ '(-1)^C2*4/(239^(2*C2+1)*(2*C2+1))' EVAL 'N2' STO N2 'S2' STO+
UNTIL N2 \->NUM ABS 10. D \->NUM NEG ^ <
END S S1 + S2 - EVAL 'S' STO S \->NUM 10. D \->NUM 1 - ^ * IP \->Q 'Q' STO Q 1 DISP S Q 10 D 1 - ^ / - EVAL 'S' STO
DO
UNTIL KEY
END
UNTIL 93. ==
END
\>>
\>>
It displays each digit, then when you press a button, it calculates the next digit. Pressing "2" ends the program. It could be made faster, I guess. One thing about that is that I believe each term can add more than one decimal digit to pi. However, the program is set up to display at most one new decimal digit for each term added. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
Pretty great how the code overlaps the ad.
"Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
I think I might try going back to the super-long floating number program. But this time on the HP50. And just in [user] RPL. You may recall that there
already was a long floating package for the 49 series (probably at hpcalc.org), and it was much more ambitious than what I plan to do (it had all kinds of
functions, including Gamma), and it was programmed in ... I dunno, actually. I don't know if it was C or SysRPL. I guess that made it run faster. But
it's also more of an investment, since you need to install it as a library, as opposed to just putting some files on your calculator. And the big downside
was, it crashed every time I used it in a long program.
I also briefly tried writing such a thing in basic, but the computer I was writing it on crashed. Also, it should be a lot easier on the HP50 anyway, because I can take advantage of the system's built-in infinite-precision integer arithmetic. (I guess that means I also could do it on a TI89; it just strikes me as being easier on the HP50). |
|||||||||
Crawl and 1OOO |
|||||||||
|
I guess I tend to be a nostalgic person anyway, but for some reason programming on a calculator takes me back to being a kid and using BASIC on a Commodore 64
or Apple II.
Anyway, I think the hardest thing about the long arithmetic (I'm still working on the addition routine) will be division (or just taking a reciprocal). The calculator can add, subtract and multiply integers exactly. No such luck with division (well, it can divide them exactly, but it gives fractions as a result). So, I'll need to use a division algorithm rather than using one the calculator already has. I could use the calculator's approximate division to simulate long division, getting about 6 digits with each step. However, it is also possible to divide using only addition and multiplication, which is probably a better approach. |
|||||||||
Crawl and 1OOO |
|||||||||
|
Wow, I think I might have finished the addition routine, and that it might work for subtraction, too. I might want to do more checking, but it seems to have
worked for several of the examples I've tried.
First, the calculator assumes you have an integer stored in the directory under the variable name DIG which holds the number of digits you want to use. I guess it could theoretically be possible to also let the user set the number of digits in the exponent, but for now I have it set to three. This program is ONE. It loads the number 1 on the stack in my long format.
%%HP: T(3)A(R)F(.);
\<< 0 \-> J
\<< "+1" 1 DIG 1 -
FOR J "0" +
NEXT "E+000" +
\>>
\>>
So, the number comes out like "+100000E+000". I think the scientific notation I use is a lot more intuitive than the other library. In mine, the decimal is right after the first number, just like in regular scientific notation. So, one hundred would be "+100000E+002". In the other one, the decimal was at the end of the mantissa, so you had to know how many digits you were using to convert to scientific. Very confusing. So, for that, one would be (I think) something like "+10000w-A005" ZERO loads zero.
%%HP: T(3)A(R)F(.);
\<< 0 \-> J
\<< "+" 1 DIG
FOR J "0" +
NEXT "E+000" +
\>>
\>>
INF loads the largest positive number this format can handle.
%%HP: T(3)A(R)F(.);
\<< 0 \-> J
\<< "+" 1 DIG
FOR J "9" +
NEXT "E+999" +
\>>
\>>
NGTV changes the sign of a number in this format. %%HP: T(3)A(R)F(.); \<< DUP 2 DIG 2 + 4 + SUB SWAP 1 1 SUB "1" + OBJ\-> 1 == IF THEN "-" ELSE "+" END SWAP + \>> And this is the monster that adds. All those other programs need to be saved for this one to work (well, maybe not ONE, but that at least gives you some numbers TO add).
%%HP: T(3)A(R)F(.);
\<< 0 0 \-> A B AX BX
\<< A DIG 3 + DUP 3 + SUB OBJ\-> 'AX' STO B DIG 3 + DUP 3 + SUB OBJ\-> 'BX' STO AX BX - ABS DIG >
IF
THEN AX BX >
IF
THEN A
ELSE B
END KILL
END A 1 DIG 1 + SUB OBJ\-> B 1 DIG 1 + SUB OBJ\-> AX BX - DUP 0 \>=
IF
THEN 10 SWAP ^ ROT *
ELSE NEG 10 SWAP ^ *
END DUP SIZE UNROT + DUP SIZE UNROT DUP DUP 0 ==
IF
THEN DROP DROP DROP DROP ZERO KILL
END 0 >
IF
THEN "+" SWAP \->STR +
ELSE \->STR
END UNROT - \->Q AX BX MAX + DUP 0 \>=
IF
THEN "E+"
ELSE "E-"
END SWAP ABS DUP \->STR SWAP SIZE DUP 1 \<=
IF
THEN DROP "00" SWAP +
ELSE 2 ==
IF
THEN "0" SWAP +
END
END DUP2 OBJ\-> SWAP "1" + 2 3 SUB OBJ\-> * DUP 999 > SWAP -999 < DUP UNROT OR
IF
THEN
IF
THEN ZERO KILL
ELSE DROP DROP INF SWAP 1 1 SUB "1" + OBJ\-> -1 ==
IF
THEN NGTV
END KILL
END
ELSE DROP
END + SWAP 1 DIG 1 + SUB SWAP +
\>>
\>>
I will need to annotate that program later. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon
Last Edited By: Crawl and 1OOO
09/23/09 12:29 PM.
Edited 1 times.
|
|||||||||
Crawl and 1OOO |
|||||||||
"Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon
Last Edited By: Crawl and 1OOO
09/23/09 1:35 PM.
Edited 1 times.
|
|||||||||
Crawl and 1OOO |
|||||||||
|
Well, that didn't work right.
Anyway, I found another bug. Adding zero to a small number (with exponent less than negative DIG) makes the sum zero. The problem can probably be fixed by changing the ZERO function.
%%HP: T(3)A(R)F(.);
\<< 0 \-> J
\<< "+" 1 DIG
FOR J "0" +
NEXT "E-999" +
\>>
\>>
So zero is defined to have an exponent of -999 (rather than 0). "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
Crap. All those KILL commands are wrong. They end ALL program execution, not just the program that calls them. So, if you'd try to use that addition
program in another program, when it hits a KILL command, everything stops.
I haven't found the correct command yet. I hope this calculator does have some way to quit a subroutine! "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
I hope this calculator does have some way to quit a subroutine! I'll bet that it doesn't. I remember being shocked that RPL does not have a GOTO command, although I eventually resigned myself to that. I think the only thing to do would be to make the entire program that follows the KILL statement part of an IF..THEN structure. It seems like an awkward thing to do, especially if they nest (but then I again I still like using GOTO). I think the basics of the program work. I also had a multiplication program. I realized one infinite series I could try that involves addition and multiplication but does not require division is the geometric series x + x^2 + x^3 + ... =x/(1-x) I tried both x=0.5 and -0.5, and the right answer (aside from truncation errors) were given in both cases. |
|||||||||
Crawl and 1OOO |
|||||||||
|
Okay, I fixed that, and some other bugs besides.
Here is the new AD program.
%%HP: T(3)A(R)F(.);
\<< 0 0 \-> A B AX BX
\<< A DIG 3 + DUP 3 + SUB OBJ\-> 'AX' STO B DIG 3 + DUP 3 + SUB OBJ\-> 'BX' STO AX BX - ABS DIG >
IF
THEN AX BX >
IF
THEN A
ELSE B
END
ELSE A 1 DIG 1 + SUB OBJ\-> B 1 DIG 1 + SUB OBJ\-> AX BX - DUP 0 \>=
IF
THEN 10 SWAP ^ ROT *
ELSE NEG 10 SWAP ^ *
END DUP SIZE UNROT + DUP SIZE UNROT DUP DUP 0 ==
IF
THEN DROP DROP DROP DROP ZERO
ELSE 0 >
IF
THEN "+" SWAP \->STR +
ELSE \->STR
END UNROT - \->Q AX BX MAX + DUP 0 \>=
IF
THEN "E+"
ELSE "E-"
END SWAP ABS DUP \->STR SWAP SIZE DUP 1 \<=
IF
THEN DROP "00" SWAP +
ELSE 2 ==
IF
THEN "0" SWAP +
END
END DUP2 OBJ\-> SWAP "1" + 2 3 SUB OBJ\-> * DUP 999 > SWAP -999 < DUP UNROT OR
IF
THEN
IF
THEN DROP DROP DROP ZERO
ELSE DROP DROP INF SWAP 1 1 SUB "1" + OBJ\-> -1 ==
IF
THEN NGTV
END
END
ELSE DROP + SWAP 1 DIG 1 + SUB SWAP +
END
END
END
\>>
\>>
Here is the MULT program. It might be a little bit simplier.
%%HP: T(3)A(R)F(.);
\<< 0 0 \-> A B AX BX
\<< A DIG 3 + DUP 3 + SUB OBJ\-> 'AX' STO B DIG 3 + DUP 3 + SUB OBJ\-> 'BX' STO A 1 DIG 1 + SUB OBJ\-> B 1 DIG 1 + SUB OBJ\-> * DUP SIZE \->Q SWAP DUP 0 ==
IF
THEN DROP DROP ZERO
ELSE DUP \->STR SWAP 0 \>=
IF
THEN "+" SWAP +
ELSE
END 1 DIG 1 + SUB SWAP DIG 2 * 1 - - AX BX + + DUP 0 \>=
IF
THEN "E+"
ELSE "E-"
END SWAP ABS \->STR DUP SIZE DUP 1 ==
IF
THEN DROP "00" SWAP + + +
ELSE DUP 2 ==
IF
THEN DROP "0" SWAP + + +
ELSE DUP 3 ==
IF
THEN DROP + +
ELSE DROP + TAIL OBJ\-> 0 <
IF
THEN DROP ZERO
ELSE INF SWAP OBJ\-> 0 <
IF
THEN NGTV
END
END
END
END
END
END
\>>
\>>
And this program works as a test for them, by summing a geometric series.
%%HP: T(3)A(R)F(.);
\<< 0 0 0 0 \-> P S X J
\<< "-5" 1 DIG 1 -
FOR J "0" +
NEXT "E-001" + 'X' STO ONE 'P' STO ZERO 'S' STO
DO P X MULT 'P' STO P S AD 'S' STO S 1 DISP
DO
UNTIL KEY
END
UNTIL 93. ==
END S
\>>
\>>
I earlier had to initialize P and S both to X, rather than 1 and 0, because before the fix, when it first tried added S to P, the KILL command in AD
killed the program.
Now I gotta do division. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
I think I'm getting jaded.
A normal scientific might not be enough to entertainment anymore. I was clothes shopping with FO, and when I was waiting for her to finish, I tried playing with my 35s, but not a lot of things to do with it occurred to me. I tried the algebraic mode, which seemed kind of awful. There was a slight reason to use the algebraic mode on the 33s, namely, that it was easier to use complex numbers in that mode. (In RPN mode, a single complex number took two stack positions, so you could only deal with two at a time) However, that was fixed with the 35s. So, I don't think there's a reason to use algebra mode anymore. Unfortunately, the parentheses command takes up an unshifted key. What I noticed about that mode is that it doesn't seem possible to show the exponent of a number if it slides off the display? In RPN mode, you can hit the right arrow to scroll it over, but in algebraic mode, that moves the cursor on the edit line. |
|||||||||
Crawl and 1OOO |
|||||||||
The HP50g apparently uses a fairly generic USB cable. I can use the USB cable for my older MP3 player with it to transfer files. I don't think the same is true for the TI89.It might be possible to use a different type of generic USB cable with a TI89, though. Apparently there are different kinds. One thing that's pretty cool about the 50g is that it can be powered through the USB. So, if you connect it to a computer, it draws power from the computer. In fact, there are A/C adapters available! So I guess you might never need to worry about running out of batteries. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
|
I got a new SD card for my 50g! It is "complete" again.
When I got a San Disk card a long time back, it didn't work -- it would seem to work, but in short order the files would all be corrupted. That doesn't seem to be a problem with this one. Maybe San Disk just sucks. I don't think I figured out how to use directories last time. I guess it's not a huge mystery -- you navigate through them with the cross pad (left-right) arrows. Even so, I don't know if this is explained in any document. For some reason, I thought you'd enter a directory with the ENTER button. If it wasn't possible to use directories, I don't know how useful a card would be. It's a gigabyte of data, which for calculator purposes is probably practically infinite, but can you imagine navigating a gigabyte's worth of files if they weren't organized into folders? And directory suppport isn't as good as it could be. I don't think it's possible to copy a file to a directory on the card through the calculator's file manager system. You can only copy to the card's root directory. So, to move things to directories, you'd either have to put the card into a computer and do it there, or move files to directories using the command line, which is sort of unintuitive. So, I'm working on the LONG floating point programs now. Let's say I wanted to move the AD program to a LONG directory on the card. I would have to recall the program to level 1 of the stack. Then I'd have to enter :3:"LONG/AD" onto the stack. Then STO. I guess that's not impossibly difficult, but you have to remember the colon command, and that 3 refers to the card. And if you'd have a long nest of subdirectories, it'd be harder. Anyway, I can take some good use of this. I might even store programming challenges on the card (interesting ones, anyway), since, again, there's pretty much infinite space there. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
Crawl and 1OOO |
|||||||||
Crawl and 1OOO wrote: I might have just made a typo in the earlier post. I think the HP might not be able to evaluate sin(pi/5), while maybe the TI can. But maybe a program to evaluate the roots of unit in algebraic terms could be something I could work on later. "Of the three ways in which men think that they acquire a knowledge of things--authority, reasoning, and experience--only the last is effective and able to bring peace to the intellect." -Roger Bacon |
|||||||||
| Affiliates Websites & Yuku/Ezboards
YUKU & EZBOARDS
WEBSITES
|