Skip to content

Instantly share code, notes, and snippets.

@egglang
Created September 28, 2017 17:18
Show Gist options
  • Save egglang/e244cec66ac59f161feba4bada44b654 to your computer and use it in GitHub Desktop.
Save egglang/e244cec66ac59f161feba4bada44b654 to your computer and use it in GitHub Desktop.
賢者からの挑戦 竹俣紅女流 Question.3
%% egglang <[email protected]>
%%
%% 賢者からの挑戦 http://www.math4life.jp/
%% 竹俣紅 Q3.
-module(m).
-compile(export_all).
main() ->
beni3(create_matrix(5, 5)).
put_and_print({X,Y}, Val) ->
put({X,Y}, Val),
io:format("X: ~p, Y: ~p Value: ~p ~n",[X, Y, Val]).
-spec create_matrix(integer(), integer()) -> list().
create_matrix(XNum, YNum) ->
Mat = [{X,Y} || X <- lists:seq(1, XNum), Y <- lists:seq(1, YNum)],
[put(T, 0) || T <- Mat], % put initial values into a process dictionary.
Mat.
-spec beni3(list()) -> integer().
beni3(L) -> beni3(L, 0).
-spec beni3(list(), integer()) -> integer().
beni3([], Val) -> Val;
beni3([{1,1}|T], Val) -> put_and_print({1,1}, 1), beni3(T, Val);
beni3([{2,1}|T], Val) -> put_and_print({2,1}, 1), beni3(T, Val);
beni3([{1,2}|T], Val) -> put_and_print({1,2}, 1), beni3(T, Val);
beni3([{X,Y}|T], _) ->
MyVal = lists:sum([get({IX,Y}) || IX <- lists:seq(1,X)]) + lists:sum([get({X,IY}) || IY <- lists:seq(1,Y)]),
put_and_print({X,Y}, MyVal),
beni3(T, MyVal).
% result:
%
% 1> c(m).
% {ok,m}
% 2> m:main().
% X: 1, Y: 1 Value: 1
% X: 1, Y: 2 Value: 1
% X: 1, Y: 3 Value: 2
% X: 1, Y: 4 Value: 4
% X: 1, Y: 5 Value: 8
% X: 2, Y: 1 Value: 1
% X: 2, Y: 2 Value: 2
% X: 2, Y: 3 Value: 5
% X: 2, Y: 4 Value: 12
% X: 2, Y: 5 Value: 28
% X: 3, Y: 1 Value: 2
% X: 3, Y: 2 Value: 5
% X: 3, Y: 3 Value: 14
% X: 3, Y: 4 Value: 37
% X: 3, Y: 5 Value: 94
% X: 4, Y: 1 Value: 4
% X: 4, Y: 2 Value: 12
% X: 4, Y: 3 Value: 37
% X: 4, Y: 4 Value: 106
% X: 4, Y: 5 Value: 289
% X: 5, Y: 1 Value: 8
% X: 5, Y: 2 Value: 28
% X: 5, Y: 3 Value: 94
% X: 5, Y: 4 Value: 289
% X: 5, Y: 5 Value: 838
% 838
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment