PHP 斐波那契数列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15600041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:33:54  来源:igfitidea点击:

PHP Fibonacci Sequence

phpfor-loop

提问by Jesse Luke Orange

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 方法假设使用 for 循环将斐波那契数列打印到指定值。我不确定为什么它不起作用?

<?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;
?>

回答by Lee Davis

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

实际上有一种方法可以通过使用舍入来计算无需迭代的斐波那契数:

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

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

function getFib($n)
{
    return round(pow((sqrt(5)+1)/2, $n) / sqrt(5));
}

回答by user2669208

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));

回答by Sathish Jayaraman

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

在这个例子中,我使用了一个 for 循环并将长度限制为 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

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

回答by Abdallah Awwad Alkhwaldah

you have 2 sulutions

你有 2 个解决方案

using for loop

使用 for 循环

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

print_r(fib(6));

recursive functions

递归函数

function rec_fib($x) {
    if ($n < 2) {
        return $n;
    }
    return fib($n - 1) + fib($n - 2);
}

print_r(rec_fib(6));

回答by Niet the Dark Absol

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.", ";
}

回答by Oswald

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

$n当你让它$n成为循环计数器时,你正在覆盖你作为函数参数得到的。

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

去掉 -for语句,只留下它的正文。

回答by Petr

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.

第一个更优雅,并以与数学定义相同的方式计算结果。问题是大 $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

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

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

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

回答by Josh Woodcock

function fib($m){
    $j=0;$k=1;
    for($i=0;$i<=$m;$i++){
          $l=$k;
          $k=$j+$k;
          $j=$l;
      }
   return $k;
}

回答by Guru

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

有关说明,请访问此处

回答by Pedro Moreira

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;
}