Starting in the top left corner of a 2
2 grid, there are 6 routes (without backtracking) to the bottom right corner.
How many routes are there through a 20
20 grid?
Erlang: Running time = 0.11s
+-%factorial
factorial(0)->1;
factorial(N)->
lists:foldl(fun(A,B)->A*B end, 1, lists:seq(1,N)).
+-%pow
pow(_,0)->1;
pow(1,_)->1;
pow(A,B)->pow(A,B,1).
pow(_,0,Res)->Res;
pow(Mult,B,Res) when B band 1 == 1 ->
pow(Mult*Mult,B bsr 1, Res*Mult);
pow(Mult,B,Res)->pow(Mult*Mult,B bsr 1, Res).
p15()->io:format("~w~n",[factorial(40) div pow(factorial(20),2)]).
Ruby: Running time = 0.02s
+-#factorial
def factorial(n)
return 1 if n<2
(2..n).inject{|u,v|u*v}
end
def p15
puts factorial(40)/(factorial(20)**2)
end