-module(misc). -compile(export_all). sq(X) -> X * X. sumsq(X,Y) -> sq(X) + sq(Y). f(A) -> sumsq(A+1,A*2). %% ----------- sqrtIter(Guess, X) -> case goodEnough(Guess, X) of true -> Guess; false -> sqrtIter(improved(Guess, X), X) end. improved(Guess,X) -> average(Guess, X/Guess). average(X,Y) -> (X+Y)/2. goodEnough(Guess,X) -> abs(sq(Guess) - X) < 0.00001. sqrt(X) -> sqrtIter(1.0, X). %% ---------- %% @spec factorial1(N::integer()) -> %% F::integer() %% @pre N >= 0. %% F == N! factorial1(0) -> 1; factorial1(N) when N>0 -> N * factorial1(N-1). factorial2(N) when N >= 0 -> factorial2(N,1,1). factorial2(N,Product,Counter) -> if (Counter > N) -> Product; true -> factorial2(N, Product*Counter, Counter+1) end. factorial21(N) when N >= 0 -> factorial21(N,1,1). factorial21(N,Product,Counter) when Counter > N -> Product; factorial21(N,Product,Counter) -> factorial21(N, Product*Counter, Counter+1). %% @pre : N >= 0. factorial3(N) -> factorial3(1,N). factorial3(Product, 0) -> Product; factorial3(Product, Counter) -> factorial3(Product*Counter, Counter-1). %% ----------- map(_F,[]) -> []; map(F,[X|Xs]) -> [F(X)|map(F,Xs)]. %% ----------- %% @spec fib(N::integer()) -> %% F::integer(). %% @pre 0 =< N. %% F az N-edik Fibonacci-szám fib1(0) -> 0; fib1(1) -> 1; fib1(N) when N>1 -> fib1(N-1)+fib1(N-2). fib2(N) when N>=0 -> fib2(N,0,0,1). fib2(N,N,A,_B) -> A; fib2(N,I,A,B) -> fib2(N,I+1,A+B,A). fib3(N) when N>=0 -> fib3(N,0,1). fib3(0,A,_B) -> A; fib3(I,A,B) -> fib3(I-1,A+B,A). %% ----------- valtas(Osszeg) -> valtas(Osszeg,5). valtas(Osszeg,Ermefajta) -> if Osszeg < 0 orelse Ermefajta == 0 -> 0; Osszeg == 0 -> 1; true -> valtas(Osszeg, Ermefajta-1) + valtas(Osszeg-cimlet(Ermefajta), Ermefajta) end. cimlet(1) -> 1; cimlet(2) -> 5; cimlet(3) -> 10; cimlet(4) -> 25; cimlet(5) -> 50. %% misc:valtas(10) == 4. misc:valtas(100) == 292. %% ----------- %% @spec expt1(B::integer(),N::integer()) -> R::integer(). %% @pre N >= 0. %% B N-edik hatványa R. expt1(_B,0) -> 1; expt1(B,N) when N>0 -> B * expt1(B,N-1). expt2(B,N) when N>=0 -> expt2(B,N,1). expt2(_B,0,P) -> P; expt2(B,N,P) -> expt2(B,N-1,B*P). expt3(_B,0) -> 1; expt3(B,N) when N>0 -> case even(N) of true -> sq(expt3(B,N div 2)); false -> B * expt3(B,N-1) end. even(N) -> N rem 2 == 0. expt4(B,N) when N>=0 -> expt4(B,N,1). expt4(_B,0,R) -> R; expt4(B,N,R) -> case even(N) of true -> expt4(B,N div 2,R*R); false -> expt4(B,N-1,R*B) end. %% f(), X0 = misc:expt4(2,0), X1 = misc:expt4(2,1), X2 = misc:expt4(2,2), X4 = misc:expt4(2,4), {X0,X1,X2,X4}. expt5(_B,0) -> 1; %% \textrm{Kell ez a klóz!} expt5(B,N) when N>0 -> expt5(B,N,B). expt5(_B,1,R) -> R; expt5(B,N,R) -> case even(N) of true -> expt5(B,N div 2,R*R); false -> expt5(B,N-1,R*B) end. %% f(), X0 = misc:expt5(2,0), X1 = misc:expt5(2,1), X2 = misc:expt5(2,2), X3 = misc:expt5(2,3), X4 = misc:expt5(2,4), X5 = misc:expt5(2,5), X6 = misc:expt5(2,6), X7 = misc:expt5(2,7), X8 = misc:expt5(2,8), {X0,X1,X2,X3,X4,X5,X6,X7,X8}. expt6(_B,0) -> 1; %% \textrm{Kell ez a klóz!} expt6(B,N) when N>0 -> expt6(B,N-1,B). expt6(_B,0,R) -> R; expt6(B,N,R) -> case even(N) of true -> expt6(B,N div 2,B*B*R); false -> expt6(B,N-1,R*B) end. expt7(_B,0) -> 1; %% \textrm{Kell ez a klóz!} expt7(B,N) when N>0 -> expt7(B,N,B). expt7(_B,1,R) -> R; expt7(B,N,R) -> case even(N) of true -> expt7(B,N div 2,B*B*R); false -> expt7(B,N-1,R*B) end.