function fibo($n) { // return 1 ? 1 : 2; // echo "\$n is: $n\n"; $n = (int) $n; return $n < 2 ? 1 : fibo($n - 2) + fibo($n - 1); }
/** * A Fibonacci generator!!! * * This is a recursive implementation. Very slow on PHP, and MUCH faster * when compiled. * * We only need this function as a fallback, since we're compiling with the IR, * this won't be touched until the benchmark. * * @param int $n The number of the sequence to generate * * @return int the fibonacci number! */ function fibo($n) { if ($n < 2) { return 1; } return fibo($n - 1) + fibo($n - 2); }
/** * @ProfileGroup("fibo") * @ProfileCaption("fibo of #1") */ function fibo($x) { if ($x <= 1) { return $x; } else { return fibo($x - 1) + fibo($x - 2); } }
function fibos($n, $m = 1) { if ($n < $m) { return; } echo fibo($m), ' '; return fibos($n, $m + 1); }
function fibo($n) { if ($n <= 0) { return 0; } if ($n == 1) { return 1; } return fibo($n - 1) + fibo($n - 2); }
function fibo($n) { $n = (int) $n; // приведение типа к int if ($n <= 2) { return 1; } else { return fibo($n - 1) + fibo($n - 2); } }
function fibo($start, $next, $limit) { global $a; if ($limit <= $a) { return false; } $a++; echo $start + $next . PHP_EOL; fibo($next, $start + $next, $limit); }
static function fibo() { Timing::Start("fibo.php"); fibo(30); Timing::Stop(); }
function main_function() { ackermann(2); ary(500); ary2(500); ary3(5); fibo(13); hash1(100); hash2(20); heapsort(200); matrix(3); nestedloop(3); sieve(1); strcat(80); binary_trees(3); fannkuch(6); }
$t = end_test($t, "simpleucall"); simpleudcall(); $t = end_test($t, "simpleudcall"); mandel(); $t = end_test($t, "mandel"); mandel2(); $t = end_test($t, "mandel2"); ackermann(7); $t = end_test($t, "ackermann(7)"); ary(50000); $t = end_test($t, "ary(50000)"); ary2(50000); $t = end_test($t, "ary2(50000)"); ary3(2000); $t = end_test($t, "ary3(2000)"); fibo(30); $t = end_test($t, "fibo(30)"); hash1(50000); $t = end_test($t, "hash1(50000)"); hash2(500); $t = end_test($t, "hash2(500)"); heapsort(20000); $t = end_test($t, "heapsort(20000)"); matrix(20); $t = end_test($t, "matrix(20)"); nestedloop(12); $t = end_test($t, "nestedloop(12)"); sieve(30); $t = end_test($t, "sieve(30)"); strcat(200000); $t = end_test($t, "strcat(200000)");
<?php //nie wiedzialem jak to rozwiazac, wydaje mi sie ze wynik funkcji jest bledny function fibo($n) { if ($n <= 2) { return 1; } else { $a = 1; $b = 1; $c = 0; for ($i = 0; $i < $n - 2; $i++) { $c = $a + $b; $a = $b; $b = $c; } echo "{$n}, {$a}, {$b}"; } } fibo(8);
<?php // Write a function that computes the list of the first 100 Fibonacci numbers. // By definition, the first two numbers in the Fibonacci sequence are 0 and 1, // and each subsequent number is the sum of the previous two. // As an example, here are the first 10 Fibonnaci numbers: // 0, 1, 1, 2, 3, 5, 8, 13, 21, and 34. function fibo($n) { $fibo = []; $a = 0; $b = 1; $index = 1; while ($index <= $n) { $c = $a + $b; $a = $b; $b = $c; $fibo[] = $c; $index++; } return $fibo; } $result = fibo(100); echo implode(', ', $result);
<?php #1000-digit Fibonacci number - Problem 25 ini_set('max_execution_time', 600); $time_start = microtime(true); ////////////////////////////////////////////////////// $a = 1; $b = 2; function fibo($n, &$a, &$b) { $f = bcadd($a, $b); $a = $b; $b = $f; return $f; } for ($i = 4; $i <= 100000000; $i++) { $f = fibo($i, $a, $b); if (strlen($f) >= 1000) { echo "Fn is {$i}"; break; } } ////////////////////////////////////////////////////////////////////// // End of execution time calculation $time_end = microtime(true); $execution_time = $time_end - $time_start; //total execution time ///////////////////////////////////////////////////////////////////// //Below Outputs the execution time in seconds echo '<br /><br /><b>Total Execution Time:</b> ' . $execution_time . ' seconds';
function fibo($n) { return $n < 2 ? 1 : fibo($n - 2) + fibo($n - 1); }
<?php //echo"Enter the number to get no. of fibonacci series"; function fibo($num) { $num1 = 0; $num2 = 1; if ($num == 0) { return 0; } if ($num == 1) { echo "{$num2}"; } for ($i = 0; $i <= $num; $i++) { $sum = $num1 + $num2; $num1 = $num2; $num2 = $sum; echo $sum . '</br>'; } } echo fibo(6);
function main_function() { for ($i = 0; $i < 100; ++$i) { ackermann(4); ary(50000); ary2(50000); ary3(100); fibo(23); hash1(10000); hash2(200); heapsort(2000); matrix(3); nestedloop(8); sieve(5); strcat(80000); binary_trees(7); fannkuch(6); } }
/** * Created by PhpStorm. * User: antonio * Date: 30/07/15 * Time: 21:30 * Crie uma pasta “p2_exercicio7” e um arquivo index.php. Dentro dele crie um programa para mostrar a sequência fibonacci até o 100º elemento, * para isso, crie uma função fibonacci que deverá receber o índice do elemento e a função irá calcular o número correspondente daquele índice. */ function fibo($n) { return $n < 2 ? $n : fibo($n - 1) + fibo($n - 2); }
<?php function fibo($limit) { $temp1 = 1; // тут хранится прошое число $temp2 = 0; // тут хранится позапрошое число for ($i = 0; $i < $limit; $i++) { $result = $temp1 + $temp2; // сума прошлого и позапрошлого числа print $result . "<br>"; $temp2 = $temp1; // записыаем то число которое было слогаемым для нынешней итерации, в "позапрошлое" для следующей $temp1 = $result; // записываем число которое было результатом для нынешней итерации, в "прошлое" для следуещей } } fibo(10);
return $t['sec'] + $t['usec'] / 1000000; } function start_test() { ob_start(); return getmicrotime(); } function end_test($start, $name) { global $total; $end = getmicrotime(); ob_end_clean(); $total += $end - $start; $num = number_format($end - $start, 3); $pad = str_repeat(" ", 24 - strlen($name) - strlen($num)); echo $name . $pad . $num . "\n"; ob_start(); return getmicrotime(); } function total() { global $total; $pad = str_repeat("-", 24); echo $pad . "\n"; $num = number_format($total, 3); $pad = str_repeat(" ", 24 - strlen("Total") - strlen($num)); echo "Total" . $pad . $num . "\n"; } $t0 = $t = start_test(); fibo(); $t = end_test($t, "fibo(30)");
/** * @ProfileGroup("Fibo Group") * @ProfileCaption("Caption of fibo, value #1 #1 #1 #1") * @param type $x * @return int */ function fibo($x) { if ($x < 2) { return 1; } else { return fibo($x - 1) + fibo($x - 2); } }
<?php function fibo_r($n) { return $n < 2 ? 1 : fibo_r($n - 2) + fibo_r($n - 1); } function fibo($n) { $r = fibo_r($n); print "{$r}\n"; } // PHP takes ages to run this. Original was 30. fibo(4);