<?php
function fibonacci($n) {
$a = 0;
$b = 1;
$c = 1;
for ($i = 1; $i < $n ; $i++) {
$c = $a + $b;
$a = $b;
$b = $c;
}
return $c;
}
echo fibonacci(1) ."\n";
echo fibonacci(3) ."\n";
echo fibonacci(100) ."\n";
// Tem um bug aqui, se usar fibonacci(0) vai dar 1.
// Como é só pra ilustrar, não vou complicar o código
// mas 1 "if" resolveria, se fosse o caso.
PD9waHAKCmZ1bmN0aW9uIGZpYm9uYWNjaSgkbikgewoJJGEgPSAwOwoJJGIgPSAxOwoJJGMgPSAxOwoJZm9yICgkaSA9IDE7ICRpIDwgJG4gOyAkaSsrKSB7IAoJCSRjID0gJGEgKyAkYjsKCQkkYSA9ICRiOwoJCSRiID0gJGM7CiAgICB9CiAgICByZXR1cm4gJGM7Cn0KZWNobyBmaWJvbmFjY2koMSkgLiJcbiI7CmVjaG8gZmlib25hY2NpKDMpIC4iXG4iOwplY2hvIGZpYm9uYWNjaSgxMDApIC4iXG4iOwoKCS8vIFRlbSB1bSBidWcgYXF1aSwgc2UgdXNhciBmaWJvbmFjY2koMCkgdmFpIGRhciAxLgoJLy8gQ29tbyDDqSBzw7MgcHJhIGlsdXN0cmFyLCBuw6NvIHZvdSBjb21wbGljYXIgbyBjw7NkaWdvCgkvLyBtYXMgMSAiaWYiIHJlc29sdmVyaWEsIHNlIGZvc3NlIG8gY2Fzby4gCg==