Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: puzzle: how many ways to make $100 (with Prolog!)

by Ovid (Cardinal)
on Apr 29, 2006 at 04:00 UTC ( #546463=note: print w/ replies, xml ) Need Help??


in reply to puzzle: how many ways to make $100

Well, if you're serious about teaching her logic skills ;) (I've ignored two dollar bills for simplicity. It should be trivial to add it)

#!/usr/bin/perl use strict; use warnings; use AI::Prolog; my @bills = qw(Fifties Twenties Tens Fives Ones); my $bills = join ',', @bills; # don't use $prolog->list because these +are vars my $program = <<"END_PROLOG"; change([ $bills ]) :- member(Fifties,[0,1,2]), member(Twenties,[0,1,2,3,4,5]), member(Tens,[0,1,2,3,4,5,6,7,8,9,10]), member(Fives,[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20 +]), Total is 50*Fifties + 20*Twenties +10*Tens + 5*Fives, Total <= 100, Ones is 100 - Total. member(X, [X|_]). member(X, [_|Tail]) :- member(X, Tail). END_PROLOG my $prolog = AI::Prolog->new($program); $prolog->query("change([ $bills ])."); while (my $result = $prolog->results) { for my $i ( 0 .. $#bills ) { print "$result->[1][$i] $bills[$i]"; print ", " unless $i == $#bills; } print "\n"; } __END__ # Beginning of output: 0 Fifties, 0 Twenties, 0 Tens, 0 Fives, 100 Ones 0 Fifties, 0 Twenties, 0 Tens, 1 Fives, 95 Ones 0 Fifties, 0 Twenties, 0 Tens, 2 Fives, 90 Ones 0 Fifties, 0 Twenties, 0 Tens, 3 Fives, 85 Ones 0 Fifties, 0 Twenties, 0 Tens, 4 Fives, 80 Ones 0 Fifties, 0 Twenties, 0 Tens, 5 Fives, 75 Ones 0 Fifties, 0 Twenties, 0 Tens, 6 Fives, 70 Ones 0 Fifties, 0 Twenties, 0 Tens, 7 Fives, 65 Ones 0 Fifties, 0 Twenties, 0 Tens, 8 Fives, 60 Ones ...

Cheers,
Ovid

New address of my CGI Course.


Comment on Re: puzzle: how many ways to make $100 (with Prolog!)
Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://546463]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2015-07-28 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (254 votes), past polls