I have been working on a php function to create an array of prime numbers. So far I have it working to where it lists all the primes from 2 to 1000. What I want to do now is generate the first 100 prime numbers by using the increment count < 100 or something similar. Here is my current code.
<?php
function prima($n)
{
$primeNumbers = []; // Initiate result array
for ($i = 1; $i <= $n; $i++)
{
$counter = 0;
for ($j = 1; $j <= $i; $j++)
{
if ($i % $j == 0)
{
$counter++;
}
}
if ($counter == 2)
{
$primeNumbers[] = $i; // store value to array
}
}
return json_encode($primeNumbers); // return converted json object
}
header('Content-Type: application/json'); // tell browser what to expect
echo prima(1000); // echo the json string returned from function
?>
At the end of the for loop, add 'if (count($primeNumbers) == 100) break;'