예제 #1
0
function echoPrimeUpTo($range)
{
    echo "Printing prime numbers up tp {$range} <br>";
    for ($i = "1"; $i <= $range; $i++) {
        if (isPrime($i)) {
            echo "{$i}<br>";
        }
    }
}
function findPrimes($n)
{
    $primes = array();
    for ($i = 1; $i < $n; $i++) {
        if (isPrime($i)) {
            $primes[] = $i;
        }
    }
    return $primes;
}
예제 #3
0
function checkPrimeRange($start, $end)
{
    for ($i = $start; $i <= $end; $i++) {
        if (isPrime($i)) {
            echo "<strong>{$i}, </strong>";
        } else {
            echo $i . ", ";
        }
    }
}
function main()
{
    $max = 2000000;
    $sum = 0;
    foreach (range(1, $max) as $number) {
        if (isPrime($number)) {
            $sum += $number;
        }
    }
    echo $sum;
}
예제 #5
0
파일: euler037.php 프로젝트: Nilithus/euler
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
0
파일: euler27.php 프로젝트: LucidTaZ/Euler
function countQuadraticConsecutivePrimes($a, $b)
{
    $nPrimes = 0;
    for ($n = 0; true; ++$n) {
        if (isPrime(abs($n * $n + $a * $n + $b))) {
            ++$nPrimes;
        } else {
            return $nPrimes;
        }
    }
}
예제 #7
0
function Prime($n)
{
    $count = 0;
    $primeNumber = 0;
    for ($k = 1; $count < $n; $k++) {
        if (isPrime($k)) {
            $count++;
            $primeNumber = $k;
        }
    }
    return $primeNumber;
}
예제 #8
0
function getPrimeNumbers($max)
{
    $result = '2';
    $nbr = 2;
    while ($nbr <= $max) {
        if ($nbr % 2 != 0) {
            if (isPrime($nbr)) {
                $result .= ',' . $nbr;
            }
        }
        $nbr++;
    }
    return $result;
}
예제 #9
0
파일: math.php 프로젝트: LucidTaZ/Euler
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;
}
예제 #10
0
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++;
    }
}
예제 #11
0
파일: euler007.php 프로젝트: Nilithus/euler
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;
}
예제 #12
0
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;
}
예제 #13
0
파일: euler035.php 프로젝트: Nilithus/euler
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;
}
예제 #14
0
파일: euler003.php 프로젝트: Nilithus/euler
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);
        }
    }
}
예제 #15
0
<?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;
}
예제 #16
0
    }
    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>
예제 #17
0
<?php

function isPrime($num)
{
    if ($num == 1) {
        return false;
    }
    if ($num == 2) {
        return true;
    }
    if ($num % 2 == 0) {
        return false;
    }
    for ($i = 3; $i < $num; $i = $i + 2) {
        if ($num % $i == 0) {
            return false;
        }
    }
    return true;
}
$a = 127;
// для вывода числа
if (isPrime($a)) {
    echo "{$a} is a prime";
} else {
    echo "{$a} is not a prime";
}
echo "<br>";
var_dump(isPrime(127));
// булевым значением
예제 #18
0
            return false;
        }
    }
    return true;
}
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return (double) $usec + (double) $sec;
}
$start = microtime_float();
$startPrime = microtime_float();
$i = 0;
while ($i < 100000) {
    $rand = rand(1147483647, 2147483647);
    if (isPrime($rand)) {
        echo "+";
    } else {
        echo "-";
    }
    $i++;
}
$endPrime = microtime_float();
$startWrite = microtime_float();
$x = 0;
while ($x < 100) {
    $i = 0;
    $longString = '';
    while ($i < 10000) {
        $rand = rand();
        $longString .= $rand;
예제 #19
0
            return true;
        } else {
            for ($divisor = 2; $divisor <= (int) sqrt($num); $divisor++) {
                if ($num % $divisor == 0) {
                    return false;
                }
            }
        }
    }
    return true;
}
if (isset($_POST['submit']) && $_POST['start'] != '' && $_POST['end'] != '' && is_numeric($_POST['start']) && is_numeric($_POST['end'])) {
    $start = $_POST['start'];
    $end = $_POST['end'];
    for ($i = $start; $i <= $end; $i++) {
        if (isPrime($i)) {
            ?>
                    <strong><?php 
            echo $i;
            ?>
</strong>
                <?php 
        } else {
            echo $i;
        }
        if ($i != $end) {
            echo ", ";
        }
    }
}
?>
예제 #20
0
파일: 7.php 프로젝트: keune/euler-solutions
<?php

