Dollar

Description

This simple Prolog program checks or generates change adding up to a dollar consisting of half-dollars, quarters, dimes, nickels, and pennies.

Source: PrologTutorial (on-line tutorial)

Download

Program source code: dollar.pl

Listing

change([H,Q,D,N,P]) :- 
    member(H,[0,1,2]),                      /* Half-dollars */ 
    member(Q,[0,1,2,3,4]),                  /* quarters     */ 
    member(D,[0,1,2,3,4,5,6,7,8,9,10]) ,    /* dimes        */ 
    member(N,[0,1,2,3,4,5,6,7,8,9,10,       /* nickels      */ 
               11,12,13,14,15,16,17,18,19,20]),  
    S is 50*H + 25*Q +10*D + 5*N, 
    S =< 100, 
    P is 100-S.
 
% ?- change([H,Q,D,N,P]). 
% ?- change([0,2,3,4,6]). 
% ?- change([0,2,3,2,P]). 

Comments

pl/prolog/pllib/dollar.txt · ostatnio zmienione: 2017/07/17 08:08 (edycja zewnętrzna)
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0