[Ruby / Python / Java / Swift / JS] What is an algorithm?

Reasons to write this article

  1. Summarize your own knowledge of algorithms (quite rough)
  2. Check when you forget

What is an algorithm?

Broad definition

"A series of steps to get the job done" [^ 1]

There is also an "algorithm" in everyday life.

Problem: I want to cook brown rice (new rice) [^ 2] deliciously.

How to cook delicious brown rice (= this is ** "algorithm" **)

  1. After washing the brown rice, let it absorb water for 12 to 24 hours.
  2. Add the same cup of water, salt and sake as brown rice.
  3. Heat with moderate heat so that it boils in 10 minutes, and when it boils, heat on low heat for 15 minutes.
  4. Turn off the heat and let it steam for 15 minutes.

If you follow the cooking method of ▲, you can cook deliciously with gas, electric stove, or IH. ▲ In the first place, you can cook with a rice cooker without cooking in a pot.

An example of an "algorithm" shaking the market [^ 3]

May 6, 2010 14:47 (Eastern Time) Dow Jones Industrial Average fell $ 1000 in 3 minutes (= ** Flash Crash **)

Possible Reasons for This Phenomenon: Details Still Unknown

・ At that time, there was a sense of crisis around the world that the Greek government would default, and when that happened, there was a fear of falling into a global financial crisis. The algorithm used by one fund manager sold $ 4 billion worth of stock futures too fast, and another algorithm responded. ・ Multiple traders colluded and tried to crash the market using multiple algorithms

"Algorithms" in the computer world

Recursion

Write an algorithm to find the factorial using recursion (Ruby / Python / Java / Swift)

factiorial.rb


def factorial(number)
	if number == 0
		1
	else
		number * factorial(number - 1)
	end
end

(0..20).each do |i|
	puts "#{i}! = #{factorial(i)}"
end

factiorial.py


def factorial(number):
    if(number==0):
        return 1
    else:
        return number * factorial(number-1)

for i in range(1,20+1):
    print(str(i) + "! = " + str(factorial(i)))

Factorial.java


class Factorial{
	public static int factorial(int number){
		if (number == 0){
			return 1;
		} else {
			return number * factorial(number - 1);
		}
	}

	public static void main(String[] args){
		for(int i=1; i<=20; i++){
			System.out.println(i + "! = " + (factorial(i)));
		}
	}
}

factorial.swift


func factiorial(_ number:Int) -> Int{
    if(number==0){
        return 1
    } else {
        return number * factiorial(number-1)
    }
}

for i in 0...20 {
     print("\(i)! = \(factiorial(i))")
}

factorial.js


const factorial = (number)=>{
   if(number===0){
       return 1;
   } else {
       return number * sample(number-1);
   }
}

for(let i=1; i<=20; i++){
   console.log(`${i}! = ${factorial(i)}`);
}

Ruby/Python/Swift/JS output result


0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 6227020800
14! = 87178291200
15! = 1307674368000
16! = 20922789888000
17! = 355687428096000
18! = 6402373705728000
19! = 121645100408832000
20! = 2432902008176640000

Java output result


1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600
13! = 1932053504
14! = 1278945280
15! = 2004310016
16! = 2004189184
17! = -288522240
18! = -898433024
19! = 109641728
20! = -2102132736

Other algorithms

Linear search Binary search Hash search

Edit history

2018/11/03 Posted 2018/11/19 Added JS code and changed title 2018/12/01 Added linear search link 2018/12/02 Added binary search / hash search link

Reference material

・ Christopher Steiner (Translation: Ryo Nagamine) "Algorithms dominate the world" (KADOKAWA, 2013) ・ Thomas H. Colmen (Translation: Takahiro Nagao) "Basics of Algorithms" (Nikkei BP, 2016) ・ Nobuhiro Shibata "Algorithms and Data Structures Learned with New and Clear Java" (SB Creative, 2017)

[^ 1]: "Basics of Algorithm" p15 [^ 2]: I cook rice in a pot because I don't have a rice cooker at home and I don't want to increase things as much as possible. [^ 3]: "Algorithms dominate the world" pp.5-10

Recommended Posts

[Ruby / Python / Java / Swift / JS] What is an algorithm?
[What is an algorithm? Introduction to Search Algorithm] ~ Python ~
[Swift / Ruby / Python / Java] Object-oriented programming
Python> What is an extended slice?
What is python
What is Python
[Python] What is Pipeline ...
What is an iterator?
[Python] What is virtualenv
Java VS PHP VS Python VS Ruby
Python is an adult language
What is an instance variable?
[Python] Python and security-① What is Python?
[Python] * args ** What is kwrgs?
What is a python map?
Python Basic Course (1 What is Python)
What is Python? What is it used for?
[Python] What is a zip function?
[Python] What is a with statement?
[Python] What is @? (About the decorator)
Develop an investment algorithm in Python 2
[python] What is the sorted key?
Python for statement ~ What is iterable ~
Ruby Python Java Case insensitive sorting
What is the python underscore (_) for?
Python in is also an operator
[Python] What to do when an error related to SSL authentication is returned
[Python] What is pandas Series and DataFrame?
[Python] What is inherited by multiple inheritance?
What is NaN? NaN Zoya (Python) (394 days late)
What kind of programming language is Python?
Python learning basics ~ What is type conversion? ~
What is "mahjong" in the Python library? ??
[Statistics for programmers] What is an event?
What is a dog? Python installation volume
Let's develop an investment algorithm with Python 1
Python algorithm
[Beginner / Saved version] What you can do by programming language (12 selections such as Ruby / Python / Java / Swift / PHP / Go)
What is wheezy in the Docker Python image?
I tried Python! ] I graduated today from "What is Python! Python!"!
What are you comparing with Python is and ==?
[Introduction to Udemy Python 3 + Application] 54. What is Docstrings?
paiza POH ec-campaign (C # / Java / Python / Ruby) # paizahack_01
An easy way to call Java from Python
Tell me what a conformal map is, Python!
[Python] Type Error:'WebElement' object is not iterable What to do when an error occurs