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"

57 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 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

newassignmenthelpaustralia said...

Get the best assignment help from Australia by experts at affordable prices. Get the best assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

Assignment Help ref. said...

Get the best marketing research assignment help from Australia by experts at affordable prices. Get the best marketing research assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

newassignmenthelpaustralia said...

Get the best strategic management assignment help from Australia by experts at affordable prices. Get the best strategic management assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

James Ian said...

Your article is attractive and informative. Thank you for sharing. Any student pursuing nursing should be passionate about it. Even though it is important to attain high scores, there’s a need to go the extra mile in delivery of services to the patients which is a skill that may not be learned in school. Also, nursing students need to understand nursing concepts and be able to implement them in real life. They have to carry out a lot of research especially when they have assignments to handle. Learn more from Nursing Paper Writers .

James Charlie said...

The blog on psychology is certainly resourceful, and well written. If you have been unable to find optimum time to conduct further research, you can head to seeking professional services at myassignmenthelpau.com. Feel free to explore the services and attain Assignment Help Ottawa.

Sam Smith said...

I have bookmarked your site for regular checkup. Did you know that One of the most recommended pieces of home exercise equipment is a treadmill? This is because it offers an effective aerobic workout for users at any skill or conditioning level. Professional runners and first-time exercisers alike can benefit from this machine, but electric models commonly found in commercial gyms can be prohibitively expensive, as well as intimidating to beginners. Learn more on Reviews on Manual Treadmills .

newassignmenthelpaustralia said...

Get the best project management assignment help from Australia by experts at affordable prices. Get the best project management assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

newassignmenthelpaustralia said...

Get the best english assignment help from Australia by experts at affordable prices. Get the best english assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

newassignmenthelpaustralia said...

Get the best python assignment help from Australia by experts at affordable prices. Get the best python assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

Emily Smith said...

The written piece is exclusive as it contains information which is unique to its own. If you ever need assistance with similar assignments, you can explore academic services offered by livewebtutors.com. Seek Law Assignment Help and be the topper in your class.

newassignmenthelpaustralia said...

Get the best business law assignment help from Australia by experts at affordable prices. Get the best business law assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

newassignmenthelpaustralia said...

Get the best social science assignment help from Australia by experts at affordable prices. Get the best social science assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

assignment help said...

Get the best networking assignment help from Australia by experts at affordable prices. Get the best networking assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

Nick Hebrew said...

Such a wonderful information blog post on this topic Allassignmentservices.com provides assignment service at affordable cost in a wide range of subject areas for all grade levels, we are already trusted by thousands of students who struggle to write their academic papers and also by those students who simply want make my assignment to save their time and make life easy.

newassignmenthelpaustralia said...

Get the best electrical assignment help from Australia by experts at affordable prices. Get the best electrical assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

assignment help said...

Get the best managerial economics assignment help from Australia by experts at affordable prices. Get the best managerial economics assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

assignment help said...

Get the best international finance assignment help from Australia by experts at affordable prices. Get the best international finance assignment writing services by professional assignment writers of Australia. On-Time Delivery. A+ Quality.

Silvia said...

I have benefited from the content in this blog. The literature review is a written overview of major writings and other sources on a selected topic. Sources covered in the review may include scholarly journal articles, books, government reports, Web sites, etc. The literature review provides a description, summary and evaluation of each source. It is usually presented as a distinct section of a graduate thesis or dissertation. Know more about Literature Review Homework Help

The Assignment House said...

Thanks for sharing. I found a lot of interesting information here. A really good post, very thankful and hopeful that you will write many more posts like this one.

assignment help
assignment help 4 me online
best paper checker
order essay online
turabian citation

david anderson said...

A student's life is a bustling issue with broad learning plans, social life, and extra-curricular responsibilities, and for a few; even part-time work. Offer your assignment stresses with us like a huge number of your companions have, from around the globe. We won't frustrate you. In our undertaking to offering the best Assignment Help online in Canada

Nursing Assignment Help said...

