How to find prime numbers
Share on Facebook

Using PHP to find prime numbers within a given range

A prime number is a number which cannot be divided by any number, other than itself and one, and return a whole number. In other words, no matter what number you divide a prime number by, you will have a remainder - unless you divide the number by itself or one.

Here are the prime numbers below 1000.

 

235711131719232931374143475359616771
7379838997101103107109113127131137139149151157163167173
179181191193197199211223227229233239241251257263269271277281
283293307311313317331337347349353359367373379383389397401409
419421431433439443449457461463467479487491499503509521523541
547557563569571577587593599601607613617619631641643647653659
661673677683691701709719727733739743751757761769773787797809
811821823827829839853857859863877881883887907911919929937941
947953967971977983991997

 

And here is the extremely elegant code, which I found on the internet, used to generate them.

 

 

The function returns the prime numbers in an array, which can then be used as normal.

Example of how to call the function to find all prime numbers up to 1,000:
<?php
$numbers = getPrimes(1000);
?>

On this page, the results were displayed in a table format using this code - which is by no means perfect (the last row does not have its full complement of cells) but it is designed to be easy to read.