Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(part1).
- -export([pass_candy/4]).
- %% Pass candy until all students have equal values
- pass_candy(Turn, S1, S2, S3) ->
- case check(S1,S2,S3) of
- %% If they all have equal candy print the result
- true ->
- io:format("Turn:~B ",[Turn]),
- io:format("S1:~B ",[S1]),
- io:format("S2:~B ",[S2]),
- io:format("S3:~B~n",[S3]);
- %% If not, pass candy around according the rules
- false ->
- pass_candy(Turn+1, adjust(S3,S1), adjust(S1,S2), adjust(S2,S3))
- end.
- %% Adjust the candy
- adjust(Pas, Rec) ->
- %% Passer gives half of their candy
- Give = Pas div 2,
- %% Receiver is reduced by half because they need to give
- Temp = (Rec div 2) + Give,
- %% If the end result is odd, add 1
- case Temp rem 2 of
- 0 ->
- Temp;
- 1 ->
- Temp+1
- end.
- %% Check if they are all equal
- check(S1, S2, S3) ->
- (S1==S2) andalso (S2==S3).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement