Sunday, August 14, 2016

The Fibonacci Sequence in Six (plus) Languages

The Fibonacci Sequence in Six (plus) Languages
Python, C, Java, Node.js, Javascript, Powershell

I wanted to write a blog post that shows how to generate the Fibonacci sequence (0;1;1;2;3;5;8;13;21;34;55;...) in ten languages. Then I realized I would have to relearn some older language's syntax and that would mess up my current knowledge. So this is now down to five languages.

You should see output like:
First 10 Fibonacci numbers
1
2
3
5
8
13
21
34
55

Some of the scripts put the numbers on one line.

I had to encode all the less than and greater than's to keep the browser from interpreting them.



Python 2
I ran this in my Mac terminal with: python fibo.py

# fibonacci series generator for the first 10 numbers

# Seeds 0 and 1

x=0
y=1

print "First 10 Fibonacci numbers"

for i in range(1,10):
  t = x + y
  x = y
  y = t
  print t






Python 3
Then I rewrote it all in Python 3, and it uses fancy tuple unpacking which avoids needing a temporary variable. Putting in the end=" " keeps the series all on one line.

# fibonacci series generator for the first 10 numbers
# using tuple unpacking
# for Python 3

# Seeds 0 and 1

x=0
y=1

print ("First 10 Fibonacci numbers")

for i in range(1,10):
  print (y, end=" ")
  x, y = y, (x+y)





C
You need to compile this with a C compiler.

#include <stdio.h>

int main () 
{
  int x=0;
  int y=1;
  int i,t;
  printf ("First 10 Fibonacci numbers\n");

  for (i=0;i<10;i++)
  {
    t = x + y;
    x = y;
    y = t;
    printf ("%d ",t); 
  }
  printf("\n");

  return (0);
}



Java
Probably should have it be more object oriented, but that doesn't save any lines in this case.
You do have to have the Java dev kit installed on your system.
#include <stdio.h>

int main () 

{
  int x=0;
  int y=1;
  int i,t;
  printf ("First 10 Fibonacci numbers\n");
  for (i=0;i<10;i++)
  {
    t = x + y;
    x = y;
    y = t;
    printf ("%d ",t); 
  }
  printf("\n");
  return (0);
}



Node.js
Node is kind of like Javascript without needing a browser. It's a nice, refreshing language.
You have to have Node locally installed and then run it in the terminal with 
node fibo.js.

fibo.js:

 x=0
 y=1
 console.log ("First 10 Fibonacci numbers.")
 for (i=0;i<10 font="" i="" nbsp="">
 {
     t = x + y
     x = y
     y = t
     console.log (t);
 } 



Javascript Load this one into a browser. Getting this to even show up in the browser was a pain. Had to remove all the html and script tags. Tag: means the tag inside of angle brackets. Close tag: means use the closing tag which is the tag proceeded with a forward slash.
Tag: html
Tag: body
Tag: script
 x=0
 y=1
 document.write("First 10 Fibonacci numbers.")
 for (i=0;i<10;i++) 
 {
     t = x + y
     x = y
     y = t
     document.write(t," ");
 } 

Close tag: script
Close tag: body
Close tag: html


Powershell
$x = 0
$y = 1

$enditer = 10

for ($i=0;$i -lt $enditer;$i++) 
{
  $t = $x + $y
  write-host $t " "
  $x = $y
  $y = $t
}

write-host "End of series"