require_once '../functions.php';
$num = 2;
$i = 0;
while (true) {
    if (isPrime($num)) {
        $i++;
    }
    if ($i == 10001) {
        break;
    }
    $num++;
}
echo $num;
<?php

$fp = fopen("php://stdin", "r");
$testCases = 0;
fscanf($fp, "%d", $testCases);
$n = 0;
while ($testCases-- > 0) {
    fscanf($fp, "%d", $n);
    echo isPrime($n), "\n";
}
function isPrime($n)
{
    if ($n === 1) {
        return "Not prime ";
    }
    for ($i = 2; $i * $i <= $n; ++$i) {
        if ($n % $i === 0) {
            return "Not prime";
        }
    }
    return "Prime";
}
예제 #22
0
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>Find Primes in Range</title>
</head>
<body>
<form>
    <label for="start">Starting Index:</label>
    <input type="text" name="start" id="start">
    <label for="end">End:</label>
    <input type="text" name="end" id="end">
    <input type="submit">
</form>
<?php 
if (isset($_GET['start']) && isset($_GET['end'])) {
    if (is_numeric($_GET['start']) && is_numeric($_GET['end'])) {
        for ($i = intval($_GET['start']), $count = 0; $i <= intval($_GET['end']); $i++, $count++) {
            $value = isPrime($i) ? "<b>{$i}</b>" : $i;
            if ($count != 0) {
                echo ', ' . $value;
            } else {
                echo $value;
            }
        }
    }
}
?>
</body>
</html>
예제 #23
0
function isPrime($n)
{
    // Iterate to half of the supplied number, rounded up if necessary. I can't use the square root like I had originally planned.
    for ($i = 3; $i <= ceil($n / 2); $i++) {
        if ($n % $i == 0) {
            return false;
        }
    }
    return true;
}
$a = 600851475143;
//$a = 13195;
for ($i = 3; $i <= floor($a / 2); $i = $i + 2) {
    if ($a % $i == 0) {
        $y = $a / $i;
        if (isPrime($y)) {
            echo $y . '<br>';
            break;
        }
    }
}
//
//
//// Script end
//function rutime( $ru, $rus, $index ) {
//	return ( $ru["ru_$index.tv_sec"] * 1000 + intval( $ru["ru_$index.tv_usec"] / 1000 ) )
//	       - ( $rus["ru_$index.tv_sec"] * 1000 + intval( $rus["ru_$index.tv_usec"] / 1000 ) );
//}
//
//$ru = getrusage();
//echo "This process used " . rutime( $ru, $rustart, "utime" ) .
예제 #24
0
파일: hw1.php 프로젝트: HYOUJI/PHP
        #echo $n."<br>";
        return $n * factorial($n - 1);
    } else {
        return 1;
    }
}
echo "factorial(5) = " . factorial(5) . "<br><br>";
#10번
echo "<h3>10.</h3>";
function isPrime($num)
{
    if ($num % 2 == 1) {
        # 1 3 5 7 9 11 13 15 17 19 21 23 25 ... 홀수
        if ($num % 3 != 0 && $num % 5 != 0) {
            echo "This is prime number!!<br>";
        }
    } else {
        echo "This is not prime number...<br>";
    }
}
$num1 = 2;
$num2 = 13;
echo "INPUT >> {$num1}<br>";
isPrime(2);
echo "INPUT >> {$num2}<br>";
isPrime(13);
echo "<br><br>";
?>
	
</body>
</html>
예제 #25
0
                    <div class="page-header">
                        <h1>Solution</h1>      
                    </div>
                    <form class="form-horizontal" action="index2.php" method="POST">
                        <div class="form-group">
                          <label for="number" class="col-sm-2 control-label">Enter a number</label>
                          <div class="col-sm-10">
                              <input type="number" class="form-control" name="given_number" id="number" placeholder="Enter an Integer number....">
                          </div>
                        </div>
                        <div class="form-group">
                          <label for="result" class="col-sm-2 control-label">Result</label>
                          <div class="col-sm-10">
                              <input type="text" class="form-control" id="result" value="<?php 
