Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 44 lines (35 sloc) 1.091 kb
96bb90f Next chunk of solutions
Andrey Paramonov authored
1 %% Problem
2 %% ---------------------
93bb022 Added more algorithms to recursion.groovy
Andrey Paramonov authored
3 %% Pentagonal numbers are generated by the formula, Pn=n(3n-1)/2. The
96bb90f Next chunk of solutions
Andrey Paramonov authored
4 %% first ten pentagonal numbers are:
5 %%
6 %% 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ...
7 %%
8 %% It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their
9 %% difference, 70 - 22 = 48, is not pentagonal.
10 %%
11 %% Find the pair of pentagonal numbers, Pj and Pk, for which their
12 %% sum and difference is pentagonal and D = |Pk - Pj| is minimised;
13 %% what is the value of D?
14 %% ---------------------
15
16 -module(p044).
17 -export([solve/0]).
18 -include_lib("eunit/include/eunit.hrl").
19
20
21 solve() -> pent(1, 1, []).
22
23 %% P(n+1) = P(n) + 3n + 1
24 %%
25 pent(N, P, Ps) ->
26 List = [ P-X || X <- Ps, is_pentagonal(P-X), is_pentagonal(P+X) ],
27 case List of
28 [] -> pent(N+1, P+3*N+1, [P|Ps]);
29 _ -> List
30 end.
31
32 %% http://en.wikipedia.org/wiki/Pentagonal_number
33 %%
34 is_pentagonal(X) ->
35 N = (math:sqrt(24*X+1) + 1) / 6,
36 N == trunc(N).
37
38
39 is_pentagonal_true_test() ->
40 ?assertEqual(true, is_pentagonal(92)).
41
42 is_pentagonal_false_test() ->
43 ?assertEqual(false, is_pentagonal(91)).
Something went wrong with that request. Please try again.