[JAVA] Try to display the Fibonacci sequence in various languages in the name of algorithm practice

It's been about half a year since I started writing python code, and I've become accustomed to thinking about algorithms, so when I wrote the Fibonacci sequence code and muttered on Twitter, from a senior high school alumnus "Fibonacci often goes to training and exams, so it's better to be able to write in various languages." Since I received the advice, I decided to display it in a language other than python, and in the process I decided to discover the features of each language.

Display method The explanation about the Fibonacci sequence is omitted. If you don't know, google (round throw) The sequence to be displayed shall start with [1,1], enter a numerical value, and if it is 2 or less, it will be displayed as [1,1]. If it is 2 or more, the sequence [1,1, ~~, n] of the input numerical value is output.

python

001.py


l = [1,1]
i = int(input("Please enter the length of the sequence"))
if i < 2:
    print(l)
else:
    for f in range(2,i):
        l.append(l[f-1]+l[f-2])
    print(l)

I feel that array relationships are easier to write than in other languages.

Java

001.java


import java.io.InputStreamReader;
import java.util.Scanner;

public class _main_ {
    public static void main(String[] args){
        int n1 =1;
        int n2 =1;
        int n3;
        System.out.print("Please enter the length of the sequence you want to find\n");
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        if (2 < a){
            System.out.print(n1+" "+n2+" ");
            for(int x=2;x<a;x++){
                n3= n1 + n2;
                System.out.print(n3+" ");
                n1 = n2;
                n2 = n3;
            }
        }
    }
}

I haven't studied much about java, so it's probably crazy. The method is to output n3, which is the sum of n1 and n2, and then replace the numbers.

C++

001.cpp


#include <stdio.h>
int main(void) {

	int x = 2;
	int  i[255] = { 1, 1 };
	printf("Enter the number in the sequence you want to find");
	scanf_s("%d", &x);
	if (x < 3) {
		printf("%d %d", i[0], i[1]);
	}
	else if (255 < x)
		printf("A value higher than the specified value has been entered");
	else
	{
		for (int n = 2; n < x; n++) {
			i[n] = i[n - 2] + i[n - 1];
		}
		for (int v = 0; v < (sizeof(i) / sizeof(i[0]))-1; v++) {

			if (i[v] != 0)
				printf("%d ", i[v]);
			else
				break;
		}
	}
	return 0;
}

I tried to build it based on the python method, but c ++ has a restriction that the length of the array must be decided from the beginning, so I was quite annoyed. I tried a method to match the maximum value with the entered value, but it didn't work, so I gave up.

*** What I found after writing *** ・ Python is easier than other languages

・ I feel that it will be a little easier to write if I know how to determine the maximum value of an array with variables in C ++.

・ In the future, I would like to be able to write in various languages such as javascript julia, which I started to touch.

・ I think there is a way to make C ++ and java code even easier, so I want to study and improve the quality of the code.

Recommended Posts

Try to display the Fibonacci sequence in various languages in the name of algorithm practice
[Cloudian # 9] Try to display the metadata of the object in Python (boto3)
Try to display the railway data of national land numerical information in 3D
Numerical representation of days of the week in various languages
Cython to try in the shortest
[Cloudian # 2] Try to display the object storage bucket in Python (boto3)
Try to model the cumulative return of rollovers in futures trading
Various comments to write in the program
I tried to display the altitude value of DTM in a graph
Various ways to read the last line of a csv file in Python
How to try the friends-of-friends algorithm with pyfof
Try to simulate the movement of the solar system
I want to display the progress in Python!
[Django] Let's try to clarify the part of Django that was somehow through in the test
Note that the method of publishing modules to PyPI has changed in various ways.
How to get the variable name itself in python
Try to solve the problems / problems of "Matrix Programmer" (Chapter 1)
Try to solve Sudoku in various ways (SAT, CSP)
Try to estimate the number of likes on Twitter
How to display multiple images of galaxies in tiles
Try to get the contents of Word with Golang
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Heapsort)
Representing Fibonacci sequences using lambda expressions in various languages
Try to decipher the login data stored in Firefox
Decrease the class name of the detection result display of object detection
To do the equivalent of Ruby's ObjectSpace._id2ref in Python
Python OpenCV tried to display the image in text.
How to display the regional mesh of the official statistics window (eStat) in a web browser
[Super easy! ] How to display the contents of dictionaries and lists including Japanese in Python
Implementation of Fibonacci sequence
HMAC in various languages
Try scraping the data of COVID-19 in Tokyo with Python
How to find the optimal number of clusters in k-means
Try to get the function list of Python> os package
Try to evaluate the performance of machine learning / regression model
I tried to find the average of the sequence with TensorFlow
Try to display various information useful for debugging with python
Make the display of Python module exceptions easier to understand
Print the name of the object directly under the object specified in Blender
Implemented the algorithm of "Algorithm Picture Book" in Python3 (Bubble Sort)
Try to evaluate the performance of machine learning / classification model
Try to improve the accuracy of Twitter like number estimation
Try to solve the problems / problems of "Matrix Programmer" (Chapter 0 Functions)
Try to automate the operation of network devices with Python
Display the timetable of the Morioka bus location system in Pythonista
Display the time in Django according to the user's region (UTC)
Try to extract the keywords that are popular in COTOHA
Try to model a multimodal distribution using the EM algorithm
Implemented the algorithm of "Algorithm Picture Book" in Python3 (selection sort)
Try to decipher the garbled attachment file name with Python
I implemented N-Queen in various languages and measured the speed
To get the name of the primitive etc. generated immediately before
Command to list all files in order of file name
Try to extract the features of the sensor data with CNN
How to display the CPU usage, pod name, and IP address of a pod created with Kubernetes
Try changing various conditions to "gather 1000 people in the room and keep giving money to random opponents"
[PyQt x pySerial] Display a list of COM ports connected to the PC in the combo box