/** * @initRequest */ function filter_prime_2(array &$requests, $server) { foreach ($requests['requests'] as &$request) { if ($request[0] === 'is_prime_2') { $request[1]['result'] = is_prime($request[1]['q']); } } }
function find_3_prime($a) { foreach ($a as $value) { if (is_prime($value)) { $primes[] = $value; } } echo $primes[2] . "<br/>"; }
function largest_prime($number) { $largest_prime_factor = 0; foreach (range(2, $number - 1) as $value) { if (is_prime($value) && $number % $value == 0 && $value > $largest_prime_factor) { $largest_prime_factor = $value; } } return $largest_prime_factor; }
protected function validate() { if ($this->isValidInput($this->mValue)) { return $this->setError('invalid'); } if ($this->aArgs['prime']) { if (!is_prime($this->mValue)) { return $this->setError('prime'); } } }
function find_3_prime() { global $arr; $primes_count = 0; foreach ($arr as $elem) { if (is_prime($elem)) { $primes_count++; if ($primes_count == 3) { return $elem; } } } }
function find_3_prime($numbers) { $primesCount = 0; foreach ($numbers as $number) { if (is_prime($number)) { $primesCount++; } if ($primesCount == 3) { echo "{$number}<br />"; break; } } }
function print_primes() { if ($_POST) { $start = intval($_POST["start"]); $end = intval($_POST["end"]); if ($start < $end) { $result = array(); for ($number = $start; $number <= $end; $number++) { $result[] = is_prime($number) ? "<strong>{$number}</strong>" : $number; } return implode(", ", $result); } else { return "The start number must be less than the end number"; } } }
function find_3_prime($ar) { $i = 0; foreach ($ar as $value) { if (is_prime($value)) { $i++; } if ($i == 3) { return $value; break; } } if ($i < 3) { return "not found"; } }
/** * @depends testPrimeLoader * @expectedException \InvalidArgumentException */ public function testIsPrimeException() { is_prime('str'); }
} function in_range($number) { global $range_down, $range_up; if ($number < $range_down or $number > $range_up) { return false; } else { return true; } } if (isset($_GET['number'])) { $param = $_GET['number']; //echo $param; if (is_numeric($param)) { if (in_range($param)) { if (is_prime($param)) { echo "The number {$param} is prime !"; } else { echo "<p class=blue> The number {$param} is NOT prime !</p>"; } } else { echo "<p class=red> The parameter is not within the range [{$range_down},{$range_up}]</p>"; } } else { echo "<p class=red> The parameter is not a number</p>"; } } ?> </body> </html>
<?php $prime_list = array(2); // 2 is on the list fo sho! $i = 3; while (count($prime_list) < 10001) { $ip = is_prime($i, $prime_list); $i = $i + 2; // don't bother checking even numbers, 2 is the only even prime } $p = array_pop($prime_list); echo "{$p}\n"; // we check whether the number is prime. // if it is, add it to the prime list. // we check against that list to make checking big primes easier function is_prime($num, &$pl) { foreach ($pl as $p) { if (fmod($num, $p) == 0) { return false; } } $pl[] = $num; return true; }
public static function factorization($n) { if (extension_loaded('gmp') && USE_EXT == 'GMP') { if (is_int($n) || is_long($n)) { if ($n < 2) { returnarray(); } $result = array(); $d = 2; foreach (self::$smallprimes as $d) { if ($d > $n) { break; } $q = $n / $d; $r = $n % $d; if ($r == 0) { $count = 1; while ($d <= $n) { $n = $q; $q = $n / $d; $r = $n % $d; if ($r != 0) { break; } $count++; } array_push($result, array($d, $count)); } } if ($n > end(self::$smallprimes)) { if (is_prime($n)) { array_push($result, array($n, 1)); } else { $d = end(self::$smallprimes); while (true) { $d += 2; $q = $n / $d; $r = $n % $d; if ($q < $d) { break; } if ($r == 0) { $count = 1; $n = $q; while ($d <= n) { $q = $n / $d; $r = $n % $d; if ($r != 0) { break; } $n = $q; $count++; } array_push($result, array($n, 1)); } } } } return $result; } } elseif (extension_loaded('bcmath') && USE_EXT == 'BCMATH') { if (is_int($n) || is_long($n)) { if ($n < 2) { returnarray(); } $result = array(); $d = 2; foreach (self::$smallprimes as $d) { if ($d > $n) { break; } $q = $n / $d; $r = $n % $d; if ($r == 0) { $count = 1; while ($d <= $n) { $n = $q; $q = $n / $d; $r = $n % $d; if ($r != 0) { break; } $count++; } array_push($result, array($d, $count)); } } if ($n > end(self::$smallprimes)) { if (is_prime($n)) { array_push($result, array($n, 1)); } else { $d = end(self::$smallprimes); while (true) { $d += 2; $q = $n / $d; $r = $n % $d; if ($q < $d) { break; } if ($r == 0) { $count = 1; $n = $q; while ($d <= n) { $q = $n / $d; $r = $n % $d; if ($r != 0) { break; } $n = $q; $count++; } array_push($result, array($n, 1)); } } } } return $result; } } else { throw new ErrorException("Please install BCMATH or GMP"); } }
<?php $NUM = 111181111; function is_prime($n) { $i = 2; while ($i < $n) { if ($n % $i == 0) { return false; } else { $i += 1; } } return true; } echo is_prime($NUM);
public function index() { readByLine($this->input, function ($line) { ifPrint(is_prime($line), $line, false); }); }
<?php include_once 'primefunctions.php'; $input = $_POST['input']; $title = "Is {$input} prime?"; include_once 'header.php'; $isprime = is_prime($input); echo "<h1>{$title}</h1>"; echo "<p>"; if (!$isprime) { echo "{$input} is not a prime number"; } else { echo "{$input} is a prime number"; } echo "</p><p><a href='index.php'>Return to main page</a></p>";
function euler_totient($n) { $n = abs($n); if (is_prime($n)) { return $n - 1; } else { if ($n <= 1) { return $n; } } $phin = 1; $factors = factorisation($n); $factors = array_count_values($factors); foreach ($factors as $p => $frequency) { $phin *= ($p - 1) * pow($p, $frequency - 1); } return $phin; }
$d = $n - 1; $s = 0; while ($d % 2 == 0) { $d /= 2; $s++; } for ($i = 0; $i < $k; $i++) { $a = rand(2, $n - 1); $x = bcpowmod($a, $d, $n); if ($x == 1 || $x == $n - 1) { continue; } for ($j = 1; $j < $s; $j++) { $x = bcmod(bcmul($x, $x), $n); if ($x == 1) { return false; } if ($x == $n - 1) { continue 2; } } return false; } return true; } for ($i = 1; $i <= 1000; $i++) { if (is_prime($i, 10)) { echo "{$i}, "; } } echo "\n";
<?php error_reporting(E_ALL); require_once 'functions.php'; define('LIMIT_BOTTOM', 0); define('LIMIT_TOP', 19999); if (!array_key_exists('number', $_GET) || !preg_match('/^\\d{1,' . strlen(LIMIT_TOP) . '}$/', $_GET['number'])) { list($messageClass, $message) = array('error', 'The parameter is not a number'); } elseif ($_GET['number'] < LIMIT_BOTTOM || LIMIT_TOP < $_GET['number']) { list($messageClass, $message) = array('error', 'The parameter is not within the range [' . LIMIT_BOTTOM . ',' . LIMIT_TOP . ']'); } elseif (!is_prime($_GET['number'])) { list($messageClass, $message) = array('not-prime', "The number {$_GET['number']} is NOT prime!"); } else { list($messageClass, $message) = array('prime', "The number {$_GET['number']} is prime!"); } ?> <!DOCTYPE html> <html> <head> <style type="text/css"> .error { color: #FF0000;} .not-prime { color: #0000FF;} .prime { color: #000000;} </style> </head> <body> <span class="<?php echo $messageClass; ?> "><?php echo $message;
{ if ($i % 2 != 1) { return false; } $d = 3; $x = sqrt($i); while ($i % $d != 0 && $d < $x) { $d += 2; } return ($i % $d == 0 && $i != $d) * 1 == 0 ? true : false; } // example: show all prime numbers between $start and $end $start = 0; $end = 1000; echo ' <p> all prime numbers between ' . $start . ' and ' . $end . ' </p> <p>'; for ($i = $start; $i <= $end; $i++) { if (is_prime($i)) { echo ' ' . $i . ' '; } } echo ' </p>'; $time_end = get_microtime(); $time = $time_end - $time_start; $time = round($time, 6); //echo '<p>this page loaded in '.$time.' seconds.</p>';
<?php /** * Problem 7: Project Euler * Title: 10001st prime * Description: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. * What is the 10 001st prime number? * Author: Jonathan Herbst * Last Edited: 10/11/2015 */ $count = 0; $max = 10001; $check = 1; $prime_num = 0; while ($count < $max) { if (is_prime($check)) { $count++; $prime_num = $check; } $check += 2; } function is_prime($number) { $index = 3; if ($number === 2 || $number === 3) { return true; } while ($index < $number) { if ($number % $index === 0) { return false; }
<?php for ($x = 2; $x <= 1000000; $x++) { if (!is_prime($x)) { continue; } $num = $x; $disp = ""; for ($y = 1; $y < strlen($x); $y++) { $num = str_split($num); $digit = array_shift($num); array_push($num, $digit); $num = implode('', $num); if (!is_prime($num)) { continue 2; } $disp .= "{$x} - {$num}<br>"; } echo "{$disp}{$x} = BINGO<br><br>"; $out[] = $x; } echo count($out);
} else { return false; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <style type="text/css"> span { color:red; } </style> </head> <body> <?php $num = $_GET['number']; if (!is_numeric($num)) { echo "<span>The parameter is not a number</span>"; } elseif (!($num >= 0 && $num <= 19999)) { echo "<span>The parameter is not within the range [0,19999]</span>"; } elseif (is_prime($num)) { echo "The number {$num} is prime !"; } else { echo "The number {$num} is NOT prime!"; } ?> </body> </html>
/** * @param $n * @return bool */ function Carmicheal($n) { if (is_prime($n)) { return false; } $p = 0; $l = 1; for ($i = 1; $i <= $n; $i++) { if ($n % $i == 0) { if (is_prime($i)) { if ($l * $i <= $n) { $l *= $i; $p++; } } } if ($p > 2) { return true; } } return false; }
return 0; } $i = 2; while ($i < $num) { if ($num % $i == 0) { return 0; } $i++; } return 1; } //CODE //Prvocisla: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, … //echo(is_prime(1)."\n".(7%2)); //Debug //Settings $min = 1; $max = 3000; //Logic $total_primes = 0; while ($min <= $max) { if (is_prime($min)) { echo $min . "\n"; //Show all primes $total_primes++; //Count all primes } $min++; } //Output echo "\nPrimes total: " . $total_primes . "\n"; //Total number of primes