Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 159 lines (110 sloc) 4.067 kB
6241c3e Solved firest 10 problems
Andrey Paramonov authored
1 -module(mymath).
93bb022 Added more algorithms to recursion.groovy
Andrey Paramonov authored
2 -export([c/2, ds/1, factorial/1, factorisation/1, factorisation/3, is_palindrome/1]).
3 -export([lcm/2, perms/1, pow/2, primes_upto/1, prod/1, ufactorisation/1]).
6241c3e Solved firest 10 problems
Andrey Paramonov authored
4 -include_lib("eunit/include/eunit.hrl").
5
6 %% Find all prime numbers upto specified value.
7 %%
8 primes_upto(N) -> eratosthenes(math:sqrt(N), lists:seq(2, N)).
9
10
11 %% Functional implementation of Eratosthenes sieve algorithm
12 %% http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
13 %%
14 %% Very clean but very slow. Works relatively fast for N < 100,000
15 %%
16 eratosthenes_sieve([]) -> [];
17 eratosthenes_sieve([P|Xs]) -> [P | eratosthenes_sieve([X || X <- Xs, X rem P > 0])].
18
19
20 %% Functional implementation of Euler sieve algorithm
21 %%
22 %% Very clean but very slow. Works relatively fast for N < 10,000
23 %%
24 euler_sieve([]) -> [];
25 euler_sieve([P|Xs]) -> [P | euler_sieve(Xs -- lists:map(fun(X) -> X*P end, [P|Xs]))].
26
27
28 %% Recursion implementation of Eratosthenes sieve algorithm
29 %% Author: Zac Brown
30 %%
31 %% Not so obvious but very efficient
32 %%
33 eratosthenes(Max, [H|T]) when H =< Max -> [H | eratosthenes(Max, sieve([H|T], H))];
34 eratosthenes(_Max, L) -> L.
35
36 sieve([H|T], N) when H rem N =/= 0 -> [H | sieve(T, N)];
37 sieve([_H|T], N) -> sieve(T, N);
38 sieve([], _N) -> [].
39
219e092 Moved lcm and gcd functions to mymath module
Andrey Paramonov authored
40
41 %% Least common multiple of two integers
42 %% http://en.wikipedia.org/wiki/Least_common_multiple
43 %%
44 lcm(A, B) -> (A * B) div gcd(A, B).
45
46
47 %% Greatest common divisor of two integers
48 %% http://en.wikipedia.org/wiki/Greatest_common_divisor
49 %%
50 gcd(A, B) when A < B -> gcd(B, A);
51 gcd(A, 0) -> A;
52 gcd(A, B) -> gcd(B, A rem B).
53
54
75e6f24 More functions in mymath module
Andrey Paramonov authored
55 %% Binomial coefficients
56 %%
57 c(N, N) -> 1;
58 c(N, M) when M < N-M -> c(N, N-M);
59 c(N, M) -> prod(lists:seq(M+1, N)) div factorial(N-M).
60
61
62 %% Product of numbers in the list
63 %%
96bb90f Next chunk of solutions
Andrey Paramonov authored
64 prod(List) -> lists:foldl(fun erlang:'*'/2, 1, List).
75e6f24 More functions in mymath module
Andrey Paramonov authored
65
96bb90f Next chunk of solutions
Andrey Paramonov authored
66 factorial(0) -> 1;
75e6f24 More functions in mymath module
Andrey Paramonov authored
67 factorial(N) -> prod(lists:seq(1,N)).
68
69
70 %% Sum of digits in the given integer
71 %%
72 ds(M) -> lists:foldl(fun(N, Sum) -> Sum + N - $0 end, 0, integer_to_list(M)).
73
74
96bb90f Next chunk of solutions
Andrey Paramonov authored
75 %% Integer power of another integer
76 %%
77 pow(N, 1) -> N;
78 pow(N, E) -> N * pow(N, E-1).
79
80
81 %% Calculates all permutations for elements from given list
82 %%
83 perms([]) -> [[]];
84 perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
85
86
87 %% Returns true if given list or integer is palindrom
88 %%
89 is_palindrome([]) -> [];
90 is_palindrome([X|Xs]) -> [X|Xs] =:= lists:reverse([X|Xs]);
91 is_palindrome(N) -> is_palindrome(integer_to_list(N)).
92
93
93bb022 Added more algorithms to recursion.groovy
Andrey Paramonov authored
94 %% Factorisation of given integer N
95 %%
96 factorisation(N) -> factorisation(N, primes_upto(N), []).
97 factorisation(1, _, Acc) -> Acc;
98 factorisation(N, [P|Ps], Acc) when N rem P =:= 0 -> factorisation(N div P, [P|Ps], [P|Acc]);
99 factorisation(N, [_|Ps], Acc) -> factorisation(N, Ps, Acc).
100
101 ufactorisation(N) -> lists:usort(factorisation(N)).
102
103
6241c3e Solved firest 10 problems
Andrey Paramonov authored
104 %% Tests
105
106 primes_upto_30_test() ->
107 ?assertEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], primes_upto(30)).
108
109 primes_upto_2m_test() ->
110 ?assertEqual(1999993, lists:last(primes_upto(2000000))).
111
112 euler_sieve_test() ->
113 ?assertEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], euler_sieve(lists:seq(2, 30))).
114
115 eratosthenes_sieve_test() ->
116 ?assertEqual([2, 3, 5, 7, 11, 13, 17, 19, 23, 29], eratosthenes_sieve(lists:seq(2, 30))).
219e092 Moved lcm and gcd functions to mymath module
Andrey Paramonov authored
117
118 lcm_test() ->
119 ?assertEqual(12, lcm(4, 6)).
120
121 gcd_test() ->
122 ?assertEqual(6, gcd(84, 18)).
75e6f24 More functions in mymath module
Andrey Paramonov authored
123
124 prod_test() ->
125 ?assertEqual(90, prod([2,5,9])).
126
127 c_test() ->
128 ?assertEqual(3, c(3, 1)).
129
130 factorial_test() ->
131 ?assertEqual(120, factorial(5)).
132
93bb022 Added more algorithms to recursion.groovy
Andrey Paramonov authored
133 factorisation_test() ->
134 ?assertEqual([3,2,2,2], factorisation(24)).
135
136 ufactorisation_test() ->
137 ?assertEqual([2,3], ufactorisation(24)).
138
75e6f24 More functions in mymath module
Andrey Paramonov authored
139 ds_test() ->
140 ?assertEqual(21, ds(1569)).
96bb90f Next chunk of solutions
Andrey Paramonov authored
141
142 pow_test() ->
143 ?assertEqual(32, pow(2, 5)).
144
145 perms_test() ->
146 ?assertEqual(["012", "021", "102", "120", "201", "210"], perms("012")).
147
148 is_palindrome_int_true_test() ->
149 ?assertEqual(true, is_palindrome(9009)).
150
151 is_palindrome_int_false_test() ->
152 ?assertEqual(false, is_palindrome(9001)).
153
154 is_palindrome_list_true_test() ->
155 ?assertEqual(true, is_palindrome("1001001001")).
156
157 is_palindrome_list_false_test() ->
158 ?assertEqual(false, is_palindrome("1001001011")).
Something went wrong with that request. Please try again.