My Prime Factor Calculator

I created this code to quickly calculate the number of prime factors each number has.

Originally, this just dumped the information into boring tables however after watching numberphile's excellent video on Prime Spirals I wondered whether this pattern repeated itself for numbers depending on the number of prime factors they have.

ie: Prime number have only one prime factor - themselves. However, each number has a unique number of prime factors. For example, 4 is 2 x 2 - this has 2 prime factors. 30 has 3 prime factors, 2 x 3 x 5.

So I used my code to plot a particular colour of square depending on how many prime factors it had onto a Ulam spiral and see if the patterns observed were unique to primes. You can now run that code on this webpage - all you need is a browser that is HTML5 compatible with Javascript turned on.

After running the code, the spiral will appear, and there will be a series of toggles at the bottom to switch on or off the dots for each group of prime factors.

Please be aware that this code calculates and stores the prime factors for the first 65,536 numbers so it could take some time to run, particularly on mobile devices. On my fairly beefy laptop it takes 4 seconds to complete, 32 seconds on my phone (Samsung Galaxy Note 2) and 80 seconds on my iPad 2. It has been programmed in such a way it shouldn't freeze up your browser, but that may depend on how well powered your machine is.


I have now optimised the code so it is a lot lot faster - will now 4 times the quantity of numbers in less time than it took to do 65,536. So I have added the options to search for more, simply click on the number you wish to calculator and display (note, 4 million numbers takes up a 2,048 x 2,048 square so you will need to scroll unless you have some fancy 4K monitor)

Calculating 4 million numbers also will take a lot longer - about two minutes on a fast laptop. Note, the progression is not linear, as the bigger the number gets there is a bigger library of prime factors to check.

I also thought it would be interesting to plot them on a triangular spiral instead of a square - but this takes up far more room so I only gave the option for smaller numbers.

Square Spiral (typical ULAM)

Triangular Spiral (alternative)

Press start to begin calculating prime factors.


This webpage and all of its code was written and created by Ben Dickson.

You can follow me on Twitter at @Alien2human.