Created
August 2, 2023 12:03
-
-
Save reflexxion/e19d9c35ae23349d3deb619fedc17cfb to your computer and use it in GitHub Desktop.
fibonacci.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Maximale Anzahl ermittelter Zahlen | |
$limit = 10; | |
$list = []; | |
while(count($list) < $limit) { | |
$currentValue = 0; | |
if (count($list) === 0) { | |
// Die Liste ist noch leer – setze den Wert auf 0 | |
$currentValue = 0; | |
} elseif (count($list) === 1) { | |
// Die Liste hat nur einen Eitnrag – setze den Wert auf 1 | |
$currentValue = 1; | |
} else { | |
//$currentValue = $list[count($list)-1] + $list[count($list)-2]; | |
$currentValue = array_sum(array_slice($list, -2)); | |
} | |
$list[] = $currentValue; | |
echo sprintf('Die %d. Fibonacci-Zahl lautet: %d', count($list), $currentValue). PHP_EOL; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment