本文整理汇总了PHP中isPrime函数的典型用法代码示例。如果您正苦于以下问题:PHP isPrime函数的具体用法?PHP isPrime怎么用?PHP isPrime使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了isPrime函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: echoPrimeUpTo
function echoPrimeUpTo($range)
{
echo "Printing prime numbers up tp {$range} <br>";
for ($i = "1"; $i <= $range; $i++) {
if (isPrime($i)) {
echo "{$i}<br>";
}
}
}
示例2: findPrimes
function findPrimes($n)
{
$primes = array();
for ($i = 1; $i < $n; $i++) {
if (isPrime($i)) {
$primes[] = $i;
}
}
return $primes;
}
示例3: checkPrimeRange
function checkPrimeRange($start, $end)
{
for ($i = $start; $i <= $end; $i++) {
if (isPrime($i)) {
echo "<strong>{$i}, </strong>";
} else {
echo $i . ", ";
}
}
}
示例4: main
function main()
{
$max = 2000000;
$sum = 0;
foreach (range(1, $max) as $number) {
if (isPrime($number)) {
$sum += $number;
}
}
echo $sum;
}
示例5: tPrime
function tPrime($n)
{
$len = strlen($n);
for ($i = 2; $i <= $len; $i++) {
if (isPrime(substr($n, 0, $i)) && isPrime(substr($n, strlen($n) - $i, $i))) {
} else {
return false;
}
}
return true;
}
示例6: countQuadraticConsecutivePrimes
function countQuadraticConsecutivePrimes($a, $b)
{
$nPrimes = 0;
for ($n = 0; true; ++$n) {
if (isPrime(abs($n * $n + $a * $n + $b))) {
++$nPrimes;
} else {
return $nPrimes;
}
}
}
示例7: getPrimeNumbers
function getPrimeNumbers($max)
{
$result = '2';
$nbr = 2;
while ($nbr <= $max) {
if ($nbr % 2 != 0) {
if (isPrime($nbr)) {
$result .= ',' . $nbr;
}
}
$nbr++;
}
return $result;
}
示例8: nextPrime
function nextPrime($x)
{
// Precondition: $x is prime or 1. (Handy for initialization of an iterator.)
if ($x == 1) {
return 2;
}
if (isDivisible($x, 2)) {
--$x;
}
do {
$x += 2;
} while (!isPrime($x));
return $x;
}
示例9: main
function main()
{
$n = 10001;
$number = 2;
$i = 0;
while (1) {
if (isPrime($number)) {
$i++;
// echo $i . ' -- '. $number . PHP_EOL;
if ($i === $n) {
echo $number;
break;
}
}
$number++;
}
}
示例10: reducer
function reducer($num, $sieve, $primes, $target)
{
$negSieve = sieve($num, $primes);
$new = $primes;
foreach ($negSieve as $key => $value) {
if ($value == 0 && isPrime($key, $primes)) {
if (!in_array($key, $primes)) {
$new[] = $key;
if (count($new) - 1 == $target) {
result(104743, $new[$target - 1]);
die;
}
}
}
}
return $new;
}
示例11: factorize
function factorize($number)
{
$factors = array();
if (isPrime($number)) {
return array($number => 1);
}
$ceil = sqrt($number);
$factor = 2;
while ($factor <= $ceil) {
if ($number % $factor == 0) {
$factors = addFactorizations($factors, factorize($factor));
$factors = addFactorizations($factors, factorize($number / $factor));
break;
}
$factor++;
}
return $factors;
}
示例12: isCircular
function isCircular($num)
{
if (!isPrime($num)) {
return false;
}
if ($num > 10) {
$numstr = (string) $num;
if (strstr($numstr, "0") || strstr($numstr, "5") || strstr($numstr, "2") || strstr($numstr, "4") || strstr($numstr, "6") || strstr($numstr, "8")) {
return false;
}
$len = strlen($numstr);
for ($i = 0; $i < $len - 1; $i++) {
$numstr = (string) $num;
$num = rotateLeft($numstr);
if (!isPrime((int) $num)) {
return false;
}
}
}
return true;
}
示例13: factorize
function factorize(&$target, &$primes, &$factor)
{
$multiplier = 2;
$factored = true;
$sieve = range($primes[count($primes) - 1] + 1, $factor);
for ($i = 0; $i < count($primes); $i++) {
if ($target % $primes[$i] == 0) {
$target = $target / $primes[$i];
if (isPrime($target)) {
result(6857, $target);
die;
}
}
if ($i == count($primes) - 1) {
$factored = false;
$factor *= $multiplier;
$primes = reducer($factor, $sieve, $primes, $target);
factorize($target, $primes, $factor);
}
}
}
示例14: microtime
<?php
/**
* 10001st prime
* https://projecteuler.net/problem=7
*/
require '../show_answer.php';
$startTime = microtime(true);
$number = 0;
$value = 2;
$max = 10001;
while ($number < $max) {
if (isPrime($value)) {
$number++;
}
$value++;
}
$result = $value - 1;
showAnswer($result, $startTime);
function isPrime($value)
{
$root = sqrt($value);
$divisor = 2;
while ($divisor <= $root) {
if ($value % $divisor == 0) {
return false;
}
$divisor++;
}
return true;
}
示例15: date
}
return $prime ? true : false;
}
$text = "Hej. Idag är det ";
$text2 = "Tiden är ";
$today = date('Y-m-d');
$time = date("H:i:s");
$hemligSumma = array(10, 20, 30, 40, 50);
$slumpadSida = array('www.flashback.org', 'http://www.piratpartiet.se/', 'www.google.se', 'www.php.net');
echo $text . $today . "<br />" . $text2 . $time . "<br />";
echo "Idag skall vi räkna matematik: ";
if (isset($_POST['submit'])) {
$summa = intval($_POST["ma1"]) + intval($_POST["ma2"]);
echo $_POST["ma1"] . " + " . $_POST["ma2"] . " är lika med " . $summa . "<br /><br />";
if ($summa == $hemligSumma[mt_rand(0, 4)]) {
echo "Du lyckades hitta det hemliga nummret du får en länk <br />" . $slumpadSida[mt_rand(0, 3)];
}
echo isPrime($summa) ? $summa . ' är ett primtal<br/>' : $summa . ' är inte ett primtal<br/>';
}
?>
<form action="<?php
echo $_SERVER['PHP_SELF'];
?>
" method="post">
<input type="text" name="ma1" /> +
<input type="text" name="ma2" /><br />
<input type="submit" name="submit" value="Räkna" />
</form>
</body>
</html>