fibonacci series in PHP

chris (2006-04-10 12:31:12)
6486 views
3 replies
This Fibonacci implementation is easy to convert into C or java or any language really. I've done it in PHP because it's quick and easy to read and understand. The secret to this is remembering to declare two inital values rather than just one so that you start of with a pair to sum. This is easily converted to java or C or whatever language you want to use.
<?php
   $first = 0;
   $second = 1;
   $n = 27;

   $inc = 1;

   do{
      $inc++;
      $final = $first + $second;
      $first = $second;
      $second = $final;
   }
   while ($inc < $n) ;

   print "nn$finalnn";
?>
comment
Sjan Evardsson
2009-05-21 19:36:11

How about Python

This Fibonacci implementation is easy to convert into C or java or any language really. I've done it in PHP because it's quick and easy to read and understand. The secret to this is remembering to declare two inital values rather than just one so that you start of with a pair to sum. This is easily converted to java or C or whatever language you want to use.


In Python:
first = 0
second = 1
n = 27
# we want to print the entire series, which start with 1, 1, 2, etc, neh?
print second
for i in range(n):
    next = first + second
    print next
    first = second
    second = next
reply iconedit reply
Sjan
2009-05-21 19:39:52

In Python

This Fibonacci implementation is easy to convert into C or java or any language really. I've done it in PHP because it's quick and easy to read and understand. The secret to this is remembering to declare two inital values rather than just one so that you start of with a pair to sum. This is easily converted to java or C or whatever language you want to use.


How about some Python?

first = 0
second = 1
n = 27
# to be correct we will print the entire series which starts off 1, 1, 2 ...
print second
for i in range(n):
    next = first + second
    print next
    first = second
    second = next
reply icon
anonymous
2009-05-22 19:10:46



How about some Python?

first = 0
second = 1
n = 27
# to be correct we will print the entire series which starts off 1, 1, 2 ...
print second
for i in range(n):
    next = first + second
    print next
    first = second
    second = next

mmm python is purdy. Thanks Sian :)
reply iconedit reply