Exemplo n.º 1
0
/**
 *    @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']);
        }
    }
}
Exemplo n.º 2
0
function find_3_prime($a)
{
    foreach ($a as $value) {
        if (is_prime($value)) {
            $primes[] = $value;
        }
    }
    echo $primes[2] . "<br/>";
}
Exemplo n.º 3
0
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;
}
Exemplo n.º 4
0
 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');
         }
     }
 }
Exemplo n.º 5
0
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;
            }
        }
    }
}
Exemplo n.º 6
0
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";
        }
    }
}
Exemplo n.º 8
0
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";
    }
}
Exemplo n.º 9
0
 /**
  * @depends testPrimeLoader
  * @expectedException \InvalidArgumentException
  */
 public function testIsPrimeException()
 {
     is_prime('str');
 }
Exemplo n.º 10
0
}
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>
Exemplo n.º 11
0
Arquivo: 7.php Projeto: arjona/euler
<?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;
}
Exemplo n.º 12
0
 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");
     }
 }
Exemplo n.º 13
0
<?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);
Exemplo n.º 14
0
 public function index()
 {
     readByLine($this->input, function ($line) {
         ifPrint(is_prime($line), $line, false);
     });
 }
Exemplo n.º 15
0
<?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";
Exemplo n.º 18
0
<?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;
Exemplo n.º 19
0
{
    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>';
Exemplo n.º 20
0
<?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;
        }
Exemplo n.º 21
0
<?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);
Exemplo n.º 22
0
    } 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>
Exemplo n.º 23
0
/**
 * @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;
}
Exemplo n.º 24
0
        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