Files
ITL2-Primzahlen/index.php
2025-11-17 14:53:33 +00:00

87 lines
2.4 KiB
PHP

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Primzahlen</title>
<style>
.wrapper {
display: flex;
gap: 20px;
}
.mono {
font-family: monospace;
white-space: pre;
}
</style>
</head>
<body>
<h1>Primzahlenberechnung</h1>
<div class="wrapper">
<div>
<form action="index.php" method="post">
<label>Input:
<input type="number" name="input" min="2" required>
</label>
<button type="submit">Berechnen</button>
</form>
<?php
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['input'])) {
$max = (int) $_POST['input'];
if ($max < 2) {
echo "<p>Bitte eine Zahl >= 2 eingeben.</p>";
} else {
$primeNumbers = [];
for ($i = 2; $i <= $max; $i++) {
if (isPrime($i)) {
$primeNumbers[] = $i;
}
}
$safe = htmlspecialchars((string)$_POST['input'], ENT_QUOTES, 'UTF-8');
echo "<h3>Primzahlen von 1 bis {$safe}:</h3>";
echo "<p>" . implode(", ", $primeNumbers) . "</p>";
}
}
function isPrime(int $n): bool {
if ($n <= 1) return false;
if ($n <= 3) return true;
if ($n % 2 === 0) return false;
$limit = (int) floor(sqrt($n));
for ($d = 3; $d <= $limit; $d += 2) {
if ($n % $d === 0) return false;
}
return true;
}
?>
</div>
<div>
<h3>Logarithmische Funktion (n = 1..100)</h3>
<div class="mono">
<?php
for ($n = 1; $n <= 100; $n++) {
$ln = log($n);
$rounded = round($ln * 2) / 2;
$full = (int) floor($rounded);
$half = ($rounded - $full) >= 0.5 ? 1 : 0;
if ($full === 0 && $half === 0) {
$visual = '-';
} else {
$visual = str_repeat('*', $full) . ($half ? '+' : '');
}
echo str_pad($n, 3, ' ', STR_PAD_LEFT) . ": " . $visual . "<br>";
}
?>
</div>
</div>
</div>
</body>
</html>