Пример #1
0
<?php

include_once 'primefunctions.php';
$input = $_POST['input'];
$title = "Prime factors of {$input}";
include_once 'header.php';
echo "<center><h1>{$title}</h1>";
if ($input > 100000) {
    echo "Less of the huge numbers, you! Let's keep it below 100,000 for everyone's sake";
} else {
    $i = 0;
    $factors = prime_factors($input);
    foreach ($factors as $factor) {
        echo "{$factor} &nbsp;&nbsp;";
        $i++;
        if ($i == 5) {
            $i = 0;
            echo "<br />";
        }
    }
}
echo "<p><a href='index.php'>Return to main page</a></p>";
Пример #2
0
Файл: 5.php Проект: arjona/euler
<?php

$t = array();
for ($i = 2; $i <= 20; $i++) {
    $pfs = prime_factors($i);
    foreach ($pfs as $pf => $pfpow) {
        if ($t[$pf] < $pfpow) {
            $t[$pf] = $pfpow;
        }
    }
}
print_r($t);
// return an array where the key is the prime factor and the value the power
function prime_factors($num)
{
    $results = array();
    for ($f = 2; $f <= $num; $f++) {
        $exp = 1;
        while (fmod($num, $f) == 0) {
            $num = $num / $f;
            $results["{$f}"] = $exp++;
        }
    }
    return $results;
}
Пример #3
0
<?php

// the number of divisors of an integer d(n) can be obtained
// doing a prime factorization.
// just add 1 to every factor's exponent and multiply them
// Ex. 36 = (2^2) * (3^2) = 3 * 3 = 9
//     28 = (2^2) * (7^1) =  3 * 2 = 6
$t = 1;
$div = 1;
while ($div < 500) {
    $num = $num + $t;
    // triangle number
    $pf = prime_factors($num);
    $div = array_reduce($pf, 'mulplus1', 1);
    // apply mulplus1() to every element of the array
    echo "{$t} {$num} {$div}\n";
    $t++;
}
// return an array where the key is the prime factor and the value the power
function prime_factors($num)
{
    $results = array();
    for ($f = 2; $f <= $num; $f++) {
        $exp = 1;
        while (fmod($num, $f) == 0) {
            $num = $num / $f;
            $results["{$f}"] = $exp++;
        }
    }
    return $results;
}
Пример #4
0
<?php

require_once './model/model.php';
$ttime = microtime(true);
$stime = microtime(true);
$below = 600851475143;
$problem = <<<'XXX'
Largest prime factor<br><br>
Problem 3<br>
The prime factors of 13195 are 5, 7, 13 and 29.<br>
<br>
What is the largest prime factor of the number 600851475143 ?<br>
source: https://projecteuler.net/problem=3 <br>
XXX;
//Title
echo $problem;
echo "<br>";
//Method 1 - Recursion
start_method();
$lpf = prime_factors($below);
end_method();
result("recursive", "largest prime factor of the number {$below}", $lpf, "1", $stime);
//Method 2 - Haskell
start_method();
$lpf = prime_factors_haskell($below);
end_method();
result("recursive haskell", "largest prime factor of the number {$below}", $lpf, "2", $stime);
echo "Total time of php execution was " . (microtime(true) - $ttime) . " seconds <br>";