Post Reply 
Prime number finder HP11C?
06-16-2016, 10:22 PM
Post: #7
RE: Prime number finder HP11C?
.
Hi, jbhp55:

(06-16-2016 07:56 PM)jbhp55 Wrote:  I don't see it, please enlighten me!

Ok, here we go. As I said, a trivial technique will shorten your program by 2 steps.

The crux of the matter has to do with code relocation.

Your program begins with 01 LBL C, then enters a loop at 05 LBL 7 to try each potential divisor in turn (implicitly up to the square root of the number). If one is found the number can't be prime so it branches to 20 LBL 8 which generates the next odd number to be tested for primality and then it simply branches back to 01 LBL C to repeat the whole procedure with the new number.

If the loop at 04 LBL 7 exhausts all potential divisors without finding one, the number is prime so it's recalled to the display and execution stops at 19 R/S, so once the user takes note and presses [R/S] to find the next prime number the program simply falls through the next step, which is 20 LBL 8, to generate the next odd number and proceed as explained before.

So far so good.

Now, to optimize the length of the program the key idea is this: there's no need for the main entry point LBL C to be at step 01 because when you start your program by either pressing [C] in USER mode or else [GSB][C] in any mode, the HP-11C will automatically search through the code for LBL C and once found it will immediately start execution there, whether LBL C is located at step 01 or at step 05, say.

With this in mind, we now rearrange the code, essentially moving before LBL C the instructions from LBL 8 to the end of the code, like this:

01 LBL 8
02 RCL 1
03 2
04 +
05 LBL C
06 STO 1
07 1
08 STO 2
09 LBL 7
10 2
11 STO + 2
12 RCL 1
13 RCL 2
14 /
15 FRAC
16 x=0
17 GTO 8
18 LST X
19 RCL 2
20 x<= y
21 GTO 7
22 RCL 1

where the original GTO C instruction has been deleted, as 05 LBL C is now the very next instruction after LBL 8, RCL 1, 2, +, and the original R/S instruction has also been deleted after 22 RCL 1 because the end of program memory acts like an implicit stop/return instruction and the program will thus stop there by itself, leaving the program counter at the top of program memory (step 000), so once the user takes note of the newly found prime and presses [R/S] the execution will continue from step 01 which is LBL 8, exactly as it did in your original code.

This way the rearranged code works exactly like your original code did before but is only 22 steps long instead of 24.

Best regards.
V.
.

  
All My Articles & other Materials here:  Valentin Albillo's HP Collection
 
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Prime number finder HP11C? - jbhp55 - 06-13-2016, 07:15 PM
RE: Prime number finder HP11C? - jbhp55 - 06-14-2016, 06:33 AM
RE: Prime number finder HP11C? - TASP - 06-14-2016, 11:35 PM
RE: Prime number finder HP11C? - jbhp55 - 06-16-2016, 07:56 PM
RE: Prime number finder HP11C? - Valentin Albillo - 06-16-2016 10:22 PM
RE: Prime number finder HP11C? - jbhp55 - 06-17-2016, 07:08 AM
RE: Prime number finder HP11C? - Dieter - 06-17-2016, 06:42 PM
RE: Prime number finder HP11C? - Marcio - 06-27-2016, 10:17 PM
RE: Prime number finder HP11C? - jbhp55 - 06-17-2016, 10:43 AM
RE: Prime number finder HP11C? - rprosperi - 06-18-2016, 10:27 PM
RE: Prime number finder HP11C? - bshoring - 06-18-2016, 07:23 PM
RE: Prime number finder HP11C? - jbhp55 - 06-19-2016, 01:14 PM
RE: Prime number finder HP11C? - rprosperi - 06-19-2016, 08:03 PM
RE: Prime number finder HP11C? - jbhp55 - 06-19-2016, 01:17 PM
RE: Prime number finder HP11C? - jbhp55 - 06-19-2016, 05:35 PM
RE: Prime number finder HP11C? - rprosperi - 06-19-2016, 07:25 PM
RE: Prime number finder HP11C? - rprosperi - 06-20-2016, 12:05 AM
RE: Prime number finder HP11C? - jbhp55 - 06-19-2016, 09:28 PM
RE: Prime number finder HP11C? - rprosperi - 06-20-2016, 12:20 AM



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