schaut's euch einfach mal an und sagt mir, was ihr besser machen würdet
danke
danke
PHP-Code:
$numbers = array();
function bitmask($bit) {
return -1 ^ (int)pow(2, $bit);
}
function add_number(&$target, $number) {
$index = floor($number / 32);
$number -= $index * 32;
if (!isset($target[$index]))
$target[$index] = 0;
$target[$index] |= (int)pow(2, $number);
}
function make_array(&$target, $min, $max) {
$full_entries = floor($max / 32);
for ($i = 0; $i < $full_entries; $i++)
$target[$i] = -1;
for ($i = $full_entries * 32; $i <= $max; $i++)
add_number($target, $i);
$min = max($min, 2);
for ($i = 0; $i < $min; $i++)
remove_number($target, $i);
}
function remove_number(&$target, $number) {
$index = floor($number / 32);
$number -= $index * 32;
$target[$index] &= bitmask($number);
}
function find_prime(&$numbers, $step = 2) {
if ($step > ceil(sqrt($_POST['max'])))
return;
for ($i = 2 * $step; $i <= $_POST['max']; $i += $step)
remove_number($numbers, $i);
find_prime($numbers, $step + 1);
}
function output_prime(&$numbers, $cols = 20) {
$primes = array();
for ($i = 0; $i < count($numbers); $i++)
for ($j = 0; $j < 32; $j++)
if ($numbers[$i] & (int)pow(2,$j))
$primes[] = $i * 32 + $j;
if (count($primes) == 0)
return '<p>Keine</p> :-(';
$primes = '<td>'.implode('</td><td>', $primes).'</td>';
$cols = str_repeat('\<td\>\d+\</td\>', $cols);
$primes = preg_replace('%('.$cols.')%', '<tr>$1</tr>', $primes);
return '<table border="1">'.$primes.'</table>';
}
if (isset($_POST['min'])) {
$_POST['min'] = max(0, (int)$_POST['min']);
$_POST['max'] = max((int)$_POST['max'], $_POST['min']);
if ($_POST['max'] > 1)
make_array($numbers, $_POST['min'], $_POST['max']);
find_prime($numbers);
echo '<h1>Primzahlen im Bereich '.
number_format($_POST['min'], 0, ',', '.').' - '.
number_format($_POST['max'], 0, ',', '.').'</h1>';
$_POST['cols'] = isset($_POST['cols']) ? max((int)$_POST['cols'], 20) : 20;
echo output_prime($numbers, $_POST['cols']);
}
$_POST['min'] = isset($_POST['min']) ? (int)$_POST['min'] : 0;
$_POST['max'] = isset($_POST['max']) ? (int)$_POST['max'] : 64;
$_POST['cols'] = isset($_POST['cols']) ? max((int)$_POST['cols'], 20) : 20;
echo '<h1>Intervall für Suche nach Primzahlen</h1>
<form action="'.$_SERVER['PHP_SELF'].'"method="post">
Untergrenze: <input type="text" name="min" value="'.$_POST['min'].'" /><br />
Obergrenze: <input type="text" name="max" value="'.$_POST['max'].'"/><br />
Spaltenzahl: <input type="text" name="cols" value="'.$_POST['cols'].'"/><br />
<input type="submit" name="" value="Abschicken" />
</form>';
Kommentar