aboutsummaryrefslogtreecommitdiff
path: root/problem35.erl
blob: 4944ec413b0ecef4d3fe98c6d2c7991fec50ac3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
-module(problem35).
-export([solve/0]).

solve() ->
	Primes = gen_list([3,2],1000000),
	length(lists:map(fun(X) -> allin(rot(X, []), Primes) end, Primes)).

gen_list(List,Max) ->
	O = gen_next(List),
	if
		O > Max ->
			List;
		true ->
			gen_list([O|List],Max)
	end.

gen_next(List) ->
	gen_next(List, lists:max(List) + 1).
gen_next(List, Next) ->
	Q = will_divide(Next, List),
	if
		Q ->
			gen_next(List, Next + 1);
		true ->
			Next
	end.

will_divide(_, []) ->
	false;
will_divide(Num, [H|T]) ->
	if
		Num rem H =:= 0 ->
			true;
		true ->
			will_divide(Num, T)
	end.

rot(Num, [Num|T]) ->
	[Num|T];
rot(Num, []) ->
	[A|B] = integer_to_list(Num),
	rot(Num, [list_to_integer(B++[A])]);
rot(Num, [H|T]) ->
	[A|B] = integer_to_list(H),
	rot(Num, [list_to_integer(B++[A]),H|T]).

allin(L1, L2) ->
	L3 = lists:map(fun(X) -> lists:member(X, L2) end, L1),
	lists:foldl(fun(X, ASF) -> X and ASF end, true, L3).