if (isset($_POST['given_number'])) {
    echo isPrime($_POST['given_number']);
}
?>
 ">
                          </div>
                        </div>
                        <div class="form-group">
                          <div class="col-sm-offset-2 col-sm-10">
                              <button type="submit" class="btn btn-default" name="btn" value="submit">Submit</button>
                          </div>
                        </div>
                    </form>
                    <div class="panel panel-success">
                      <div class="panel-heading">
                        <h3 class="panel-title">Solution Code</h3>
                      </div>
예제 #26
0
What is the largest prime factor of the number 600851475143 ?
Solution by Tom Tillistrand
Solved for 13892 instead of 600851475143 because browser cannot handle larger number.
*/
$number = 13892;
$index = ceil($number / 2);
// The largest possible factor of any number is half of it, so we start our index here
$primeFound = false;
// Flag variable set to false initially. When prime factor is found, it's set to true to stop the loop
function isPrime($testNum)
{
    for ($i = 2; $i <= $testNum / 2; $i++) {
        if ($testNum % $i === 0) {
            return false;
        }
    }
    return true;
}
while (!$primeFound) {
    if ($number % $index === 0) {
        if (isPrime($index)) {
            echo "<p>The largest prime factor of 13892 is {$index}";
            $primeFound === true;
            break;
        }
    }
    $index--;
}
?>
	</body>
</html>
    <input type="text" id="startIndex" name="start"/>
    <label for="endIndex">End: </label>
    <input type="text" id="endIndex" name="end"/>
    <input type="submit" value="Submit"/>
</form>
<p>
    <?php 
for ($i = $start; $i <= $end; $i++) {
    ?>
            <?php 
    if (isPrime($i)) {
        ?>
<strong><?php 
        echo $i;
        ?>
</strong><?php 
    }
    if (!isPrime($i)) {
        echo $i;
    }
    if ($i < $end) {
        ?>
, <?php 
    }
    ?>
    <?php 
}
?>
</p>
</body>
</html>
예제 #28
0
        }
    }
    return true;
}
$primeNumbers = array();
for ($i = 1; $i <= 10000; $i++) {
    if (isPrime($i)) {
        array_push($primeNumbers, $i);
    }
}
$twiceSquares = array();
for ($i = 1; $i <= 10000; $i++) {
    array_push($twiceSquares, 2 * pow($i, 2));
}
for ($n = 35; $n <= 10000; $n += 2) {
    if (!isPrime($n)) {
        foreach ($twiceSquares as $sq) {
            if ($sq >= $n) {
                break;
            }
            foreach ($primeNumbers as $prime) {
                if ($prime >= $n) {
                    break;
                }
                if ($prime + $sq == $n) {
                    continue 3;
                }
            }
        }
        echo $n;
        break;
예제 #29
0
        }
        if ($num == 2) {
            return true;
        }
        if ($num % 2 == 0) {
            return false;
        }
        for ($i = 3; $i <= ceil(sqrt($num)); $i = $i + 2) {
            if ($num % $i == 0) {
                return false;
            }
        }
        return true;
    }
    for ($i = $startIndex; $i <= $endIndex; $i++) {
        $isPrime = isPrime($i);
        if ($isPrime === true) {
            if ($i == $endIndex) {
                ?>
                <b><?php 
                echo "{$i}";
                ?>
</b>
            <?php 
            } else {
                ?>
                <b><?php 
                echo "{$i}, ";
                ?>
</b>
            <?php 
    } else {
        for ($i = 2; $i <= (int) sqrt($number); $i++) {
            if ($number % $i == 0 && $i != $number) {
                $checker = false;
                break;
            } else {
                $checker = true;
            }
        }
    }
    if ($checker) {
        return "<span style='font-weight: bold'>{$number}</span>";
    } else {
        return "<span>{$number}</span> ";
    }
}
if (isset($_GET['start'], $_GET['end'])) {
    $startIndex = $_GET['start'];
    $endIndex = $_GET['end'];
    if (is_numeric($startIndex) && is_numeric($endIndex) && $endIndex > $startIndex) {
        for ($i = $startIndex; $i <= $endIndex; $i++) {
            if ($i == $endIndex) {
                echo isPrime($i);
            } else {
                echo isPrime($i) . ", ";
            }
        }
    } else {
        die('Please enter valid numbers!');
    }
}