<?php
$ID = '$Id: prim.php 1011 2009-04-02 19:19:33Z bb $';
require '../app.inc.php';
require '../appmenu.inc.php';
$app = new Application('Primfaktor-Zerlegung', $menu);
$app->header();
$n = $_GET['n'];
?>
<form action=""><p>
<label for="n">Zu zerlegende Zahl grösser als 1:</label>
<input type="text" id="n" name="n" maxlength="8" size="8" value="<?php echo $n ?>" />
<input type="submit" value="Zerlegen!" />
</p></form>
<p>
<?php
$zahl = intval($n);
if ($zahl > 1) {
$teiler = 2; // Startwert für Teiler
$ende = intval(sqrt($zahl)); // Endwert für Teiler
echo $zahl;
$trenntext = " = ";
while ($zahl > 1 && $teiler <= $ende) { // solange nicht fertig
if ($zahl % $teiler == 0) { // wenns teilbar ist
echo $trenntext, $teiler; // ausgeben
$zahl /= $teiler; // und teilen
$trenntext = " * ";
}
else { // sonst
if ($teiler == 2) // den nächsten Teiler versuchen
$teiler = 3;
else
$teiler += 2;
}
}
if ($zahl > 1) { // wenn noch ein Rest da
if ($zahl == $n)
echo " ist eine <strong>Primzahl</strong>!";
else
echo $trenntext, $zahl; // den Rest ausgeben
}
}
elseif ($n != "") // Falsche Eingabe
echo "Ungültige Eingabe";
echo "</p>";
$app->footer();
?>