[PYTHON] [Bilingual, it's a waste] Which is your favorite Fibonacci?

I am always indebted to you. I'm drumath, a high school programmer. This time I will write an article about the Fibonacci sequence. Maybe someone who is just starting programming Some people may be wondering, "I want to start programming, but which language should I start with?" In this article, let's implement the same algorithm in eight languages: C / C ++, Java, Python, Ruby, PHP, Perl, JavaScript, and CoffeeScript! So maybe we can help you choose your language.

About the Fibonacci sequence

I met when I was taking the entrance exam for junior high school.

1,1,2,3,5,8,13,21 ...

What numbers follow this? At first, everyone will be troubled by the question. The correct answer is 34. When I heard the explanation of the sum of the previous two terms, I found it really mathematical and interesting. However, the true appearance of the Fibonacci sequence was truly "mathematical attraction". I learned about it two years later when I read a math girl. I won't talk much about it here, but the Fibonacci sequence is ubiquitous in nature, such as pine cones and the golden ratio, and its existence is noble and beautiful. .. .. And even in the world of programming, it is sometimes introduced with algorithms from beginner to intermediate level. I think this is an example of a good algorithm that allows beginners to feel a sense of accomplishment because it is easy to write code.

Fibonacci in various languages.

2016-04-12.png

Well, this is the main subject. As a program --Create a function (or method) `fibona (num) ``` that outputs a Fibonacci sequence --Substitute the desired term in the argument `num``` --Output the Fibonacci number up to that item to the standard output --Run it in the main function

I will make a program called.

C/C++ C / C ++ is a general term for the language C and the language C ++. C language is the first language I've come across, so I have a lot of thought. It is a little more difficult and complicated to grammar than other languages, but it has the fastest processing speed and it is no exaggeration to say that it is the language closest to the computer. C ++ is an extension of the C language with object-oriented programming. It makes it possible to write code that is simpler than C language, and is mainly used for DirectX game programming.

First from C language

fibona.c


#include <stdio.h>
void fibona(int num);

int main(void)
{
	fibona(20);
	return 0;
}

void fibona(int num)
{
	int x=1, y=1, i=0, tmp=0;
	while (i < num) {
		printf("%d\n", x);
		tmp=x;
		x=y;
		y+=tmp;
		i++;
	}
}

And C ++

fibona.cpp



#include <iostream>

using namespace std;

void fibona(int num);

int main(void)
{
	fibona(20);
	return 0;
}

void fibona(int num)
{
	int x=1,y=1,i=0,tmp=0;
	while (i < num) {
		cout << x << endl;
		tmp=x;
		x=y;
		y+=tmp;
		i++;
	}
}

When I try to compile using something called bcc (Borland C ++ Compile)

Console


>bcc32 fibona.c
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
fibona.c:
Turbo Incremental Link 5.00 Copyright (c) 1997, 2000 Borland
>fibona.exe
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

Java language

Java is a language created based on the C language grammar. I'm sure some of you have heard the word Java itself. Java runs on a virtual machine called the JVM (Java Virtual Machine). Therefore, even a win machine can be programmed on a Mac machine. Mainly used in Android programming.

fibona.java


public class fibona
{
	public static void main(String[] args) {
		fibonachi(20);
	}

	public static void fibonachi(int num)
	{
		int x=1,y=1,i=0,tmp=0;
		while(i < num){
			System.out.println(x);
			tmp=x;
			x=y;
			y+=tmp;
			i++;
		}
	}
}

Java is a VM language, so compile it using the javac command. This will create a class file called fibona.class. Do this with the java interpreter.

Console


>javac fibona.java
>java fibona
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

Python python is an object-oriented scripting language. Since it is executed line by line, it is easy to debug, and since the program is created with indentation, even beginners can write code that is easy to read. It has a wide range of defense and may be used for artificial intelligence, big data, and application extensions.

fibona.py


def fibona(num):
	x=1
	y=1
	i=0
	while(i<num):
		print(x)
		(x,y)=(y,x+y)
		i+=1

fibona(20)

Since it is a scripting language, it can be executed immediately with Python commands. By the way, all the programs I will write can be executed immediately with an interpreter like python.

Console


>python fibona.py
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

Ruby Ruby, like Python, is an object-oriented scripting language. Since the developers are Japanese, there are many Japanese documents and the code can be written easily. In addition, there is "fun programming" as the philosophy of Ruby, and you can program intuitively. It's also my favorite language. It seems that it is often used when creating web applications mainly with the framework called Ruby on Rails.

fibona.rb


def fibona(num)
	x=1
	y=1
	num.times do
		print "#{x}\n"
		x,y=y,x+y
	end
end

fibona(20)

Let's run it.

Console


>ruby fibona.rb
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

PHP php is mainly used to create web pages. You can dynamically create a web page by writing the php code in the `<??>` tag. Frameworks include CakePHP and others.

fibona.php


<?
function fibona($num){
	$x=1;$y=1;$i=0;
	while ($i < $num) {
		echo $x."\n";
		list($x, $y)=array($y, $x+$y);
		$i++;
	}
}

fibona(20);
?>

Console


1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

Perl Perl is primarily used as a Web CGI. Perl is an old-fashioned object-oriented language, so it's also nice to have some weird parts that other languages don't have.

fibona.pl


sub fibona {
	my($num) = @_;
	$x=1; $y=1; $i=0;
	while ($i < $num) {
		print "$x\n";
		($x, $y)=($y, $x+$y);
		$i++;
	}
}

fibona(20);

Console


>perl fibona.pl
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

JavaScript It's one of the most exciting languages these days. A language that allows you to write scripts in HTML for web development. You can create universal apps for Windows, and you can also create native apps such as .exe and .app with Electron created by GitHub. This time, I will use a tool called Node to program js as a general-purpose application.

MyFibona.js


var fibona = function(num){
	var x=1;
	var y=1;
	var i=0;

	while (i<num) {
		var tmp;
		console.log(x);
		tmp=x;
		x=y;
		y+=tmp;
		i++;
	}
};

fibona(20);

Console


>node MyFibona.js
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

CoffeeScript CoffeeScript is a language that makes it easy to write JavaScript. So compiling CoffeeScript will generate JavaScript code.

fibona.coffee


fibona = (num)->
	x=1;y=1;i=0;
	while i<num
		console.log x
		[x,y] = [y,x+y]
		i++

fibona(20)

Console


>coffee -c fibona.coffee

Generated code

fibona.js


// Generated by CoffeeScript 1.10.0
(function() {
  var fibona;

  fibona = function(num) {
    var i, ref, results, x, y;
    x = 1;
    y = 1;
    i = 0;
    results = [];
    while (i < num) {
      console.log(x);
      ref = [y, x + y], x = ref[0], y = ref[1];
      results.push(i++);
    }
    return results;
  };

  fibona(20);

}).call(this);

Console


>node fibona.js
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
1597
2584
4181
6765

in conclusion

How about that? Languages also have various personalities and are interesting. You can also see that anthropomorphic manga will appear. When I learn a new language, I feel like I've been to a new country. Talk to your computer according to the rules of the country. I used to think that "programming is something that science does?", But as the word "language" means, linguistic fun has come to the fore. There is no such thing as learning at school, having a test, doing homework, etc. like Japanese and English, and it is also attractive that it is easier than those. That's why it's a waste to be bilingual.

Recommended Posts

[Bilingual, it's a waste] Which is your favorite Fibonacci?
It's a Mac. What is the Linux command Linux?
Use a free GPU in your favorite environment