Do you remember what a prime number is? Any number that is evenly divisible only by the number 1 and itself. For example, 2, 3, 5, 7, 11, 13, 17, and 19 are all prime numbers.

### Prime Number Algorithm

- You have a positive number and you want to know if it is prime of not.
- Check the modulus of 2 on that number. If it is zero, the number is not prime unless the number
*is*2, then it is prime. - Continue to incrementally check every number from 2 to the number BEFORE the number you want to check. If any of those numbers have a modulus of zero, then the number is not prime.

I guess it becomes pretty obvious that other than the number 2, all prime numbers are odd and they are positive. Negative numbers cannot be prime numbers.

### Doing this in VBA

Dim longNumberToTest As longEnd longNumberToTest = 97 Dim boolNotPrime As Boolean boolNotPrime = False Dim i As Long Dim i2 As Long For i2 = 2 To i - 1 If i Mod i2 = 0 Then boolNotPrime = True End If Next i2 If boolNotPrime = False Then ... it is a prime number ... End If

Note that this procedure in VBA isn’t testing the number 1. That would cause an exception.

### Doing this in Perl

print &is_prime([some number]) ? 'Yes' : 'No'; sub is_prime { if($_[0] == 2) { return 1; } elsif($_[0] > 2) { if($_[0] % 2 == 0) { return 0; } else { for(my $c = 2; $c < $_[0]; $c++) { if($_[0] % $c == 0) { return 0; } } return 1; } } else { return 0; } }

Of course, I had to create a module for generating prime numbers for a personal project I was working on. It allows you to either generate a range of prime numbers or it will step forwards/backwards through prime numbers from a starting point.

You can run the test script here. | |

init_is_prime.lib | |

MongerPrime.pm | |

primetest.pl* | |

flexbox.css** | |

programming_blog.css** | |

Github Repository |

**NOTE: The Perl script downloads as a .txt file to keep your browser from pitchin’ a hissy. Just save it with a .pl extension.*

***NOTE: You will need the two CSS files for uploadtest.pl to display correctly. Change the path to them in that file.*

Now go, code, be happy. 🤓