Category: Correspondence

Question (EN): Find the sum of the smallest natural numbers A and B that satisfy the equation 360÷(A×A×A/B)=5.
Question (KO): 360÷(A×A×A/B)=5의 식을 만족하는 가장 작은 자연수 A, B의 합을 구하여라.

Answer: 9

Solution (Python):
var_a = '360÷(A×A×A/B)=5'
var_b = 'A'
ans_dict = dict()
var_a = var_a.replace('×','*')
var_a = var_a.replace('x','*')
var_a = var_a.replace('÷','/')
variable_candi = set(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])
for v in set(var_a):
    if v in variable_candi:
        ans_dict[v] = []
candidate_num = [i for i in range(51)]
candi = list(itertools.product(candidate_num, repeat=len(ans_dict)))
for c in candi:
    temp = var_a
    for i, (k, _) in enumerate(ans_dict.items()):
        temp = temp.replace(k, str(c[i]))
    term_list = []
    op_list = []
    temp_c = ''
    for tc in temp:
        if tc not in '+-*/=><().':
            temp_c += tc
        else:
            op_list.append(tc)
            term_list.append(temp_c)
            temp_c = ''
    term_list.append(temp_c)
    new_eq = ''
    for i in range(len(op_list)):
        if term_list[i] == '':
            new_eq += str(term_list[i])+op_list[i]
        else:
            new_eq += str(int(term_list[i]))+op_list[i]
    new_eq += str(int(term_list[-1]))
    new_eq=new_eq.replace('=', '==')
    new_eq=new_eq.replace('>==', '>=')
    new_eq=new_eq.replace('<==', '<=')
    eval_result = False
    try:
        if '=' in new_eq and '>' not in new_eq and '<' not in new_eq:
            new_eq=new_eq.replace('==','=')
            new_eq=new_eq.replace('>','')
            new_eq=new_eq.replace('<','')
            new_eq=new_eq.split('=')
            for i in range(len(new_eq)-1):
                eval_result = math.isclose(eval(new_eq[i]), eval(new_eq[i+1]))
                if not eval_result:
                    break
        else:
            eval_result = eval(new_eq)
    except:
        eval_result = False
        pass
    if eval_result:
        for i, (k, _) in enumerate(ans_dict.items()):
            ans_dict[k].append(int(c[i]))
list_a = list(set(ans_dict[var_b]))
var_c = 1
list_b=list_a.copy()
list_b.sort()
var_d = list_b[var_c-1]
var_e = '360÷(A×A×A/B)=5'
var_f = 'B'
ans_dict = dict()
var_e = var_e.replace('×','*')
var_e = var_e.replace('x','*')
var_e = var_e.replace('÷','/')
variable_candi = set(['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'])
for v in set(var_e):
    if v in variable_candi:
        ans_dict[v] = []
candidate_num = [i for i in range(51)]
candi = list(itertools.product(candidate_num, repeat=len(ans_dict)))
for c in candi:
    temp = var_e
    for i, (k, _) in enumerate(ans_dict.items()):
        temp = temp.replace(k, str(c[i]))
    term_list = []
    op_list = []
    temp_c = ''
    for tc in temp:
        if tc not in '+-*/=><().':
            temp_c += tc
        else:
            op_list.append(tc)
            term_list.append(temp_c)
            temp_c = ''
    term_list.append(temp_c)
    new_eq = ''
    for i in range(len(op_list)):
        if term_list[i] == '':
            new_eq += str(term_list[i])+op_list[i]
        else:
            new_eq += str(int(term_list[i]))+op_list[i]
    new_eq += str(int(term_list[-1]))
    new_eq=new_eq.replace('=', '==')
    new_eq=new_eq.replace('>==', '>=')
    new_eq=new_eq.replace('<==', '<=')
    eval_result = False
    try:
        if '=' in new_eq and '>' not in new_eq and '<' not in new_eq:
            new_eq=new_eq.replace('==','=')
            new_eq=new_eq.replace('>','')
            new_eq=new_eq.replace('<','')
            new_eq=new_eq.split('=')
            for i in range(len(new_eq)-1):
                eval_result = math.isclose(eval(new_eq[i]), eval(new_eq[i+1]))
                if not eval_result:
                    break
        else:
            eval_result = eval(new_eq)
    except:
        eval_result = False
        pass
    if eval_result:
        for i, (k, _) in enumerate(ans_dict.items()):
            ans_dict[k].append(int(c[i]))
list_c = list(set(ans_dict[var_f]))
var_g = 1
list_d=list_c.copy()
list_d.sort()
var_h = list_d[var_g-1]
var_i = var_d + var_h
print(int(var_i))