def is_prime(n): if n == 1: return False elif n == 2: return True else: for x in range(2, n): if n % x == 0: return False return True filename = 'lk_text.txt' with open(filename, "r") as ins: array = [] for line in ins: array.append(line) newArray = [] for i in array: j = i.split(' ') k = [int(n) for n in j] newArray.append(k) l = len(newArray) for i in range(l-1): array1 = newArray[-1] array2 = newArray[-2] for j in range(len(array2)): if not (is_prime(array1[j])): array2[j] += max(array1[j], array1[j + 1]) newArray.pop(-1) newArray[-1] = array2 print(newArray[0][0])