r/PowerShell • u/StartAutomating • Jul 14 '26
Script Sharing The Power of Primes
Prime numbers are pretty powerful.
That's why I just released a new PowerShell module based off of an old mathematical concept: PrimeTime.
PrimeTime uses prime numbers as time intervals.
Let's learn how this helps
Prime Number Primer
Prime Numbers can only be divided by themselves and one.
This makes primes pretty rare.
Prime numbers are particularly useful in programming, but it's not always obvious why or how.
A lot of people might vaguely point towards cryptography as the prime real estate for prime utility.
The thing of it is, if you're writing your own cryptography, you're probably doing it wrong.
Let's talk about a more practical application of primes.
The Cicada Principle
In North America there is a curious critter known as the periodical cicaca.
For the vast majority of their long lifespans, they live underground.
Once every N years, they surface in mass to start the next generation.
That N is a prime.
Why?
Cicadas come out en masse so that there are too many of them to eat.
Millions of little critters have to have a perfectly timed multi-year internal clock in order to make this work.
If two cicadas of different intervals produced offspring, their children might have a messed up internal clock, and come out of the ground at the worst time.
So there's an evolutionary advantage to cicadas coming out in large batches, as long as another cicade brood isn't doing the same thing at the same time.
Which brings us back to primes.
Primes are relatively rare.
So are products of primes (at least past the first few)
Let's take two primes as an example.
Imagine one brood of cicadas came out every 11 years, and another brood came out every 13 years.
We can find out how long it will take for these two broods to come out at the same time by simply multiplying the primes.
11 * 13 -eq 143So, with just two relatively low primes, we have an overlap every 143 years.
This is how primes are most useful to programming: they rarely overlap.
Sieve of Eratosthenes
This has been known for much longer than computers have existed.
Imagine we wanted to find prime numbers quickly.
We can do this by constructing a sieve that filters out any non-prime number.
This is called the Sieve of Eratosthenes
Once we know 2 is prime, we know every other even number is not prime.
Once we know 3 is prime, we know every third number is not prime.
To quickly get prime numbers up to a point, we can use this little PowerShell filter
# Calculate primes reasonably quickly with the Sieve of Eratosthenes
# Pipe in any positive whole number to see if it is prime.
filter prime {
$in = $_
if ($in -isnot [int]) { return }
if ($in -eq 1) { return $in }
if ($in -lt 1) { return}
if (-not $script:PrimeSieve) {
$script:PrimeSieve = [Collections.Queue]::new()
$script:PrimeSieve.Enqueue(2)
}
if ($script:PrimeSieve -contains $in) { return $in}
foreach ($n in $script:PrimeSieve) {
if (($n * 2) -gt $in) { break }
if (-not ($in % $n)) { return }
}
$script:PrimeSieve.Enqueue($in)
$in
}Prime Animations
Imagine we want a vibrant page. We want things to keep changing yet feel unpredictable. All we need to do is use different prime intervals.
The PrimeTime logo animates eight primes:
7 * 11 * 13 * 17 * 19 * 23 * 29 * 31The logo will repeat every 6685349671 seconds, or almost 212 years.
The PrimeTime page background uses 56 primes.
This background will repeat every 8.84753141993573E+116 seconds.
That's exponential notation.
This is a mind-boggling large number (so large it overflows the .NET [TimeSpan]).
Turn that interval into years and it's still mind-boggling.
The page background will repeat every 100 billion years
Performance and Scheduling
Imagine we want to design a system that's constantly checking for problems.
We want the system to know about problems as soon as we can, but nobody's exactly sure how often they need to check for something.
If we go around and ask our colleagues "how often should we can scan for this?", the response if often a shrug 🤷.
Often, people will pick an arbitrary number that seems reasonable. Let's say every 5 minutes, 10, or 15 minutes.
Are we starting to see the problem here?
Every 5 minutes, every computer in the cloud starts to collect stats and report them back.
And we get a traffic jam.
Every 10 minutes, more computers in the cloud collect more data, and our traffic jam gets worse.
Every 15 minutes, even more computers collect even more data, and our traffic jam puts your average freeway to shame.
Left to our own intuition, we create problems for ourselves and our organizations.
Each individual query is small, but because we're doing so many at once, it can grind performance to a halt.
By the way, this isn't a hypothetical.
Long long ago, the Office365 team asked me to make some monitoring software to help improve internal visibility into the datacenters.
Everyone asked for 5, 10, or 15 minute intervals. ~100 different metrics were collected from ~30000 machines.
And the first time we tried it on everything, the traffic jam ensued.
That's when I first realized the power of primes.
I made three slight adjustments to the timeframes:
- Every 5 minutes became every ~7 minutes
- Every 10 minutes became every ~11 minutes
- Every 15 minutes became every ~17 minutes
Now, instead of having a traffic jam every 5 minutes, things smoothed out.
- A small traffic jam would occur every ~77 minutes (
7*11) - Another small traffic jam would occur every ~119 minutes (
7*17) - Another small traffic jam would occur at ~187 minutes (
11*17) - All traffic could jam every ~1309 minutes (
7*11*17)
Note the tildas.
The real trick came in by using prime intervals in both minutes and seconds and using a random delay on the tasks to ensure they didn't all start at once.
This took the system from something that could derail a datacenter to something that could monitor thousands of machines while barely impacting performance.
This is the power of primes.
Hope this helps!
3
u/omglazrgunpewpew Jul 15 '26
Really digging the larger operational point here. Periodic jobs have such a remarkable ability to independently decide that 10:00 is the perfect time to run a surprise load test against their own backend. Distinct prime intervals plus some per-host staggering can help break up that little team-building exercise.
I have a few questions about where the prime behavior ends and the scheduling behavior begins.
Starting with a clean cache, so we have a known state:
I get all three numbers back. Is the intended contract that the filter must first receive an ascending sequence containing all the necessary divisors?
For example,
2..50 | primeworks because3,5, and7pass through before their composites. That seems different from the comment saying any positive whole number can be piped in independently.If that ordering and cached history are intentional, would stateful trial division be a more accurate description than the Sieve of Eratosthenes? My understanding of the sieve is that it starts with a bounded candidate range and marks multiples as composite, while this tests each incoming value against whatever has previously entered the cache.
Also noticed that the filter explicitly returns
1, the precomputed file starts with1, and the demo displays it as the first prime. Is1intentionally included for the timing framework, or has it managed to obtain an honorary membership in the prime club?Also also, I am having trouble reproing the time conversion. Dividing
8.84753141993573E+116seconds by the number of seconds in a 365.25-day year gives me approximately2.8E+109years. What calculation or set of intervals produces the 100 billion-year figure? Is the stated seconds value possibly from a different group of animations?On the sched side, how much of the smoothing would you attribute to the prime intervals versus the randomized start delay?
Distinct prime periods are pairwise coprime, so their least common multiple is their product. But every machine running the same seven min schedule would still remain aligned if they started aligned. Would it be fair to describe the primes as reducing collisions among different metrics, while the per-machine delay handles synchronization across the fleet?
Asking all of this because the operational story is genuinely useful. Periodic work quietly coordinating its own traffic jam is a very real problem, and separating those two mechanisms would make the lesson even stronger.
2
u/New_Drive_3617 Jul 15 '26
The logic seems sound in this; avoid collision by minimizing concurrent activity. However, acknowledgment must be given to the significant reduction in datapoints. For 5 > 7, the number of datapoints in a year is reduced from 105120 to 75085, representing a 29% reduction. That's usually adequate for most purposes, but in cases where 5 minutes is already a concession to resource availability, moving to 7 minute intervals can further obscure trends.
11
u/vlaircoyant Jul 14 '26
This was a really interesting read, especially about the overlap avoidance/concurrency issues.
Thank you.
9
u/y_Sensei Jul 14 '26
Excellent post, and an extra kudos for the Sieve of Eratosthenes implementation in PoSh.
Primes are an interesting topic, and they're even related to one of mathematic's most important unsolved problems - the Riemann hypothesis.
I find it fascinating that if the Riemann hypothesis holds, all of the seemingly randomly distributed primes share a common denominator, hence that distribution can be approximated by calculation. It's kind of like a hidden order in a chaotic system.
1
u/StartAutomating Jul 14 '26
BTW, I also gave PrimeTime a brief page. Any feedback is appreciated!
1
u/narcissisadmin Jul 15 '26
Prime numbers aren't rare, there are an infinite number of them (so far as we know).
1
u/Szeraax Jul 14 '26
Fun post! Thanks James!
0
u/[deleted] Jul 15 '26 edited Jul 15 '26
[removed] — view removed comment