Blog gw

deret fibonaci

Posted by dillah2008 on November 15, 2008

Untuk menampilkan deret fibonaci sebanyak n buah maka bisa dipergunakan fungsi ini

function fibonaci($n){

$res = “”;
if($n> 0){

for($i=1;$i<=$n;$i++){

if($i > 2){

$temp[$i] = $temp[$i-1]+$temp[$i-2];
$res .= ($temp[$i-1]+$temp[$i-2]).”,”;

}else{

$temp[$i] = 1;
$res .= “1,”;

}

}

}
return $res;

}

Untuk bilangan fibonacci pada urutan ke-n

function get_fibonaci($n){

if($n == 1 || $n == 2){

return 1;

}else{

return (get_fibonaci($n-1) + get_fibonaci($n-2));

}

}

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <pre> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>