DEV Community

Teddy Zugana
Teddy Zugana

Posted on

PHP Recursive Fibonacci

<?php
  $inputdata = @$_POST[‘inputdata’];  if(empty($inputdata))
  {
   $inputdata = @$_GET[‘inputdata’];
  }  

  $result = array();
  if(is_numeric($inputdata))
  {
   if(!empty($inputdata))
   {

    $result = array();

    for ($i = 0; $i < $inputdata; $i++)
    {  
        $dataseriestemp=series($i); 
        if($dataseriestemp==0)
        {
        }
        else if($dataseriestemp%2==1)
        {
          $result[]=$dataseriestemp;
        } 
     } 
   }
 }

function series($num)
{ 
 if($num == 0)
 { 
   return 0; 
 }
 else if($num == 1)
 { 
   return 1; 
 } 
 else 
 { 
   return (series($num-1) + series($num-2)); 
 } 
}

rsort($result);
header(‘Content-Type: application/json’);
echo json_encode($result);
?>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)