import sys 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 ): return 0 res = tri[i][j] res += max(maxSum(tri, i + 1, j, 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) for row in pyramid: for i in range(len(row)): row[i] = int(row[i]) for row in pyramid: for i in range(len(pyramid[-1])-len(row)): row.append(0) for i in range(len(pyramid)): for j in range(len(pyramid)): if isPrime(pyramid[i][j]): pyramid[i][j] = -sys.maxsize - 1 #to eliminate prime numbers, I assign max negative value to those cells print("The maximum sum is ", maxSum(pyramid, 0, 0, len(pyramid)))