solve a newspaper quiz

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Sun May 9 07:02:45 EDT 2010


superpollo <utente at esempio.net> writes:

> "if a b c are digits, solve ab:c=a*c+b"
>
> solved in one minute with no thought:

Obviously.

> for a in range(10):
>     for b in range(10):
>         for c in range(10):
>             try:
>                 if (10.*a+b)/c==a*c+b:
>                     print "%i%i:%i=%i*%i+%i" % (a,b,c,a,c,b)
>             except:
>                 pass
>
> any suggestion for improvement?

Here is my take (sorry, no nice prints):

# c = 0 has no solution
# c = 1 -> 9a = 0 whatever b
for b in xrange(10):
    print 0,b,1
# c = 2 -> 6a = b
for a in xrange(2):
    print a,a*6,2
# c = 3 -> a = 2b
for b in xrange(0,5):
    print 2*b,b,3
# c = 4-9 -> a = b = 0
for c in xrange(4,10):
    print 0,0,c

Improvements: a lot more fun to design, a lot more efficient to run

-- Alain.



More information about the Python-list mailing list