Fixed up a few last things.
This commit is contained in:
57
index.php
57
index.php
@@ -2,15 +2,11 @@
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Primzahlen</title>
|
||||
<style>
|
||||
.wrapper {
|
||||
display: flex;
|
||||
flex-direction: row;
|
||||
gap: 20px;
|
||||
|
||||
}
|
||||
.wrapper { display: flex; gap: 20px; }
|
||||
.mono { font-family: monospace; white-space: pre; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
@@ -19,38 +15,38 @@
|
||||
<div>
|
||||
<form action="index.php" method="post">
|
||||
<label>Input:
|
||||
<input type="number" name="input">
|
||||
<input type="number" name="input" min="2" required>
|
||||
</label>
|
||||
<button type="submit">Berechnen</button>
|
||||
</form>
|
||||
|
||||
<?php
|
||||
if(isset($_POST['input'])) {
|
||||
$primeNumbers = [];
|
||||
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['input'])) {
|
||||
$max = (int) $_POST['input'];
|
||||
|
||||
for($i = 0; $i <= $max; $i++) {
|
||||
if ($max < 2) {
|
||||
echo "<p>Bitte eine Zahl >= 2 eingeben.</p>";
|
||||
} else {
|
||||
$primeNumbers = [];
|
||||
for ($i = 2; $i <= $max; $i++) {
|
||||
if (isPrime($i)) {
|
||||
$primeNumbers[] = $i;
|
||||
}
|
||||
}
|
||||
|
||||
echo "<h3>Primzahlen von " . htmlspecialchars($_POST["input"], ENT_QUOTES, 'UTF-8') . " sind:</h3>";
|
||||
|
||||
foreach($primeNumbers as $number) {
|
||||
echo "<span>" . $number . "</span><br>";
|
||||
$safe = htmlspecialchars((string)$_POST['input'], ENT_QUOTES, 'UTF-8');
|
||||
echo "<h3>Primzahlen von 1 bis {$safe}:</h3>";
|
||||
echo "<p>" . implode(", ", $primeNumbers) . "</p>";
|
||||
}
|
||||
}
|
||||
|
||||
function isPrime($i) {
|
||||
if ($i <= 1) return false;
|
||||
if ($i <= 3) return true;
|
||||
if ($i % 2 == 0) return false;
|
||||
function isPrime(int $n): bool {
|
||||
if ($n <= 1) return false;
|
||||
if ($n <= 3) return true;
|
||||
if ($n % 2 === 0) return false;
|
||||
|
||||
for ($j = 3; $j * $j <= $i; $j += 2) {
|
||||
if ($i % $j == 0) {
|
||||
return false;
|
||||
}
|
||||
$limit = (int) floor(sqrt($n));
|
||||
for ($d = 3; $d <= $limit; $d += 2) {
|
||||
if ($n % $d === 0) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
@@ -64,12 +60,17 @@
|
||||
<?php
|
||||
for ($n = 1; $n <= 100; $n++) {
|
||||
$ln = log($n);
|
||||
$lnFormatted = number_format($ln, 4, '.', '');
|
||||
$rounded = round($ln * 2) / 2;
|
||||
$full = (int) floor($rounded);
|
||||
$half = ($rounded - $full) >= 0.5 ? 1 : 0;
|
||||
|
||||
$starsCount = (int) floor($ln);
|
||||
$stars = $starsCount > 0 ? str_repeat('*', $starsCount) : '-';
|
||||
if ($full === 0 && $half === 0) {
|
||||
$visual = '-';
|
||||
} else {
|
||||
$visual = str_repeat('*', $full) . ($half ? '+' : '');
|
||||
}
|
||||
|
||||
echo sprintf("%3d: ln=%6s %s<br>", $n, $lnFormatted, $stars);
|
||||
echo str_pad($n, 3, ' ', STR_PAD_LEFT) . ": " . $visual . "<br>";
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user