Skip to content

Instantly share code, notes, and snippets.

@frankiesardo
Created October 3, 2013 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frankiesardo/6809076 to your computer and use it in GitHub Desktop.
Save frankiesardo/6809076 to your computer and use it in GitHub Desktop.
Count the coins kata in prolog
value(1, 1).
value(2, 5).
value(3, 10).
value(4, 25).
value(5, 50).
count_coins(Amount, Ways) :- cc(Amount, Ways, 5).
cc(X, 0, _) :- X < 0.
cc(_, 0, 0).
cc(0, 1, _).
cc(Amount, Ways, Type) :-
value(Type, Value),
NewAmount is Amount - Value,
NewType is Type - 1,
cc(NewAmount, NewWays1, Type),
cc(Amount, NewWays2, NewType),
Ways is NewWays1 + NewWays2.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment