Prime numbers will always have a special place in every mathematicians (or cryptographers) heart. The usefulness for the standard PowerShell user is questionable, but just for the fun of it I have created/translated a couple of functions for working with Primes.

The first function is called Get-PrimeNumbers and will, as the name suggests, output the first x number of primes (x being controlled by the Amount parameter). The function is not just looking up numbers in an already prepared list, but are calculating/finding the primes using one of three algorithms; the Standard method as well as the Sieve of Eratosthenes and the Sieve of Sundaram. You can choose between the three by using the Method parameter.

The second function is called Test-IsPrime and can be used to test if a number is a prime or not. This function will return either True of False depending on whether the input is a prime or not. It uses the Rabin-Miller primality test to check for primality.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s