lines = [] print("Paste triangle here:") while True: line = input() if line: lines.append(line) else: break text = ' '.join(lines) numbers = list(map(int, (list(text.split(" "))))) noprimelist = [] for s in numbers: if s > 1: for i in range(2, s): if (s % i) == 0: noprimelist.append(s) break else: noprimelist.append(0) elif s == 1: noprimelist.append(s) rowamount1 = int((((8*(len(noprimelist)) + 1)** 0.5) - 1)//2) rowlist = [] for a in range(rowamount1): b = a + 1 x = (a*(a + 1))//2 y = (b*(b +1))//2 rowlist.append(noprimelist[x:y]) for t in range(len(rowlist)): if t == 0 and sum(rowlist[t]) == 0: del rowlist[0: len(rowlist)] rowlist.append([0]) break if t > 0 and sum(rowlist[t]) == 0: del rowlist[t: (len(rowlist))] break else: pass g = len(rowlist)-1 s = 0 nextrange = range(g-1, -1, -1) for n in nextrange: while s <= n: if rowlist[n][s] != 0: if s == 0: rowlist[n][s] = max((rowlist[n][s] + rowlist[n+1][s]), (rowlist[n][s] + rowlist[n+1][s+1])) elif s != 0: rowlist[n][s] = max((rowlist[n][s] + rowlist[n+1][s-1]), (rowlist[n][s] + rowlist[n+1][s]), (rowlist[n][s] + rowlist[n+1][s+1])) s = s + 1 s = 0 print("Maximum sum", rowlist[0][0])