Exemplo n.º 1
0
function prime_factors($x)
{
    echo "<center>";
    $primes = primes_to($x);
    if (in_array($x, $primes)) {
        return array("{$x} is a prime number itself, and therefore can't be created by multiplying other prime numbers.");
    } else {
        echo "<p>If you multiply all the following numbers together, you get {$x}:</p>";
        $factors = array();
        $count = count($primes) - 1;
        $c = 0;
        while ($x > 1) {
            if ($x % $primes[$c] == 0) {
                $factors[] = $primes[$c];
                $x /= $primes[$c];
            } else {
                $c++;
            }
        }
        // end while
    }
    // end else
    return $factors;
}
Exemplo n.º 2
0
<?php

include_once 'primefunctions.php';
$input = $_POST['input'];
$title = "All primes up to {$input}";
include_once 'header.php';
echo "<center><h1>{$title}</h1>";
if ($input > 20000) {
    echo "<p>That's a really big number! Are you really sure you need all those primes? What are you planning to do with them?</p>";
} else {
    echo "<p>";
    $primes = primes_to($input);
    $i = 0;
    foreach ($primes as $prime) {
        echo "{$prime} &nbsp;&nbsp;";
        $i++;
        if ($i == 10) {
            $i = 0;
            echo "<br />";
        }
    }
    echo "</p>";
}
echo "<p><a href='index.php'>Return to main page</a></p>";