Behind the PowerShell Pipeline logo

Behind the PowerShell Pipeline

Subscribe
Archives
June 24, 2025

A Prime Scripting Solution

Last month, I left you with what I hope was a fun PowerShell scripting challenge. My goal for the challenge was to provide you with an opportunity to utilize a few common PowerShell scripting constructs. The challenges revolved around prime numbers.

  • Using PowerShell, calculate the first 50 prime numbers.
  • Using the list of prime numbers, create another list of the differences between each prime number and the next.
  • Display the sum of each pair of prime numbers.
  • Get the sum of the first and last prime numbers in your list, then the second and second to last, etc.

You can use the list of prime numbers at https://en.wikipedia.org/wiki/List_of_prime_numbers to verify your results.

How did you do?

Test-IsPrime

The first thing I did was to write a simple function to test if a number is prime. I wanted to use this function to generate a list of prime numbers. The function takes an integer greater than or equal to 2 and returns $true if the number is prime and $false otherwise.

function Test-IsPrime {
    [CmdletBinding()]
    [Alias('tip')]
    [OutputType([bool])]
    param (
        [Parameter(
            Mandatory,
            ValueFromPipeline,
            HelpMessage = 'Enter an integer greater than 2 to test if it is prime'
        )]
        [ValidateScript({ $_ -ge 2 }, ErrorMessage = 'Expected a number greater than or equal to 2')]
        [int]$Number
    )
    Process {
        for ($i = 2; $i -le [math]::Sqrt($number); $i++) {
            if ($number % $i -eq 0) {
                return $false
            }
        }
        $true
    }
}

The function attempts to divide the number by all integers from 2 up to the square root of the number. If any division results in a remainder of zero, the function returns $false, indicating that the number is not prime. If no such divisor is found, it returns $true. I shouldn't have to test dividing by a number beyond the square root of the number.

The function accepts pipeline input so that I can use it like this:

PS C:\> 2..10 | where {tip $_}
2
3
5
7

Building the List of Primes

Armed with this function, I can create a list of the first 50 prime numbers. I can use a simple loop to do this, starting with the first prime number, 2, and continuing until I have 50 primes in the array.

$count = 50
$i = 2
$primes = @()
do {
    if (Test-IsPrime -Number $i) {
        $primes += $i
    }
    $i++
} Until ($primes.Count -ge $count)

As an alternative to the array, you could also use a generic list.

$count = 50
$i = 2
$primes = [System.Collections.Generic.List[int]]::new()
do {
    if (Test-IsPrime -Number $i) {
        $primes.Add($i)
    }
    $i++
} Until ($primes.Count -ge $count)

This is slightly more efficient than using an array. The results are the same.

PS C:\> $primes.count
50
PS C:\> $primes -join ","
2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229

Prime Differences

To calculate the differences between each prime number and the next, I can use a simple loop that iterates through the list of primes and calculates the difference between each pair of consecutive primes.

$differences = @()
for ($k = 1; $k -lt $primes.Count; $k++) {
    $differences += $primes[$k] - $primes[$k - 1]
}

I am looping through the list of primes and referencing the current and previous prime numbers to calculate the difference using the index number in brackets. Because arrays are zero-based, I start at one and go to the end of the array. The result is a list of differences between each prime number and the next.

PS C:\> $differences -join ","
1,2,2,4,2,4,2,4,6,2,6,4,2,4,6,6,2,6,4,2,6,4,6,8,4,2,4,2,4,14,4,6,2,10,2,6,6,4,6,6,2,10,2,4,2,12,12,4,2
Want to read the full issue?
GitHub Bluesky LinkedIn About Jeff
Powered by Buttondown, the easiest way to start and grow your newsletter.