The Museum of HP Calculators


Prime number check for the HP-15C

This program is by Martin Ask and is used here by permission.

This program is supplied without representation or warranty of any kind. Martin Ask and The Museum of HP Calculators therefore assume no responsibility and shall have no liability, consequential or otherwise, of any kind arising from the use of this program material or any part thereof.

Overview

This is a small program to check if a number is a prime or not.

It might not be the most efficient or comprehensive one, but its small size makes up for that (in my opinion, at least :).

If you want a more complete (and seven times larger) program there's an excellent one for the HP-67 in this library that should be easily portable.

Usage:

Enter the number you want to test in the X register and start the program.

If the number returned is the same as the number entered, it's a prime.

Otherwise the number it's divisible by will flash in the display. The resulting quotient will be placed in the Y register.

Note: The number 2 is an exception. It will incorrectly be recognized as a non-prime. I didn't think it was worth the memory space to sort out this "bug". Everyone knows 2 is the only even prime, right? :)

It should also be noted that this program could be way more efficient by using primes as divisors instead of *all* odd numbers sequentially (and 2 initially).

Again, this would use up a lot of memory and my goal was to make as small a program as possible and still maintain fairly good efficiency.

Also, as one of the major drawbacks of the HP-15c is its slow speed, large numbers will take time. E.g. the prime 15,485,863 takes about half an hour to complete...

Registers used:	  R0: Test value.
                  R1: Incrementing divisor.
                  R2: DSE register. Only real purpose is to change divisor increment from "1" to "2" after first loop.
                  R3: Sqrt of test value. Reference value for ending loop.

Listing

Line	Instr.	  Display


000		
001	LBL E     42,21,15
002	STO 0	  44 0
003	SQRT	  11
004	STO 3	  44 3
005	2	  2
006	STO 1	  44 1
007	STO 2	  44 2
008	LBL 0	  42,21,0
009	RCL 0	  45 0
010	RCL 1	  45 1
011	/	  10
012	ENTER	  36
013	INT	  43 44
014	x=y?	  43,30,5
015	GTO 2	  22 2
016	RCL 3	  45 3
017	x>y?	  43,30,7
018	GTO 1	  22 1
019	DSE 2	  42,5,2
020	GTO 3	  22 3
021	2	  2
022	LBL 4	  42,21,4
023	STO + 1	  44,40,1
024	GTO 0	  22 0
025	LBL 3	  42,21,3
026	1	  1
027	GTO 4	  22 4
028	LBL 2	  42,21,2
029	RCL 1	  45 1
030	SF 9	  43,4,9
031	RTN	  43 32
032	LBL 1	  42,21,1
033	RCL 0	  45 0
034	RTN	  43 32

Go back to the software library
Go back to the main exhibit hall