Программа на python 3, перебирающая все возможные последовательности определённой длины: def shortest_chains(n): def next_chains(chain): new_elems = set() for i in range(len(chain)): for j in range(i, len(chain)): new_elem = chain[i] + chain[j] if new_elem > chain[-1] and new_elem not in new_elems: new_elems.add(new_elem) yield chain + [new_elem]
current_stage = None next_stage = [[1]] answer = [] while len(answer) == 0: current_stage = next_stage next_stage = [] for chain in current_stage: next_stage.extend(next_chains(chain)) answer = [chain[1:] for chain in next_stage if chain[-1] == n] return answer
def print_solution(n): answer = shortest_chains(n) print("Для {} есть {} решений(-я, -е):".format(n, len(answer))) for i in range(len(answer)): print("{}. {}".format(i + 1, " ".join(map(str, answer[i] print()
Запустив, можно получить все 5 возможных решений для числа 7, по 4 решения для 15 и 23 и 87 решений для 63.
Программа на python 3, перебирающая все возможные последовательности определённой длины: def shortest_chains(n): def next_chains(chain): new_elems = set() for i in range(len(chain)): for j in range(i, len(chain)): new_elem = chain[i] + chain[j] if new_elem > chain[-1] and new_elem not in new_elems: new_elems.add(new_elem) yield chain + [new_elem]
current_stage = None next_stage = [[1]] answer = [] while len(answer) == 0: current_stage = next_stage next_stage = [] for chain in current_stage: next_stage.extend(next_chains(chain)) answer = [chain[1:] for chain in next_stage if chain[-1] == n] return answer
def print_solution(n): answer = shortest_chains(n) print("Для {} есть {} решений(-я, -е):".format(n, len(answer))) for i in range(len(answer)): print("{}. {}".format(i + 1, " ".join(map(str, answer[i] print()
Запустив, можно получить все 5 возможных решений для числа 7, по 4 решения для 15 и 23 и 87 решений для 63.
def shortest_chains(n):
def next_chains(chain):
new_elems = set()
for i in range(len(chain)):
for j in range(i, len(chain)):
new_elem = chain[i] + chain[j]
if new_elem > chain[-1] and new_elem not in new_elems:
new_elems.add(new_elem)
yield chain + [new_elem]
current_stage = None
next_stage = [[1]]
answer = []
while len(answer) == 0:
current_stage = next_stage
next_stage = []
for chain in current_stage:
next_stage.extend(next_chains(chain))
answer = [chain[1:] for chain in next_stage if chain[-1] == n]
return answer
def print_solution(n):
answer = shortest_chains(n)
print("Для {} есть {} решений(-я, -е):".format(n, len(answer)))
for i in range(len(answer)):
print("{}. {}".format(i + 1, " ".join(map(str, answer[i]
print()
Запустив, можно получить все 5 возможных решений для числа 7, по 4 решения для 15 и 23 и 87 решений для 63.
def shortest_chains(n):
def next_chains(chain):
new_elems = set()
for i in range(len(chain)):
for j in range(i, len(chain)):
new_elem = chain[i] + chain[j]
if new_elem > chain[-1] and new_elem not in new_elems:
new_elems.add(new_elem)
yield chain + [new_elem]
current_stage = None
next_stage = [[1]]
answer = []
while len(answer) == 0:
current_stage = next_stage
next_stage = []
for chain in current_stage:
next_stage.extend(next_chains(chain))
answer = [chain[1:] for chain in next_stage if chain[-1] == n]
return answer
def print_solution(n):
answer = shortest_chains(n)
print("Для {} есть {} решений(-я, -е):".format(n, len(answer)))
for i in range(len(answer)):
print("{}. {}".format(i + 1, " ".join(map(str, answer[i]
print()
Запустив, можно получить все 5 возможных решений для числа 7, по 4 решения для 15 и 23 и 87 решений для 63.