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