Ronald Stewart headshot
Ronald Stewart headshot

Hello! I'm Ronald. I work online as a personal maths tutor 😃

New Blog Post: 2024 MAT and TMUA university admissions testing

Prime Number Pooping Bear - From the Archive

From Maths with Ronald


This is part of a series "From the Archive" - revisiting older mathematical blog posts. I've updated it with information relevant to Maths, Further Maths, and Computer Science A-Level students in 2024. A mild language warning - the official name uses a different word than pooping 😳😆

The Prime Number Pooping Bear has been online since 2001, pooping out more than one prime number each second.

JavaScript Code

The current version of the pooping bear uses your own computer to calculate the prime numbers, one-by-one, starting with 2 and 3. I've copied a part of the Javascript code so we can see what's going on there:

Code (copyright Arktinen Krokotiili Projekti) Ronald's comments
// Function returns FALSE for prime numbers and TRUE for non-prime numbers useful code comment - this function does the reverse of what you expect from the name!
function is_x_prime_number(x) maybe we could just change the name instead tho
{ x is our prime candidate - we will check if x is prime
var limit=0;
var div=3; start by checking x against divisor 3
var x_limit = Math.sqrt(x); only need to check x against divisors up to sqrt(x) - make this the limit
while (x%div!=0 && div<x_limit) loop, stopping if we find a perfect divisor, or reach the limit
div+=2; add two to the divisor each loop
is_prime = (x%div==0 && x!=div)*1 if we have found a divisor of x that isn't x itself, set is_prime to 0 (false)
return is_prime; remembering that is_prime actually returns isnt_prime
}
function find_next_prime() a function to find the next prime
{
do number+=2; loop, adding 2 to our (odd) prime candidate each loop
while(is_x_prime_number(number)) keep looping as long as the is_x_prime_number function above returns false
}

Improving the Algorithm

Being online since 2001 already proves the code is successful at its job. However, there are a bunch of places we could imagine improving this code.

For example, the current version checks if a number is prime by checking it against all odd numbers up to .

We can easily do better, mathematically. To check if a number is prime, you only need to check it against all prime numbers up to . Composite numbers like 15 don't need to be checked, since we will have already checked 3 and 5.

This would be faster, but you'd need more computer memory for this method to keep going - enough space to store the entire list of prime numbers you've come up with so far.

What other improvements could you make to the code?

Question: How high has it reached?

Okay, a challenge question:

The current version of the Prime Pooping Bear was first seen online on 4th November 2001.

If you had left a browser window open, and the bear had managed to poop one prime number each second since then (22.5 years!), approximately what number what it be pooping right now?

You can use the internet to help with this one 😉 and I'll give you a clue that it's possible to search for the answer, if you have the right search engine.

Feel like going for more accuracy? Don't forget about leap seconds 😃

Tutoring

Do you want some personal help with studying Mathematics, Further Mathematics, or Computer Science A-Level? Tutoring is my full-time work!

Write to me at ronald@mathswithronald.com or WhatsApp +31682589013 and we can arrange a first session.