To create a list of primes in a bash based on php wich can be resumed after breaking I did use fmod() and some snippets offered by two more users here on php comments.
This will output :
"prime;difference-between-last-and-current-prime"
So credit goes to them. I only did the logfile output.
This will function up to whatever fmod supports as highest value. Just enter the $end value. And do a touch to the logfile followed by chmod 666 so php can access it.
<?php
function tailCustom($filepath, $lines = 1, $adaptive = true) {
$f = @fopen($filepath, "rb");
if ($f === false) return false;
if (!$adaptive) $buffer = 4096;
else $buffer = ($lines < 2 ? 64 : ($lines < 10 ? 512 : 4096));
fseek($f, -1, SEEK_END);
if (fread($f, 1) != "\n") $lines -= 1;
$output = '';
$chunk = '';
while (ftell($f) > 0 && $lines >= 0) {
$seek = min(ftell($f), $buffer);
fseek($f, -$seek, SEEK_CUR);
$output = ($chunk = fread($f, $seek)) . $output;
fseek($f, -mb_strlen($chunk, '8bit'), SEEK_CUR);
$lines -= substr_count($chunk, "\n");
}
while ($lines++ < 0) {
$output = substr($output, strpos($output, "\n") + 1);
}
fclose($f);
return trim($output);
}
function isPrime( $num )
{
for( $i = 2; $i*$i <= $num; $i++ )
if( !fmod($num,$i) )
return FALSE;
return TRUE;
}
$logfile = 'prim_save.log';
$lastline = explode(";", tailCustom($logfile));
$begin = ($lastline[0] +1);
$lastprime = $lastline[0];
$end = 999999999999999999999999999999999999;
$fp = fopen($logfile, 'a');
for($i = $begin; $i<$end; $i++)
{
if(isPrime($i) == TRUE)
{
$difference = $i - $lastprime;
fputs($fp,$i.';'.$difference.';'."\n");
$lastprime = $i;
}
}
fclose($fp);
?>