Calculation of the sequence of Fibonacci



  • In fact, the competition in the headline.

    Regulation:

    • The organizer of the competition is not involved.
    • Language of programming php

    Winning criteria

    • Win that algorithm that will calculate the longest sequence

    Testing

    • I suggest we use this service. http://sandbox.onlinephpfunctions.com/ For testing algorithms.


  • There are many ways of computing the number of Fibonacci, a definition can be used and calculated consistently. However, it seems to me that the most productive approach is first to create a function. fibanchi()the arbitrary number of Fibonacci in its number in sequence. The fastest way to accomplish this private task is the Binne formula.

    <?php
    function fibanchi($n) {
      return round((((1 + sqrt(5)) / 2) ** $n - ((1 - sqrt(5)) / 2) ** $n) / sqrt(5));
    }
    

    However, it is not enough that the formula itself is approaching, with the large numbers of Fibonacci, it is possible to move quickly beyond the borders of both a number and a number with a floating point. If the objective is the highest values of Fibonacci &apos; s numbers and accuracy, it is possible to calculate in the mass directly by definition long, but the values obtained will be very accurate (in this case, the numbers may belated 2000 and above, known to be inaccessible by Bina &apos; s calculation using scalar types).

    <?php
    function fibanchi($n) {
      // К-во разрядов
      $digit = intval($n / 4) + 5;
      $result = '';
      $a1 = [];
      $a2 = [];
      $a3 = [];
      $a1[0] = 1;
      $a2[0] = 1;
      for($j = 2; $j < $n; $j++)
      {
        for($i = 0; $i < $digit; $i++)
        {
          $a1[$i] = isset($a1[$i]) ? $a1[$i] : 0;
          $a2[$i] = isset($a2[$i]) ? $a2[$i] : 0;
          $b = intval(($a1[$i] + $a2[$i])/10);
          $a3[$i] = $a1[$i] + $a2[$i] - $b * 10;
          isset($a1[$i + 1]) ? $a1[$i + 1] += $b : $a1[$i + 1] = $b;
        }
        for($i = 0; $i < $digit; $i++)
        {
          $a1[$i] = $a2[$i];
          $a2[$i] = $a3[$i];
          $a3[$i] = 0;
        }
      }
      $flg = false;
      for($i = count($a2) - 1; $i >= 0; $i--)
      {
        if($a2[$i] >= 1) $flg = true;
        if($flg) $result .= $a2[$i];
      }
      return $result;
    }
    

    In order to achieve consistency, the cycle can be used

    <?php
    for($i = 0; $i < 20; $i++) {
      echo fibanchi($i)."<br />";
    }
    

Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2