Created
August 2, 2016 16:44
-
-
Save edvardm/2f967c427746f5b0d5c73d99dd38ada8 to your computer and use it in GitHub Desktop.
rekursio.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rekursiosta: kertoma | |
# | |
# n! = n * n-1 * n-2 * .. * 1 | |
# | |
# Esim. | |
# 2! = 2 * 1 | |
# 3! = 3 * 2 * 1 = 6 | |
# 4! = 4 * 3 * 2 * 1 = 24 | |
# 5! = 5 * 4 * 3 * 2 * 1 = 120 | |
# 6! = 6 ... = 720 | |
# 7! = ... = 5040 | |
# | |
# Matematiikassa: | |
# | |
# n! = | 1 jos n < 2 | |
# | n * (n - 1)! muussa tapauksessa | |
# | |
# 4! = 4 * 3! | |
# = 4 * 3 * 2! | |
# = 4 * 3 * 2 * 1! | |
# = 4 * 3 * 2 * 1 = 24 | |
# Ruby - vertaa matemaattiseen määritelmään | |
def kertoma(n) | |
if n < 2 | |
1 | |
else | |
n * kertoma(n-1) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment