Ускорьте код, чтобы решить, какая буква соответствует какой цифре в заданном уравнении.Python

Программы на Python
Anonymous
Ускорьте код, чтобы решить, какая буква соответствует какой цифре в заданном уравнении.

Сообщение Anonymous »


I am going to do a math competition which allows writing programs to solve problems.

The code attempts to solve this problem:

WHITE+WATER=PICNIC where each distinct letter represents a different digit. Find the number represented by PICNIC.

No imports are allowed (tqdm was just a progress bar). What I tried is below. At the rate of which my computer goes, it is not fast enough for the times competition because it needs to range over 10 to the power of digits there is.

Is there a clear solution to this problem?
from tqdm import tqdm def find_solution(): for W in tqdm(range(10)): for H in tqdm(range(10), desc='H'): for I in tqdm(range(10), desc='I'): for T in tqdm(range(10)): for E in tqdm(range(10)): for A in (range(10)): for R in (range(10)): for P in (range(1, 10)): # P cannot be 0 for C in (range(10)): for N in (range(10)): white = W * 10000 + H * 1000 + I * 100 + T * 10 + E water = W * 10000 + A * 1000 + T * 100 + E * 10 + R picnic = P * 100000 + I * 10000 + C * 1000 + N * 100 + I * 10 + C if white + water == picnic: return {'W': W, 'H': H, 'I': I, 'T': T, 'E': E, 'A': A, 'R': R, 'P': P, 'C': C, 'N': N} return None solution = find_solution() if solution: print("Solution found:") print(solution) else: print("No solution found.")

Источник: https://stackoverflow.com/questions/780 ... en-equatio

Вернуться в «Python»