Still the best...
Kevin O'Gorman
kevin
Mon May 17 11:33:45 PDT 2004
On Sun, Jun 23, 2002 at 07:10:52AM -0500, David A. Bandel wrote:
> On Sat, 22 Jun 2002 18:51:06 -0700
> begin "Kevin O'Gorman" <kevin at kosmanor.com> spewed forth:
>
> [snip]
>
> > In fact, I calculate (this the help of my local friendly Linux machine)
> > that there are exactly 240 ways to do it. It's very simple to do; just
> > try 0-4 quarters, then 0 to some number of dimes (depending on how many
> > quarters), 0 to some number of nickels, and pennies to fill out the
> > buck. Here's what it looks like in Python:
> >
> > #!/usr/bin/python
> > w = 0
> > for q in range(0,5):
> > for d in range(0,10):
> > if q*25+d*10>100: break
> > for n in range (0,20):
> > if q*25 + d*10 + n*5 > 100: break
> > w = w + 1
> > print \
> > "%2.2d quarters, %2.2d dimes, %2.2d nickels, %3.3d pennies: %d ways"
> > \
> > % (q, d, n, 100- q*25 - d*10 - n*5, w)
> >
> >
>
> Please grep your output for:
> 20 nickels
> 10 dimes
> and there?s a number of others missing. Last time I looked, the above 2
> were legitimate ways to change a dollar.
>
> Just by inspection, 240 cannot be the correct number. The number will be
> odd.
Oops, you're right. I don't use Python enough to rememeber reliably
that range() needs to overshoot. I got it right for quarters, but forgot
the other two. This however adds just two cases, the count is 242, still
less than 293, and even.
The new program is
> > #!/usr/bin/python
> > w = 0
> > for q in range(0,5):
> > for d in range(0,11):
> > if q*25+d*10>100: break
> > for n in range (0,21):
> > if q*25 + d*10 + n*5 > 100: break
> > w = w + 1
> > print \
> > "%2.2d quarters, %2.2d dimes, %2.2d nickels, %3.3d pennies: %d ways" \
> > % (q, d, n, 100- q*25 - d*10 - n*5, w)
++ kevin
--
Kevin O'Gorman (805) 650-6274 mailto:kevin at kosmanor.com
Permanent e-mail forwarder: mailto:Kevin.O'Gorman.64 at Alum.Dartmouth.org
At school: mailto:kogorman at cs.ucsb.edu
Web: http://www.cs.ucsb.edu/~kogorman/index.html
Web: http://kosmanor.com/~kevin/index.html
"Life is short; eat dessert first!"
More information about the Linux-users
mailing list