"A series of steps to get the job done" [^ 1]
Problem: I want to cook brown rice (new rice) [^ 2] deliciously.
How to cook delicious brown rice (= this is ** "algorithm" **)
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.
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
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
Linear search Binary search Hash search
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
・ 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