Own-Programming-Language-Tu.../examples/functions/factorial.own
2015-07-11 21:19:59 +03:00

35 lines
663 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Вычисляем факториал
def factorial(n) {
if n == 1 return 1
return n * factorial(n-1)
}
print factorial(6)
print "\n"
// 0 1 1 2 3 5 8 13 21 34
def fibonacci(count) {
def fib(n) {
if n < 2 return n
return fib(n-2) + fib(n-1)
}
return fib(count)
}
for i = 0, i < 10, i = i + 1
print " " + fibonacci(i)
print "\n"
// Вычисляем факториал с помощью хвостовой рекурсии
def factorial_tailrec(n) {
def fact(acc, n) {
if n == 0 return acc
return fact(acc * n, n - 1)
}
return fact(1, n-1)
}
print factorial_tailrec(6)
print "\n"