#native_company# #native_desc#
#native_cta#

Fibonacci Sequence

By Ville Juusto
on July 20, 2006

Version: 1.1

Type: Function

Category: Math Functions

License: GNU General Public License

Description: There are two functions. One does it by recursion the other with a while loop.

This script can be run from the shell as follows:

/path/to/php fibo.php 10

will generate:

*************************************

Fibonacci Sequence with recursive function:
1,1,2,3,5,8,13,21,34,55,89

Fibonacci Sequence with non-recursive function:
1,1,2,3,5,8,13,21,34,55,89

*************************************

Have fun!

/*
				calculates two strings to a new string ... calc("1234", "1234") -> "2468"
			*/
			function calc($a, $b){
				$result = "";//final result
				$len_1 = strlen($a);
				$len_2 = strlen($b);
				if($len_1 > $len_2){
					$up = $len_1;
				}else{
					$up = $len_2;
				}
				$a =  strrev ( $a );
				$b =  strrev ( $b );
				
				$arr1 = str_split($a);
				$arr2 = str_split($b);
				
				for($i = 0;$i < ($up+1);$i++){
					$numb = $jj + $arr1[$i] + $arr2[$i];
					if($numb >= 10){
						$jj = 1;
						$numb = ($numb % 10);
					}else{
						$jj = 0;
					}
					$result = $numb."".$result;
				}
				return ltrim($result, "0");
			}
			
			$fibo_1 = "1";
			$fibo_2 = "1";
			
			for($i = 1;$i <= 201;$i++){
				$new = calc($fibo_1, $fibo_2);
				echo "".$new."";
				if($i % 2 == 0){
					$fibo_1 = $new;
				}else{
					$fibo_2 = $new;
				}
			}