Last active
September 13, 2015 17:05
-
-
Save shintakezou/666a96e63b0c01be2f78 to your computer and use it in GitHub Desktop.
Python(3) code to sieve solution(s) for “A quiz with Susi” n. 927
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/python3 | |
# | |
# italian magazine "La Settimana Enigmistica", | |
# quiz "Quesito con la Susi" n. 927 | |
# | |
# R, B, S: prices of one bottle of red, white and sparkling wine | |
# a, b, c: number of sparkling (a), white (b) and red (c) wine in a | |
# box of seven bottles | |
# | |
# R = 3B | |
# 7R = 3S | |
# a + b + c = 7 | |
# aS + bB + cR = 119 (euro) | |
# | |
# we find: | |
# | |
# R(49 - 6b - 4c) = 3*119 (1) | |
# | |
# The code uses this one, plus the fact that the smallest | |
# euro coin is the cent, hence prices are multiplied by 100 and | |
# treated as integers | |
# | |
# We obtain 3 solutions. The original problem gives us another | |
# constraint (which I missed): prices (in €) are integer. So I've | |
# added a variable that makes the code consider this constraint too. | |
# | |
# To do that, it's enough we don't multiply by 100 as we did | |
# to count in eurocent. | |
# | |
integer_prices = True | |
B = lambda r: int(r/3) # no reason to use lambda instead of def... | |
S = lambda r: int(7*r/3) | |
mult = 1 # prices are integer and in euro | |
if not integer_prices: | |
mult = 100 # prices are integer and in eurocent | |
for a in range(1,7): | |
sl = 7 - a # b + c = 7 - a | |
bc = [ (i, sl - i) for i in range(1,sl) ] | |
n = 3*119*mult # rhs of eq.(1), (*100) | |
for pair in bc: | |
b = pair[0] | |
c = pair[1] | |
d = 49 - 6*b - 4*c # lhs of eq.(1) /R | |
if d <= 0: # with these values, it won't happen | |
continue | |
r = int(n/d) | |
v = a*S(r) + b*B(r) + c*r | |
if v == 119*mult: | |
print(a, b, c, "-", S(r), B(r), r) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment