Skip to content

Instantly share code, notes, and snippets.

@reflexxion
Created August 2, 2023 12:03
Show Gist options
  • Save reflexxion/e19d9c35ae23349d3deb619fedc17cfb to your computer and use it in GitHub Desktop.
Save reflexxion/e19d9c35ae23349d3deb619fedc17cfb to your computer and use it in GitHub Desktop.
fibonacci.php
<?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