int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}
long long int fibb(int n) {
int fnow = 0, fnext = 1, tempf;
while(--n > 0) {
tempf = fnow + fnext;
fnow = fnext;
fnext = tempf;
}
return fnext;
}
function factorial(n) {
if (n < 0) { throw "Number must be non-negative"; }
var result = 1;
while (n > 1) {
result *= n;
n--;
}
return result;
}
function fib(n) {
var a = 0, b = 1, t;
while (n-- > 0) {
t = a;
a = b;
b += t;
console.log(a);
}
return a;
}
(defn factorial [x]
(loop [x x
acc 1]
(if (< x 2)
acc
(recur (dec x) (*' acc x)))))
(defn- fib-iter
[max n i j]
(if (= n max)
j
(recur max
(inc n)
j
(+ i j))))
(defn fib
[max]
(if (< max 2)
max
(fib-iter max 1 0N 1N)))
function fact(n, acc)
acc = acc or 1
if n == 0 then
return acc
end
return fact(n-1, n*acc)
end
function fibs(n)
return n < 2 and n or fibs(n - 1) + fibs(n - 2)
end
fn factorial_recursive (n: u64) -> u64 {
match n {
0 => 1,
_ => n * factorial_recursive(n-1)
}
}
fn factorial_iterative(n: u64) -> u64 {
(1..=n).product()
}
fn main () {
for i in 1..10 {
println!("{}", factorial_recursive(i))
}
for i in 1..10 {
println!("{}", factorial_iterative(i))
}
}
factorial() {
if [ $1 -le 1 ]
then
echo 1
else
result=$(factorial $[$1-1])
echo $((result*$1))
fi
}
fib() {
if [ $1 -le 0 ]
then
echo 0
return 0
fi
if [ $1 -le 2 ]
then
echo 1
else
a=$(fib $[$1-1])
b=$(fib $[$1-2])
echo $(($a+$b))
fi
}
function! Factorial(n)
if a:n < 2
return 1
else
return a:n * Factorial(a:n-1)
endif
endfunction
function Num2Bin(n)
let n = a:n
let s = ""
if n == 0
let s = "0"
else
while n
if n % 2 == 0
let s = "0" . s
else
let s = "1" . s
endif
let n = n / 2
endwhile
endif
return s
endfunction
echo Num2Bin(5)
echo Num2Bin(50)
echo Num2Bin(9000)