当前位置: 动力学知识库 > 问答 > 编程问答 >

for loop - PHP Fibonacci Sequence

问题描述:

This php method is suppose to print the Fibonacci sequence up to a specified value using a for loop. I am unsure why it does not work?

<?php

function fib ($n) { // a function called fib, declaire variable n (the sequence number)

for ($n=0;$n<30;$n++) {

if ($n < 3) { return $n; } // if n is smaller than 3 return n (1 or 2)

else { return fib ($n - 1) + fib ($n - 2); }

/* if the number is 3 or above do 2 sums (n-1) and (n-2)

and then add the 2 sums together (n-1)+(n-2)

Example Fibonacci number 4

(4-1)+(4-2) = 5

3 + 2 = 5

*/

}

print $n;

?>

网友答案:

There is actually a way to calculate a fibbonacci number without iteration by using rounding:

http://en.wikipedia.org/wiki/Fibonacci_number#Computation_by_rounding

function getFib($n)
{
    return round(pow((sqrt(5)+1)/2, $n) / sqrt(5));
}
网友答案:

Simple function of fibonacci

function fibonacci($n,$first = 0,$second = 1)
{
    $fib = [$first,$second];
    for($i=1;$i<$n;$i++)
    {
        $fib[] = $fib[$i]+$fib[$i-1];
    }
    return $fib;
}
echo "<pre>";
print_r(fibonacci(50));
网友答案:

In this example, I am using a for loop and limiting the length to 10:

$x = 0;    
$y = 1; 

for($i=0;$i<=10;$i++)    
{    
    $z = $x + $y;    
    echo $z."<br />";         
    $x=$y;       
    $y=$z;     
}   

Output:
1
2
3
5
8
13
21
34
55
89
144

网友答案:

Your basic understanding of how the language works seems wrong. You're defining a function but never calling it. You are giving a parameter that you then immediately overwrite.

Try this:

$fib = [1,0];
for($i=0; $i<30; $i++) {
    $next = array_sum($fib);
    array_shift($fib);
    array_push($fib,$next);
    echo $next.", ";
}
网友答案:

You are overwriting the $n you get as function argument when you let $n to be the loop counter.

Get rid of the for-statement, leave just the body of it.

网友答案:
function fib($m){
    $j=0;$k=1;
    for($i=0;$i<=$m;$i++){
          $l=$k;
          $k=$j+$k;
          $j=$l;
      }
   return $k;
}
网友答案:
    function fibonacci( $n ) {
if (( $n == 0 ) || ( $n == 1 )) {
     return $n;
}
return fibonacci( $n-2 ) + fibonacci( $n-1 );}
网友答案:

This is the method used by me:

function fibonacci_series($n) {
  $f1 = -1;
  $f2 = 1;

  for ($i = 1; $i <= $n; $i++) {
    $f = $f1 + $f2;
    $f1 = $f2;
    $f2 = $f;
    echo "$f<br />"; 
  }
 }

echo fibonacci_series(5);

For explanation visit here

网友答案:

I know I am 1 year late, but here is my contribution. There are 2 possible solutions:

function fib1($n)
{
    return $n < 3 ? 1 : fib1($n - 1) + fib1($n - 2);
}
function fib2($n, $c = 2, $n2 = 0, $n1 = 1)
{
    return $c < $n ? fib2($n, $c + 1, $n1, $n1 + $n2) : $n1 + $n2;
}

The first one is more elegant and calculates the result same way as it is defined mathematically. The problem is it is very expensive for big $n.

The second one is much faster, but you have to make sure user sets only the first parameter.

Execution time:

$n = 20, fib1 = 0.092s, fib2 = 0.001s

$n = 30, fib1 = 12.2827s, fib2 = 0.001s

$n = 40, fib1 = >500s fib2 = 0.001s

网友答案:
function getFib($n, $nN, $ct = 0, $max = 100)
 {
     $ct++;
     echo "$nN \n";
     ($ct < $max) ? getFib($nN, $n+$nN, $ct) : die();

 }

 echo 0;
 getFib(0, 1);

Recursive Fib

网友答案:

Fibonacci Series looks like:

0 , 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , 34 ,

function fabnacaiiSeries($num){
    $first=0;$second=1;
    for($i=0;$i<$num;$i++){
        if($i<=1){
            $next=$i;
        }  else  {
            $next=$first+$second;
            $first=$second;
            $second=$next;
        }

        echo  $next." , ";
    }   
}

fabnacaiiSeries(10);

View more detail

网友答案:

This is inefficient fib ($n - 1) + fib ($n - 2); here are my solutions with recursion and without. I'm not using accumulated dictionaries like $fib[1..N] = [] but I calculate step sum and pass it to next iteration or recursion call.

// with recursion 
function fib($n, $a = 0, $b = 1) {
    if ($n < 1) {
        return $a;
    }        

    return fib($n - 1, $b, $a + $b);
}


// with simple loop
function fib($N) {
    $a = 0;
    $b = 1;
    for ($i = 0; $i < $N; $i++) {
        $t = $a + $b;
        $a = $b;
        $b = $t;
    }
    return $a;
}
网友答案:

Try this. Optimized function

function fib($n, $all=0){
f[0] = 1;
f[1] = 1;
for($i = 2; $i<= $n; $i++) f[$i] = f[$i-1]+f[$i-2];
if($all) return $f;
return $f[$n];
}

print fib(5);
var_dump(fib(5, 1));  
网友答案:

If you want to do it recursively

function fib($n, $first, $second) {
    if( $n > 0 ) {
         $sum = $first + $second;
         $first = $second;
         $second = $sum;
         print $sum;
         fib($n-1, $first, $second);
    }
}
网友答案:
$count = 0;
$x = 0;
$y = 1;

echo $x."<br />";
echo $y."<br />";

while($count < 10) {
    $z = $x + $y;
    echo $z."<br />";
    $x = $y;
    $y = $z;
    $count ++;
}

Refer: http://blog.innovsystems.com/php/fibonacci-series-program-php

网友答案:

Solution: Working PHP code for fibonacci sequence

$prev = 0;
$curr = 1;
$fib = 0;
for($i=0;$i<=20;$i++){
    echo $fib . ",";               
    $prev = $curr;
    $curr = $fib;
    $fib = $prev + $curr; 
}
网友答案:

This one is as fast and as simple as I could make it

function fibonacci($n, $rec = 0) {
    echo "$n\t";
    fibonacci($n + $rec, $n);
}
echo fibonacci(1);

Include header('Content-Type: text/plain'); to see the \t tabs.

网友答案:

I came across this question recently and found your question. You were half-way there then I modified your code to working version.

<?php

function fib ($n) { // a function called fib, declaire variable n (the sequence number)
    if($n==0) return 0;
    if ($n < 3) { return 1; } // if n is smaller than 3 return n (1 or 2)
    else { return fib ($n - 1) + fib ($n - 2); }
    /* if the number is 3 or above do 2 sums (n-1) and (n-2)
    and then add the 2 sums together (n-1)+(n-2)
    Example Fibonacci number 4
    (4-1)+(4-2) = 5
    3 + 2 = 5
    */

}

        echo fib(50);
网友答案:

A more readable, non-recursive approach:

function fibonacciSequence($max = 13) {
    if($max < 1)
        return [];

    $sequence = [];

    $older = 0;
    $newer = 1;

    do {
        $number = $newer + $older;
        $sequence[] =  $number;

        $older = $newer;
        $newer = $number;
    } while($number < $max);

    return $sequence;
}
网友答案:
    <?php
    $b=1;
    for($a=$b++;;$b+=$a=$b-$a)    
    {    
       echo"$a<br>"; 
    if($a>500) {die();}   //there must be a LIMIT!!!
    }   

    ?>

output: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610

分享给朋友:
您可能感兴趣的文章:
随机阅读: