(48) (49g) (50g) Program To Determine If A Number Is Prime Or Composite

12142020, 02:26 PM
Post: #1




(48) (49g) (50g) Program To Determine If A Number Is Prime Or Composite
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. 

12152020, 08:01 PM
Post: #2




RE: (48) (49g) (50g) Program To Determine If A Number Is Prime Or Composite
The 49g and 50g have builtin 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 HP48, where your program would be most useful. 

« Next Oldest  Next Newest »

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