Files
2p_pracownia_programowania_php/Zadania/Z75b - liczby fibonacciego rek/index.php
2025-11-16 19:21:21 +01:00

52 lines
1.5 KiB
PHP

<!doctype html>
<html lang="pl">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Z75b - Liczby Fibonacciego rekurencyjnie</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<header>
<h1>Zadanie Z75b</h1>
<h2>Autor: Jakub Grzegorczyk</h2>
</header>
<div class="box">
<p>Napisz program, który dla danej liczby całkowitej n wypisuje wyrazy ciągu Fibonacciego według zależności</p>
<img src="075.png" alt="Zależność Fibonacciego">
<p>Dla n=20 program powinien wypisać: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765</p>
</div>
<div class="box">
<form action="index.php" method="post">
<fieldset>
<label for="n">Podaj Liczbę N:</label>
<input type="number" name="n" id="n">
</fieldset>
<button type="submit">Wyślij</button>
</form>
</div>
<?php
function fib($n) {
if ($n === 0) return 0;
else if ($n === 1) return 1;
else return fib($n-1) + fib($n-2);
}
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$n = htmlspecialchars($_POST['n']);
echo '<div class="box">';
$ciag_fib = array();
for ($i = 0; $i <= $n; $i++) {
$ciag_fib[] = fib($i);
}
for ($i = 0; $i < sizeof($ciag_fib); $i++) {
echo 'F(' . $i . ') = ' . $ciag_fib[$i] . ' <br>';
}
echo '</div>';
}
?>
</body>
</html>