Tuesday 8 May 2012

Is it prime - comments on the logic

In my previous post I described some logic that could be used to test whether a number is prime. The first bit of the psuedo code checks to see if the number entered, X, is greater than or equal to 372 = 1369. This is the range of this code but it can be altered easily to check numbers much larger than this. It depends on how much memory you have available in your fx-50F calculator. For example, by continuing the test division of X by eight more primes 37, 41, 43, 47, 53, 59, 61 and 67, it would be possible to test numbers up to but not including 712 = 5041.

The next part of the code checks to see if X is less than 11 and, if it is, checks to see if it is one of the primes 2, 3, 5, or 7. If it is Y is set to 1 otherwise Y is 0.

If X is greater than or equal to 11 we can divide X by each of the prime numbers 2, 3, 5, and 7 to see if it is divisible by any of these numbers. As described previously, we use the calculator's sine function for this purpose. Again, if X is divisible by one of these numbers, then Y is set to 0 (i.e. X is not prime) otherwise it is set to 1. If X is less than 121 (=112) then we need not test the number any further.

If X is greater than or equal to 121 we can also try dividing it by the prime numbers 11, 13, 17 and 19. Again, if X is divisible by one of these numbers, then Y is set to 0 (i.e. X is not prime) otherwise it is set to 1. If X is less than 529 (=232) then we need not test the number any further.

Effectively we are bootstraping our way up through the prime numbers and the process is repeated in the remaining steps. As such, it can be extended, within the limits of the calculator, to much larger numbers as described above.

No comments:

Post a Comment