The program works by using a combination of the sieve of Eratosthenes and trial division. After a bit of initialization, it begins by coping the number to variable "R". "P" is the current prime number and "M" is the multiplicity of the prime (P^M) that will be checked to determine if it can evenly divide "R". If it can, "M" (for multiplicity) is increased one and the test is repeated until it fails. Once it does, the actual division takes place to reduce "R" to a lesser number.
If "P" fails to evenly divide "R" with the multiplicity of one ("M" = 1), then "P" is increased by one and the whole test repeats till "R" is reduced to 1. If the division is successful, the prime number ("P") and its multiplicity ("M") is displayed. If the division failed, "P" is still increased by one to test the next number for division.
After many successive tests and divisions, "R" will eventually reduce to 0 and the program ends. The program also has a flag "D" that if it's less than two (it increases with every increase of "P" and/or "M") will conclude the program with the display that the number given to it is, in fact, prime.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.