Get the best Nursing Assignment Help and services by professional assignment writers of Australia. The Assignment problem is a fundamental combinatorial optimization problem where the objective is to assign a number of resources to an equal number of activities so get the best Nursing Assignment Help from Australia by experts at affordable prices. On-Time Delivery. A+ Quality.

Marie Orpos said...

HP Wireless Printer Setup

Connecting a printer over a USB cable is pretty easy. But connecting a wireless printer can become very challenging quite often. Follow the HP wireless printer setup process to install it in no time.

1. Take the printer out of the box and remove all tapes from it before starting.
2. Plug in the power cord, turn on the printer and install the print cartridges. Allow your printer to complete its startup routine, which includes printing of an alignment page (if applicable).
3. Go to the manufacturer's website and download drivers for your printer model.
4. Install the drivers and follow the instructions during hp wireless printer setup process.
5. In the meantime, go to printer's control panel and use wireless printer setup wizard.
6. Identify your wifi network and connect the printer to it using its correct credential.
7. Complete the wireless printer setup process by connecting to your printer model as it would appear during installation


hp wireless printer setup

Marie Orpos said...

Canon printer support phone number

Canon wireless printer setup is very easy if compared with other available printers in the market. In case of any problem or confusion, canon printer support phone number is easy to get from the supplied manual. Follow canon printer setup process to install canon wireless printer.

1. Take the printer out of the box and remove all tapes from it before starting.
2. Plug in the power cord, turn on the printer and install the print cartridges. Allow your printer to complete its startup routine, which includes printing of an alignment page (if applicable).
3. Go to the manufacturer's website and download drivers for your printer model.
4. Install the drivers and follow the instructions during canon wireless printer setup process.
5. In the meantime, go to priter's control panel and use wireless printer setup wizard.
6. Identify your wifi network and connect the printer to it using its correct credential.
7. Complete the wireless printer setup process by connecting to your printer model as it would appear during installation.


canon printer support number

Marie Orpos said...

Brother Printer Setup

We are one of the most reliable, and independent 3rd party printer technical support and service providers, providing 24/7 online or on-call technical support services to the users of brother printer at a very nominal charge. Our printer experts have extensive knowledge to set up brother wireless printer in the simple ways. For brother printer setup, you can call our Brother printer support experts 24/7.

Brother Printer Support

Marie Orpos said...

Pogo.com/sign in problems

Check for correct User Name or Screen Name. Most of the time we rely on the browser’s auto-complete feature and select wrongly entered screen name in the past.
Passwords are case sensitive. Therefore, always check the letter’s cases of the entered password. If possible, type the password as you use in a notepad and then copy and paste the letters in the password field. Below is a list of things you should always check before entering into troubleshooting.
1. If any of your family member shares the same account, ask them if they might have changed the screen name or password.
2. Look into your email inbox to get the actual screen name when you had registered with Pogo
3. If you find any point relevant to your case, you can go to the article explaining how to reset password of your Pogo account and follow all the steps carefully to reset your pogo account password.

pogo.com/sign in problems

Marie Orpos said...

Gmail not working

When your email stops working or gets hacked, don’t panic. Call Email Support Phone Number and let the highly skilled Specialists get you up running.
Troubleshooting steps you can try to fix basic email problems:
1: Make sure you’re connected to the web.
2: Close the app, then reopen it.
3: Sign out, then sign back in.
4: Clearing the cookies and history of browser
5: Check your browser extensions or applications and remove unwanted extensions


Gmail not receiving email

assignmentauthors said...

Children are freer to talk to their Mrs. D.S than Mr. G.S. Most concerns are presented to the mother who would speak to their father hire an essay writer

Online Assignment Help said...

This particular field overlaps with several others from the humanities such as Political Science Assignment Help anthropology, biology, Public policy, and others. It overlaps with some other areas as well which do not fail under the category of humanities like information, architecture, mathematics etc

Investmango said...

