HP Forums

Full Version: (48) (49g) (50g) Program To Determine If A Number Is Prime Or Composite
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
A whole number is composite or not a prime number if there exists at least one smaller number whose square is less than or equal to the former and evenly divides the former. The square criterion limits the number of factors needed to be searched.

<< -> N << 0 'NF' STO 2 'T' STO WHILE T 2 ^ N ABS <= REPEAT N T MOD 0 == IF THEN NF 1 + 'NF' STO T "FACTOR" ->TAG END T 1 + 'T' STO END NF 0 == IF THEN N "PRIME" ->TAG ELSE NF "NFACTORS<√N" ->TAG N "COMPOSITE" ->TAG END >> >>

The program lists all the factors (if any) of the number being tested up to or less than the square root of the number, the count of such factors found and finally an indication of whether the number is prime or composite.
The 49g and 50g have built-in commands FACTOR and ISPRIME? which give similar results, albeit without the explanatory tags. There is also the Factor library which is faster and returns a factor list in a format which is generally more useful in programs.

None of the above applies to the HP-48, where your program would be most useful.
Reference URL's