def isPrime(num): if num == 1: return False for i in range(2, num): if num % i == 0: return False return True def maxSum(tri, i, j, size): if(i == size or j < 0 or isPrime(int(tri[i][j]))): return 0 res = int(tri[i][j]) res += max(maxSum(tri, i + 1, j, size), maxSum(tri, i + 1, j + 1, size), maxSum(tri, i + 1, j - 1, size)) return res f = open("/content/drive/My Drive/input.txt","r") text = f.read() rows = text.split('\n') pyramid = [] for row in rows: currentRow = row.split(' ') pyramid.append(currentRow) print("The maximum sum is ", maxSum(pyramid, 0, 0, len(pyramid)))