Thank you for your outstanding article. Our goal is to offer top-class specialized services to you that create a fruitful, long-lasting relationship between us. Our excellent real estate portfolio management is the reason why our clients become like family. We provide the Great Deals and Discounts on Ace Divino 3 BHK Flats, 4 BHK Flats, Apartments, Penthouse, Corporate Properties, Commercial Properties in Noida, Greater Noida West, Delhi NCR. Learn more about Investmango: visit our website and call now: +918076042671

liyasmith200 said...

Great post! I have definitely noted some points from it. It feels crazy how a well-composed assignment can have a strong impact on the grades. To help you achieve the academic goals, the LiveWebTutors platform has curated an exclusive Finance Assignment Help Australia facility to help students enjoy their academic journey.

James Smith said...

Enjoy the bulldog campground sturgis sd with easy trail access to 700+ miles of motorized trails just behind BCC. If you haven't hit the trails before DON'T WORRY we have folks that would gladly take you along and show you some fo the trails.

liyasmith200 said...

Good and informative post. Java Assignment Help has always been a friend in need for the students. Thanks for informing me about LiveWebTutors and their expert services regarding the assignment. I will surely come back for more assistance.

Unknown said...

"Thank you so much for sharing all this wonderful info with us it is so appreciated "Buy L shape Sofa Online with trendy and Classy Styles only at custom Houzz .we have it all and we have them in a range of color options so that you can pick the one that goes best in your living room. With our range of colors, you’re sure to find something that matches your decor preferences well. Visit us to know more .

amarasmith said...

Thanks for sharing the blog it's very useful and informative. You also explained the topic very well. For any requirement of Geometry Homework Help in USA, you can connect with LiveWebTutors and find the best solutions to submit your assignment with premium writing quality.

Rose Black said...

Doing research is a big thing in itself and then presenting, assembling, and structuring the data is the biggest and difficult thing. If you have research paper helpers around you then it becomes easy to have a professional appearance and supervision. They will give you the needed suggestions be it adding thesis statements at the beginning or creating a proper outline. The best help with research paper is that they will make it creative and easy to understand.

Anonymous said...

Are you looking for Online Assignment Help in Australia? We are an assignment help service provider in Australia. Our expert Australian writers provide high quality assignment help Australia which is affordable. NAH also provides assignments in different subjects like cheap assignment help, assignment help Sydney, nursing assignment help, assignment help Melbourne etc.

Information said...

Great post! I hope you still do your work like this within the future, for more engaging posts! Gemini Login | Chime Login

linda said...

Collaborate for free with online versions of Microsoft Word, PowerPoint, Excel, and OneNote. Save documents, spreadsheets, and presentations online
Outlook 365 Login | Facebook Login

Jennifer said...

Hi! this is nice article you shared with great information. Thanks for giving such a wonderful informative information. I hope you will publish again such type of post. Also, please check out about MoonPay Login
PayPal Login
Venmo Login

chest of drawers said...

Rather than have a full-fledged cupboard that will crowd in the room, you can use a chest of drawers. A chest of drawers offers lots of extra storage and can also serve as a decorative stand too. Buy chest of drawers online at low prices in India at Custom Houzz.

news said...

Thanks for the amazing information. I really love to have more information like this soon.

netgear router login |

WordPress Login

linda said...

I like your post keep posting in future. ANIMEFLIX - Find English Anime Online Subbed, Dubbed and more. Choose from your favorite anime shows and watch one online on Animeflix
AnimeFlix | GoGoAnime

linda said...

To set up Roadrunner/Spectrum Webmail on your Android device, open up the '​Email' app on your device, go to 'Settings' and then select 'Add Account'.
Roadrunner Email | aka.ms/remoteconnect

news said...


This is a good post. This post gives truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. Thank you so much

Espn.com/activate |
ij.start.cannon

Assignment Writing Help said...

Are you looking for Assignment Help online? We are here to provide you the best assignment help in Australia at affordable prices. NAH also provides different types of Assignments like Cheap assignment help, Nursing assignment help, Assignment Help Melbourne, Assignment Help Sydney etc.
visit us: https://www.newassignmenthelpau.com/