Skip to content

Instantly share code, notes, and snippets.

@toopay
Created May 11, 2013 18:41
Show Gist options
  • Save toopay/5560937 to your computer and use it in GitHub Desktop.
Save toopay/5560937 to your computer and use it in GitHub Desktop.
<?php
class Fibonaci {
/**
* Penggunaan :
* $fib = new Fibonaci();
* echo 'Angka fibonaci ke-1:'.$fib->term(1); // 0
* echo 'Angka fibonaci ke-2:'.$fib->term(2); // 1
* echo 'Angka fibonaci ke-20:'.$fib->term(20); // 4181
*
* @param int Posisi
* @return int Nilai
*/
public function term($position) {
$num = array(0,1);
if ($position == 1) {
return $num[0];
} elseif ($position == 2) {
return $num[1];
} else {
for ($i=2; $i<=$position; $i++) {
$num[$i] = $num[$i-2] + $num[$i-1];
}
return $num[$position-1];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment