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"

12 comments:

Sharon Minsuk said...

Object oriented, schmobject oriented. There's a time and a place for everything, and this was not that. ;-)

Getting Javascript to show up: try substituting '<' for '<' to be able to make tags visible. Then wrap the whole thing with the 'pre' tag to get all your whitespace to show up without all those pesky  .

Sharon Minsuk said...

Bah. That was supposed to say, try substituting the character entity in place of '<' ! Blogger turned my character entity back into a <. :-)

Let's see if this works:

&<
&lt;

Ellen said...

I had to take out all the html in those sections and use the pre tag, but I have to remove all the html first.

Nathan William said...

Geography is a subject of learning and each student need to have sound knowledge into it. However, majority of the students face some issues in completing their assignments and end up scoring less marks in those. So, are you in need of a perfectly written Geography assignment that can fetch you highest scores? Here, at Myassignmenthelp.com you will get all kinds of solutions to your problems. So stop worrying now and get instant do my homework online service from professional experts of our website. Not only our essay assistance experts are highly experienced, but also they hold either a master’s or doctorate degree from eminent universities of UK and USA.

Assignment Help said...

Your article is so convincing that I never stop myself from appreciating this. Thank you for posting this. If you are looking for Online Assignment Help online services and if you want to boost your marks without putting your efforts. If you have time issues, do contact online assignment writing service provider and receive the best assignments as per your job requirements.

Max Willor said...

Very informative and well written post! Quite interesting and nice topic chosen for the post. My Assignment Help Au has been a pioneer within the Best Assignment Help on-line services and within the Australia and different countries ceaselessly for over Seven years. Make an order now for professional work, delivered on time and pure. You can email us at cs@Myassignmenthelpau.Com or Phone Number: +61-2-8005-8227.

printersupportnumber said...

Nice Post..
HP solution center is specially designed to resolve printer problems. This inbuilt program assists in managing the functionality of the HP printer and scanner. When you see that your Hp solution center has stopped working, our printer techies are technically capable of solving this error completely from the root. As a trustworthy third party support provider, our printer experts have the technical potential for resolving HP Solution Center stopped working issue within a few seconds. Still, if you have any queries, our printer support team is available 24/7 to assist you remotely.

Dan Smith said...

Thanks for posting this info. I just want to let you know that I just check out your site and I find it very interesting and informative. I can't wait to read lots of your posts. If you're looking for CDR Writing Services. You can email us at cs@needcdrreport.com or Phone Number: +61-2-8005-8227

Jenifer Discota said...

The most challenging thing in student life is when you run out of money and you have to hire writing service like cost accounting assignment help. Keeping this situation in mind our Academic Writing Experts provides the best assignment help service at most affordable price in Australia.

My Assignment Help Online said...

My Assignment help is the most preferred place to get assignment help Australia services where we offer 100% plagiarism free and money back guarantee ensuring complete satisfaction of students with every order. Our subject-specific experts are available 24x7 to provide properly formatted and referenced solutions in any subject from any level in Australian universities.

Assignment Help Australia said...

Students can take experts' assistance via Assignment Help services from the Australian service providers. Professional subject matter experts will help you to write your papers before the due dates even if you have issues of timing or quality content.
My Assignment Help
Assignment Help Online
Online Assignment Help
Assignment Help Australia

Olivia Crew said...

You had me engaged by reading and you know what? I want to read more about that. That's what's used as an amusing post.
read more:- Assignment Help Sydney