Post Reply 
Made a new program for the HP 11C
01-07-2014, 10:06 PM
Post: #2
RE: Made a new program for the HP 11C
A couple of things you can do to significantly increase the speed of this program.

First, if a number is odd, you only need to test by odd numbers, so you can increment register 3 by two instead of one. Of course, this means that you have to treat 2 as a special case since it's even and prime.

Second, you only need to test up to the square root of the number, or at worst the next larger integer. Steps 003 and 004 can be replaced by SQRT ; 1 ; + . This won't make much difference for smaller numbers but is significant for larger numbers. For example, to test whether 1001 is prime, you only have to go to 32 instead of 500. Combined with the first optimization, this makes the program about thirty times faster. For 10001 it's about a hundred times faster.

Don't take the above as a criticism of your program. It's just that stuff like this becomes second nature by the time you've written your third prime number test. I think I wrote my first one forty years ago...
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
Made a new program for the HP 11C - vma - 01-07-2014, 09:21 PM
RE: Made a new program for the HP 11C - kakima - 01-07-2014 10:06 PM



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