skip to Main Content

I’m trying to understand a specific pattern where each term is the sum of the previous term and the current Fibonacci number. The pattern follows this sequence:

1
1 + 1 = 2 
1 + 1 + 2 = 4 
1 + 1 + 2 + 3 = 7 
1 + 1 + 2 + 3 + 5 = 12 

And so on

I would like to create a function in PHP that generates this pattern. I have attempted to modify a standard Fibonacci function, but the results are not as expected. Here’s the code I’ve tried so far:

function getResult($n) {
  $result = [];
  $previous = 0;
  $current = 1;

  for ($i = 1; $i <= $n; $i++) {

    $result[] = $current;
    
    $previous = $current; 
    
    $next = $previous + $current;
    
    $current = $next;
  
    
  }

  return $result;
}

2

Answers


  1. Put $previous = $current; after $next = $previous + $current;

    Live JavaScript snippet just to illustrate the result:

    function getResult ($n) {
      let $result = [];
      let $sum = 0;
      let $previous = 0;
      let $current = 1;
      let $next;
    
      for (let $i = 1; $i <= $n; $i++) {
        $result.push($current);
        $sum += $current;
        $next = $previous + $current;
        $previous = $current;
        $current = $next;
      }
    
      return { $result, $sum };
    }
    
    for (let $n = 1; $n <= 10; $n++) {
      let { $result, $sum } = getResult($n);
      console.log(`${$result.join(' + ')} = ${$sum}`);
    }

    Update: Or did you mean you want to generate the sequence 1 2 4 7 12 20 33?

    Then this is basically Sn = Fn+2-1:

    function getResult ($n) {
      let $result = [];
      let $previous = 1;
      let $current = 2;
      let $next;
    
      for (let $i = 1; $i <= $n; $i++) {
        $result.push($current - 1);
        $next = $previous + $current;
        $previous = $current;
        $current = $next;
      }
    
      return $result;
    }
    
    for (let $n = 1; $n <= 10; $n++) {
      console.log(getResult($n).join(', '));
    }
    Login or Signup to reply.
  2. For Fibonacci Sequent (0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144…..), you may use the following PHP code to get:

    <?php  
    
    function getresult($x) {
      $num = 0;  
      $n1 = 0;  
      $n2 = 1;  
      $n3 = 0;  
      $n4 = 0; 
    
    if ($x==0) { return $n1; }
    if ($x==1) { return $n2; }
    
    
    while ($num < ($x-1) )  {  
        $n3 = $n2 + $n1;  
        $n4=$n3;
        $n1 = $n2;  
        $n2 = $n3;  
        $num = $num + 1;  
    }
    
    if ($x>=2){
      return $n4;   
      }
    }   
    
    //How to use:
    //echo getresult(2);
    
    ?>  
    

    So, for your case which you want to get the sum of the previous terms and the current Fibonacci number (1 2 4 7 12 20 33 54 88 143 …), you can use the following:

    <?php  
    
    function getresult($x) {
      $num = 0;  
      $n1 = 0;  
      $n2 = 1;  
      $n3=0;  
      $n4=0; 
    
    if ($x==0) { return $n1; }
    if ($x==1) { return $n2; }
    
    
    while ($num < ($x-1) )  {  
        $n3 = $n2 + $n1;  
        $n4=$n3;
        $n1 = $n2;  
        $n2 = $n3;  
        $num = $num + 1;  
    }
    
    if ($x>=2){
      return $n4;   
      }
    
    
    }   
    
    
    // show Fibonacci series; 
    for($local=0; $local < 10; $local++) {
           echo getresult($local) . ' ' ;       
    }
    
    
    function getresult2($fi)
    {
        $local2=0;
        
      for($local=0; $local < $fi; $local++) {
           $local2 +=getresult($local) ;        
      }
        return $local2; 
        
    }
    
    
    echo "nn";
    
    $fi=10;
    for($local=2; $local < $fi+2; $local++) {
           echo getresult2($local) . ' ' ;      
    }
    
    
    ?>  
    

    See DEMO

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search