Skip to content

Instantly share code, notes, and snippets.

@yong27
Created July 6, 2017 08:27
Show Gist options
  • Save yong27/a291323ed240815b371b9fcdce45fa61 to your computer and use it in GitHub Desktop.
Save yong27/a291323ed240815b371b9fcdce45fa61 to your computer and use it in GitHub Desktop.
Make 10
"""
오늘은 8, 7, 3, 4로 10을 만들어 봅시다!
존재하는 모든 연산을 허용합니다. 단, 숫자 붙이기(ex. 1과 5를 붙여 15를 만드는 것 등)는 허용하지 않습니다. 각 숫자는 한 번씩만 사용합시다.
https://twitter.com/MathQ_kr/status/882781179849482240
"""
from itertools import permutations, product
numbers = 7, 8, 3, 4
operators = {
#All python operations are in https://docs.python.org/3/library/operator.html#mapping-operators-to-functions
'+': lambda a,b: a + b,
'-': lambda a,b: a - b,
'x': lambda a,b: a * b,
'/': lambda a,b: a / b,
'%': lambda a,b: a % b,
#'|': lambda a,b: a | b,
#'&': lambda a,b: a & b,
#'^': lambda a,b: a ** b,
#'//': lambda a,b: a // b,
}
for p in permutations(numbers, 4):
for ops in product(operators.items(), repeat=3):
o = [x[0] for x in ops]
f1, f2, f3 = [x[1] for x in ops]
try:
result = f3(f1(p[0], p[1]), f2(p[2], p[3]))
except:
continue
if result == 10:
print('({}{}{}){}({}{}{})={}'.format(
p[0], o[0], p[1], o[2], p[2], o[1], p[3], result))
"""
Result using only +,-,x,/,% (% is mod)
(7%8)+(3%4)=10
(7+3)%(8+4)=10
(7+3)%(8x4)=10
(7+3)+(8%4)=10
(7+3)-(8%4)=10
(7+3)%(4+8)=10
(7+3)%(4x8)=10
(8%4)+(7+3)=10
(8%4)+(3+7)=10
(3+7)%(8+4)=10
(3+7)%(8x4)=10
(3+7)+(8%4)=10
(3+7)-(8%4)=10
(3+7)%(4+8)=10
(3+7)%(4x8)=10
(3%4)+(7%8)=10
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment