Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

1,000 bucks

by idnopheq (Chaplain)
on May 17, 2001 at 22:57 UTC ( [id://81336]=CUFP: print w/replies, xml ) Need Help??

I found the following puzzle here , very similar to one I did in high school Pascal class.

"A man told his son that he would give him $1000 if he could accomplish the following task. The father gave his son ten envelopes and a thousand dollars, all in one dollar bills. He told his son, "Place the money in the envelopes in such a manner that no matter what number of dollars I ask for, you can give me one or more of the envelopes, containing the exact amount I asked for without having to open any of the envelopes. If you can do this, you will keep the $1000."

When the father asked for a sum of money, the son was able to give him envelopes containing the exact amount of money asked for. How did the son distribute the money among the ten envelopes?"

I've also read it as a guy at an auction buying a car, etc. Anyway, our assignment for class went a few steps further. We had to initialy take in any amount of money, place the cash in as few envelopes as possible, and exit if the initial amount is less than the amount of money asked for in return.

I no longer suffer myself to code in Pascal, and I thought this fun for Perl. Here's mine, dashed off while on a conference call today ... no doubt there are more efficient ways to accomplish this ... curious to see the mix of TIMTOWTDI.

UPDATE: THX to gaudior for pointing out that CarTalk did this as one of their puzzles a ways back ... I could not remember what put the bug in my bonnet about this. Also, feel free to golf this ... those of you who haven't yet.

#!/usr/bin/perl -w use strict; die "Too little money or you are too greedy!\n" if $ARGV[1] > $ARGV[0]; my $cash = $ARGV[0]; my $amount = $ARGV[1]; my @dispersal; # fill the envelopes while ( $cash > 0 ) { # what would be the next amount in geometric progression my $current = 2**($#dispersal + 1); # if our next number is more than the cash in hand, we'll stuff the +last # envelope with the remainder if ( $current > $cash ) { push @dispersal, $cash } # otherwise, take the progression and stuff that else { push @dispersal, $current } # deduct the cash in this envelope $cash -= $dispersal[$#dispersal]; } # perform a numeric sort versus ASCII my @envelope = sort { $a <=> $b } @dispersal; print $#envelope+1 . " are as few envelopes as I can use.\n"; for ( my $i = $#envelope + 1; $i >= 0; $i-- ) { if ( $envelope[$i-1] <= $amount ) { $amount -= $envelope[$i-1]; print "Envelope $i (\$$envelope[$i-1])\n\$$amount remaining\n"; } }

--
idnopheq
Apply yourself to new problems without preparation, develop confidence in your ability to to meet situations as they arrise.

Replies are listed 'Best First'.
Re: 1,000 bucks
by merlyn (Sage) on May 17, 2001 at 23:48 UTC
    How about this?
    my $amount = shift; # disperse amount die "must be integer" if $amount != int($amount); die "too big" if $amount > 1000; die "too small" if $amount < 1; for (my $en = 1; $amount; $en <<= 1) { next unless $amount & $en; print "Giving you envelope with $en\n"; $amount &= ~ $en; }

    -- Randal L. Schwartz, Perl hacker

      Randal, that implies you have envelopes with 1, 2, 4, 8, 16, 32, 64, 128, 256 and 512 dollars. This totals more than 1000. oops. :-)

      Have fun,
      Carl Forde

        yeah, so you just chuck what's left into the 10th envelope - 489 i think (can't find my calculator)
Re: 1,000 bucks
by dailylemma (Scribe) on May 17, 2001 at 23:42 UTC
    If I understand the problem correctly, then
    my @dispersal; my $maxpow = int(log($cash)/log(2) - 1); @dispersal = map(2**$_, 0..$maxpow); $dispersal[$#dispersal+1] = $cash - 2**($maxpow +1)+1; my @envelope = sort { $a <=> $b } @dispersal;
    should correctly generate @envelope, although there is no need for @dispersal anymore.
Re: 1,000 bucks
by tachyon (Chancellor) on May 20, 2001 at 10:05 UTC
    I like Randal's answer even if it is technically wrong as you need $1023 to make it work! Some really interesting syntax there. Naturally you want a geometric progression. This is short, but very un-obfu ;-) tachyon print "Enter an integer 1-1000 :"; my $amount = <>; #die "Integers 1-1000 only!\n" unless $amount =~ m/^\d{1,4}$/ and $amo +unt <= 1000 and $amount > 0; my @envelope = qw(1 2 4 8 16 32 64 128 256 490); for (reverse @envelope) { next if $_ > $amount; printf "Giving you envelope with \$%d.00\n", $_; $amount -= $_; }
      tachyon, I think you need to count the amounts in your envelopes again...

      Have fun,
      Carl Forde

Re: 1,000 bucks
by gaudior (Pilgrim) on May 18, 2001 at 22:35 UTC
    This was done on NPR's Cartalk a few months ago.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://81336]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2024-04-20 14:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found