function prime($n, $i = 2) { if ($n == $i) { return 0; } else { if (!($n % $i)) { return 1; } else { return prime($n, $i + 1); } } }
function prime($number, $i) { if ($i == 1) { return 1; } else { if ($number % $i == 0) { return 1 + prime($number, --$i); } else { 0 + prime($number, --$i); } } }
function prime_factors_rev($num) { for ($i = (int) ($num / 2); $i >= 2; $i--) { if (prime($i)) { if ($num % $i == 0) { return $i; } } } return $num; }
<?php function prime($a) { if ($a % 2 == 0 && $a != 2 || $a < 2) { return false; } $limit = sqrt($a); for ($i = 2; $i <= $limit; $i++) { if ($a % $i == 0) { return false; } } return true; } foreach (range(1, 100) as $x) { if (prime($x)) { echo "{$x}\n"; } }
<?php /** * prime() determines the prime number * Use iteration method * * @param [integer] $input [The input number] * @return [integer] 1 or 0 [The prime result] */ function prime($input) { for ($i = 2; $i <= ceil(sqrt($input)); $i++) { if ($input % $i == 0) { return 1; } } return 0; } $number = 13; if (prime($number) == 1) { echo "{$number} is not a prime number"; } else { echo "{$number} is a prime number"; }
echo '<br><br>'; function sum($a, $b) { $c = $a + $b; return $c; } $sum2 = sum(6, 34); echo 'sum of two digit = ' . $sum2; echo '<br><br>'; function prime($j) { for ($l = 2; $l < $j; $l++) { if ($j % $l == 0) { $m = 0; break; } else { $m = 1; } } return $m; } $prime2 = prime(59); if ($prime2 == 1) { echo 'this is prime no.'; } else { echo 'this is not a prime number'; } echo '<br><br>'; ?> </body> </html>
//numbers to be checked as prime $counter = 0; for ($j = 1; $j <= $i; $j++) { //all divisible factors if ($i % $j == 0) { $counter++; } } //prime requires 2 rules ( divisible by 1 and divisible by itself) if ($counter == 2) { print $i . "<br/>"; } } } echo '<strong>Prime Number 2,3,5,7....</strong></br>'; prime(20); //find prime numbers from 1-20 echo str_repeat('-', 100) . '</br>'; /********************************************************************************* ************************ Star Triangle ****************************************** ********************************************************************************/ echo '<strong>Star Triangle</strong></br>'; for ($i = 0; $i <= 5; $i++) { for ($j = 1; $j <= $i; $j++) { echo "* "; } echo "<br>"; } echo "<br>"; /********************************************************************************/ for ($i = 0; $i <= 5; $i++) {
<?php error_reporting(-1); $min = 1; $max = 100; $total = 0; $message = $min . "~" . $max . "までの素数を除いた数字を足す"; if (isset($_POST['add'])) { // $min~$maxまでの足し算を繰り返す for ($i = $min; $i <= $max; $i++) { $count = 0; prime($i, $count); $total = $total + $i; } $message = $min . "~" . $max . "までの素数を除いた合計は" . $total . "です。"; } // 素数を判定して飛ばす関数 function prime(&$i, &$count) { // 1から$iまでの数で$1を剰余算 for ($j = 1; $j <= $i; $j++) { // わり切れた回数をカウント if ($i % $j == 0) { $count = $count + 1; } } // わり切れた回数が二回なら素数 if ($count == 2) { // 2は次が素数なので二つ飛ばす if ($i == 2) { $i = $i + 2;
/** * @depends testPrimeLoader */ public function testPrime() { $this->assertEquals(2, prime(1), '1st prime number'); $this->assertEquals(29, prime(10), '10th prime number'); $this->assertEquals(7919, prime(1000), '1000th prime number'); }