Счетчик не срабатывает и продолжает выгружать результаты бесконечно. В чем ошибка ?
import os
def generate_combinations(powers_of_2, current_combination, current_sum, min_sum, max_sum, specific_number, file, count):
if count >= 1000:
return
if current_sum > max_sum:
return
if min_sum <= current_sum <= max_sum:
difference = specific_number - current_sum
if 0 < difference < specific_number and difference < 2**120:
print(f"Found combination: {' + '.join(map(str, current_combination))} = {current_sum}")
print(f"Hex difference: {hex(difference)}, Hex specific_number - difference: {hex(specific_number-difference)}")
file.write(f"{hex(difference)},{hex(specific_number-difference)}\n")
return generate_combinations(powers_of_2, [], 0, min_sum, max_sum, specific_number, file, count + 1)
for i, power in enumerate(powers_of_2):
new_combination = current_combination + [power]
new_sum = current_sum + power
generate_combinations(powers_of_2[i+1:], new_combination, new_sum, min_sum, max_sum, specific_number, file, count)
def main():
max_sum = 2**130
min_sum = 2**50# 0x11111111111110000000000000000000
specific_number = 0x39ec23e5dc38d798aff551c44fba9c65f
powers_of_2 = [2**i for i in range(10, 130)]
script_dir = os.path.dirname(__file__)
results_file = os.path.join(script_dir, "results.txt")
with open(results_file, "w") as file:
generate_combinations(powers_of_2, [], 0, min_sum, max_sum, specific_number, file, 0)
if __name__ == "__main__":
main()