Improve the speed and algorithm of this code, The problem is to find a set of unique…
- User
Improve the speed and algorithm of this code, The problem is to find a set of unique values that when all put over one and added up equal one import random from datetime import datetime
class SplitNumbers(): def init(self, lst, file_name): self.lst = lst self.original = lst[:] self.FILE_NAME = file_name
self.cached_numbers = {} self.fraction_list = [] self.counter = 0 self.index = 0 self.done = True def reader(self): with open(self.FILE_NAME, "r") as reader: data = reader.readlines() for line in data: number, number_list = line.strip().split(":") number = int(number) number_list = list(map(int, number_list.split(", "))) if number in self.cached_numbers: self.cached_numbers[number].append(number_list) else: self.cached_numbers[number] = [number_list] self.cached_numbers = dict(sorted(self.cached_numbers.items())) def splitter(self): array = self.cached_numbers.get(self.index, []) if not len(array): return def is_valid(lst): return all(item not in self.lst for item in lst) valid_list = [lst for lst in array if is_valid(lst)] if not len(valid_list): return next_lists = [sum(len(self.cached_numbers.get(number, [])) for number in valid) / len(valid) for valid in valid_list] max_next_list = valid_list[next_lists.index(max(next_lists))] self.lst.remove(self.index) self.lst.extend(max_next_list) self.lst.sort() # if len(self.lst) > 890: print(self.counter, round(sum([1/i for i in self.lst])), len(self.lst), datetime.utcnow()) self.counter = 0 def reloop_self(self): while True: mm_score = [] max_mins = [] while len(mm_score) < 5: i = random.randint(2, 2023) combos = self.cached_numbers.get(i, []) if not combos: continue def in_list(c): return all(number in self.lst for number in c) def not_in_list(c): return all(number not in self.lst for number in c) combos_in_list = list(filter(in_list, combos)) if not len(combos_in_list): continue valid_combos = list(filter(lambda c: not_in_list(c), combos)) if not len(valid_combos): continue min_list_combo = min(combos_in_list, key=len) max_valid_combo = max(valid_combos, key=len) max_mins.append([min_list_combo, max_valid_combo]) mm_score.append(len(max_valid_combo) - len(min_list_combo)) # if len(max_valid_combo) >= len(min_list_combo): if not len(mm_score): return best_min = max_mins[max(mm_score)][0] best_max = max_mins[max(mm_score)][1] pre_list = [number for number in self.lst[:] if number not in best_min] pre_list.extend(best_max) pre_list.sort() self.lst = pre_list[:] self.done = False return def main(self): print("At:", datetime.utcnow()) print(round(sum([1/i for i in self.lst]), 14), len(self.lst)) self.reader() print("split_tom.py starting…") best_list = [] while True: if len(best_list) < len(self.lst): best_list = self.lst[:] print(str(best_list).replace(" ", ""), round(sum([1/i for i in best_list]), 14)) print("Best list len", len(best_list)) if len(self.lst) > 899: with open("lists2.csv", "a") as writer: writer.writelines(f"{str(len(self.lst))}, " f"{str(datetime.utcnow())}, " f"{str(self.lst)}\n") for self.index in self.lst: self.splitter() if self.done: # if self.counter > 300: # if len(self.lst) > 800: # with open("lists.csv", "a") as writer: # writer.writelines(f"{str(len(self.lst))}, " # f"{str(datetime.utcnow())}, " # f"{str(self.lst)}\n") # if len(best_list) < len(self.lst): # best_list = self.lst # print("Most recent list:") # print(str(self.lst).replace(" ", ""), # round(sum([1/i for i in self.lst]), 14)) # print("Recent list len", len(self.lst)) # print() # print("Best list so far:") # print(str(best_list).replace(" ", ""), # round(sum([1/i for i in best_list]), 14)) # print("Best list len", len(best_list)) # print("At:", datetime.utcnow()) # print("\nRestarting list…") # self.lst = self.original[:] # self.counter = 0 # else: self.reloop_self() self.done = True self.counter += 1 - Assistant
- User
write a python script to find a set of unique values 2 < x < 2023 and when all the values of x are over one like 1/x and summed together they equal 1
- Assistant
You can use a brute-force algorithm to find a combination of unique values between 2 and 2023, where the sum of their reciprocals equals one. Note that this may not be the most efficient method, and it might take some time to find the right combination. Here's a simple implementation:
import itertools from fractions import Fraction def find_combination(start, end): for length in range(1, end - start + 1): for combination in itertools.combinations(range(start, end + 1), length): fractions = [Fraction(1, x) for x in combination] if sum(fractions) == 1: return combination return None start, end = 2, 2023 result = find_combination(start, end) if result is not None: print("Combination found:", result) else: print("No combination found.")This implementation uses itertools and the fractions module from Python's standard library. It iterates over all possible combinations (of varying lengths) within the given range, checks if the sum of their reciprocals is equal to one, and returns the combination if found, otherwise returns
None.Keep in mind that the given solution is not at all optimized and might take a long time to execute.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.