$y = sqrt($x); while ($x % $d != 0 && $d < $y) { $d += 2; } return ($x % $d == 0 && $x != $d) * 1 == 0 ? true : false; } function find_3_prime($a) { foreach ($a as $value) { if (is_prime($value)) { $primes[] = $value; } } echo $primes[2] . "<br/>"; } find_3_prime($arr); function check_excists($a) { if (in_array(146, $a)) { $f = "The number 146 exists in the array"; } else { $f = "The number 146 does not exist in the array"; } if (in_array(284, $a)) { $s = "The number 284 exists in the array"; } else { $s = "The number 284 does not exist in the array"; } if (in_array(871, $a)) { $t = "The number 871 exists in the array"; } else {
global $arr; $check_numbers = array(146, 284, 871); foreach ($check_numbers as $elem) { if (!in_array($elem, $arr)) { echo "The number {$elem} [does not] exist in the array <br />"; } } } ?> <!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 print_r(find_3_prime()); echo "<br />"; print_r($arr); echo "<br />"; print_r(check_exists()); ?> </body> </html>
if (is_prime($value)) { $i++; } if ($i == 3) { return $value; break; } } if ($i < 3) { return "not found"; } } function check_exists($ar) { $s = "The number %d %s exist in the array.<br>"; $array = array(146, 284, 871); foreach ($array as $value) { if (in_array($value, $ar)) { printf($s, $value, ""); } else { printf($s, $value, "does not"); } } } printf("Third prime number: %s <br>", find_3_prime($arr)); check_exists($arr); ?> </body> </html>
<?php error_reporting(E_ALL); require_once 'functions.php'; $numbers = range(20, 1000, 37); find_3_prime($numbers); check_exists($numbers); function find_3_prime($numbers) { $primesCount = 0; foreach ($numbers as $number) { if (is_prime($number)) { $primesCount++; } if ($primesCount == 3) { echo "{$number}<br />"; break; } } } function check_exists($numbers) { $toCeck = array(146, 284, 871); foreach ($toCeck as $number) { $not = in_array($number, $numbers) ? '' : ' does not'; echo "The number {$number}{$not} exists in the array.<br />"; } }