12-14-2020, 02:26 PM
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.
<< -> 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.