Partition function P: Difference between revisions

Line 211:
Took 131ms</pre>
 
=={{header|Erlang}}==
<lang Erlang>
-mode(compile).
 
main(_) ->
ets:new(pN, [set, named_table, protected]),
io:format("~w~n", [p(6666)]).
 
p(0) -> 1;
p(N) ->
case ets:lookup(pN, N) of
[{N, Pn}] -> Pn;
[] ->
Terms = [p(N - G) || G <- gpentagonals(N)],
Pn = sum_partitions(Terms, 0, 0),
ets:insert(pN, {N, Pn}),
Pn
end.
 
sum_partitions([], _, Sum) -> Sum;
sum_partitions([N|Ns], Sgn, Sum) ->
Summand = case Sgn < 2 of
true -> N;
false -> -N
end,
sum_partitions(Ns, (Sgn+1) band 3, Sum + Summand).
 
gpentagonals(Max) -> gpentagonals(1, Max, [0]).
gpentagonals(M, Max, Ps = [N|_]) ->
GP = N + case M rem 2 of
0 -> M div 2;
1 -> M
end,
if
GP > Max -> tl(lists:reverse(Ps));
true -> gpentagonals(M + 1, Max, [GP|Ps])
end.
</lang>
{{Out}}
<pre>
$ time ./partition6666.erl
193655306161707661080005073394486091998480950338405932486880600467114423441282418165863
 
real 0m0.480s
user 0m0.490s
sys 0m0.080s
</pre>
=={{header|F_Sharp|F#}}==
An implementation of the formula in the task description. P(123456) is included for comparison with the largest value in the related task.
357

edits