Program question - Printable Version +- HP Forums (https://archived.hpcalc.org/museumforum) +-- Forum: HP Museum Forums (https://archived.hpcalc.org/museumforum/forum-1.html) +--- Forum: Old HP Forum Archives (https://archived.hpcalc.org/museumforum/forum-2.html) +--- Thread: Program question (/thread-119670.html) |
Program question - Vincze - 07-30-2007 I have very enjoyable weekend playing with new 35s. Based on one post on Friday about square root algorithm, I sat down and wrote program on 35s to calculate the square root. I had some problem with it, but it generally work ok. I also write program in C to do, and it much quicker that calculator, but that understandable. In C, it very fast for some numbers, but when number get over 20 digit long, it take long time to calculate. I know we have sqrt function on calculator, but anyone know of way to write optimized program on 35s? I not very good with programing calculators, but I be interested in seeing how wiser person write.
Again, please excuse poor English mine.
Re: Program question - Gerson W. Barbosa - 07-30-2007 Hello Vincze, This is not optimized for the HP-35s (I haven't read the manual yet). As an exercise, I tried to use only the stack, although I am not sure if this will make to program run any faster on the 35s. It could be shorter, I think. This is the same algorithm in the TurboBCD program below, except it doesn't handle sqrt(0). The initial guess should be improve (the program will take longer to run for large arguments).
S001 LBL S I am implementing some functions in TurboBCD. The square root function will be handy for inverse trigs. Regards, Gerson. -----------------------------------------------------------------
Edited: 30 July 2007, 10:35 p.m.
Re: Program question - Vincze - 07-31-2007 What key is Rv? I no see on my calculator.
Re: Program question - Don Shepherd - 07-31-2007 Roll the stack down.
Re: Program question - Vincze - 07-31-2007 Thank you. I assume that be key with R and down arrow on? One other question. What is STOP key?
Re: Program question - Thomas Radtke - 07-31-2007 R/S (Run/Stop)
Re: Program question - Vincze - 07-31-2007 Gerson, I try your program, but it no work. Maybe I not use program correct. I clear stack, and put 9 in X, but I get divide by 0 error.
Re: Program question - Gerson W. Barbosa - 07-31-2007 Hi Vincze, Please check your listing:
K001 LBL K
Please notice x<>y ‚ x<>y? Regards, Gerson. P.S.: I changed the label to K to match the square-root symbol.
Checksum: C435 ( yellow-shift MEM 2 yellow-shift SHOW )
Edited: 31 July 2007, 12:33 p.m.
Re: Program question - Vincze - 07-31-2007 Thank you Gerson. I see I did have error in entry. I fix and try and it now work. It seem slow. I enter 9999999999999 and it take about 8 seconds. I wonder how HP wrote algorithm for assigned sqrt key? I always wonder how a key could be faster than program can be. Granted, it must be written in machine language, so should be faster, but I wonder if anyone have knowledge of how calculator company does this.
Re: Program question - Gerson W. Barbosa - 07-31-2007
Quote: This takes 4.5 seconds in mine. Perhaps you've missed one 9 here. It all depends on the initial guess. Change line K002 to 3E-7 and it will take 1.2 seconds. On the other hand, it will take 4.5 second for 9.
Quote: Well, not exactly from a calculator company (MS in MSX stands for you know what).
Re: Program question - Thomas Klemm - 07-31-2007 Instead of calculating the square root of a number d the inverse of the square root may be calculated followed by the multiplication of d:
1 Use an initial value:
x0 ~ 1 / sqrt(d)
and calculate x1, x2, x3, ...
1 - d * xi2 This method has the advantage that only a division by 2 is needed.
Here's an HP-11c program: 01 LBL A 14 * I guess it's easy to convert that into a program for the HP-35s.
Now let's suppose we want to calculate sqrt(5) only with paper
1.00000Credits to Newton for this marvelous algorithm.
Edited: 31 July 2007, 4:09 p.m.
Re: Program question - Vincze - 07-31-2007 Quote: I do like Newton's method, but I believe the Babylonian method to be better since it considered a quadratically convergent algorithm meaning that with each iteration, it get closer to final result. On a side note, I do some research and see that most pocket calculator use routine to compute exponential function and the natural log in for of sqrt = e^.5lnS where S is the number you are trying to find sqrt of (I believe). I try this, but I have not had luck with yet on 35s.
That is it. I try with large number (63 9's) and it calculate instantly. I should have thought of this before with my experience with slide rule. That how we could find sqrt on slide rule (although good slide rule have ability to square and show square root too). Edited: 31 July 2007, 5:10 p.m. after one or more responses were posted
Re: Program question - Thomas Klemm - 07-31-2007
Quote:
Both algorithms converge quadratically with a good initial choice.
Quote:
Well, that's not exactly the definition of Re: Program question - Vincze - 07-31-2007 Good evening Thomas. Yes you right that both quadratically convergent. I think that yours harder to do in head if wanting, but we not talking about doing in head, but in basic calculator program, so you have valid point. My apologies.
Please excuse my poor english.
Re: Program question - Gerson W. Barbosa - 08-01-2007 Even shorter and faster:
S001 LBL S Just kidding :-) Klemm's idea and mine was to present a numeric method for the square root function. Using logarithm to compute the square root is slower than computing it through those methods, so computer languages and calculator might use the latter, even though they have built-in logs. Here is an improved version of the first algorithm, slightly slower but shorter despite handling zero:
L001 LBL L People who've read up the manual might come up with an even shorter routine :-)
Edited: 1 Aug 2007, 12:20 a.m.
Square root digit-by-digit - Nenad (Croatia) - 08-01-2007 This is something I remember from elementary school, i.e. how we calculated the square root digit-by-digit, implementing pen-and pencil methods. It goes something like this:
In each step you calculate a single digit x, by guessing it so that the present remainder added the two digits of the original number, divided by the doubled current result with the digit x, written at its end times x subtracted from the current remainder gives the new remainder... OK, I admit that I have just got lost in this explanation, but hopefully the shown example may shed some more light. Maybe, someone could write an RPN program to do this.
Until then I prefer the Gerson's solution for my faithful HP67: Re: Square root digit-by-digit - Gerson W. Barbosa - 08-01-2007 Hello Nenad,
Quote: That's the way we computed square roots at school too. I remember once I forgot the algorithm when I needed it but I did remember the iterative method. I couldn't help remembering Isaac Asimov's short story "A Feeling of Power"...
Quote: Actually, I prefer just to press the SQRT(x) key, especially when it is a primary key as on the HP-35s. There are some side effects though... Read Asimov's short story :-) Best regards, Gerson.
Re: Square root digit-by-digit - Vincze - 08-01-2007 Good morning Gerson. The story you talk about is very important to mankind. I remember reading long time ago, and thinking it not possible that man forget how to do math. But when you see young people struggle with math, and even simple math like multiplication, it do seem very real, and very scary. This why I have fear of my son using calculator in high school. Calculator ok for checking, but I think too many child become calculator dependent that they know not how to think properly. I know I am not smartest person in world, but I also know that skills that I have need to be used to maintain them and calculator and computer only tool to help do math faster. I believe it is so important that child know math and not rely on calculator. In Hungary, we no allowed to use calculator when I was in school. Has this changed since, I do not know. I need to write my brother and see as he have children my son age there. In fact, when I in university, slide rule was even frowned upon as a crutch, but much needed crutch for more advanced math. Main point though, all students at university understood concept of doing math with pencil and paper first.
Anyhow friend, thank you for reminding us of that short story. It is something all should read.
Re: Square root digit-by-digit - Frank Rottgardt - 08-02-2007 Once the rifle and matches became common our hunting ancestors couldn´t imagine how huamnity will survive without knowing the basics of archery and making fire. The most important part of hunting is a) to find the deer b) to sneak up and finally c) to kill it. Today only c) has changed. Skills a) and b) are still something each hunter need to know how.
Back to math: a) and b) is the same as for 100 years ago, only c) changed: calculator or PC. As long as our kids are familiar with a+b I can´t see the end of the world. If your calculator should be broken, let´s fetch a second one from the 2-3 each one has around. Or go to the next shop and buy a new one for 20 bugs or borrow one from your neighbour.
But I do agree in one point: Re: Square root digit-by-digit - Thomas Klemm - 08-02-2007
Quote:
Just wanted to point out that by actually calculating the quotient in the last step some more correct digits may be obtained: sqrt(24378) = 156.134557353... Edited: 2 Aug 2007, 11:04 a.m.
|