# YEKTA CAN TURSUN # We need a function to check whether the number is prime or not # We can use basic implementation, check all number from 2 to N-1 and # If there is not any number we can say that the number is prime def isPrime(N): if N == 1: return False i = 2 while i < N: # This statement check whether N/i results is integer or not # If it is true we can say that there is a number thus N is not prime if N % i == 0: return False i += 1 return True # At that point, we need to implement input reader # We can read the input and returns the value as a list object # I am going to assume that we are reading the input from txt file # and it is spaced with " ", end with "\n" and there is "eof" def readInput(adressOfFile): lines = [] with open(adressOfFile) as file1: lines = [line.rstrip() for line in file1] result = [] for x in lines: result2Int = [int(i) for i in x.split(" ")] result.append(result2Int) return result # Let's wread the input file inputMatrix = readInput("input1.txt") print(inputMatrix) # We can use recursivly try every possible ways to traverse the tree like structure and write all possible results # Then select max value in the list results = [] def traverse(cordinateY,cordinateX,sum): # Firstly lets check is prime if isPrime(inputMatrix[cordinateY][cordinateX]): return else: #Let's check are we end of the pyrimid if cordinateY+1 < len(inputMatrix): #Let's add this node's number sumAfter = sum + inputMatrix[cordinateY][cordinateX] # We can go down or diagonally # Go down traverse(cordinateY+1,cordinateX,sumAfter) #Right diagonally traverse(cordinateY+1,cordinateX+1,sumAfter) # Left diagonally #Let's assume that we cannot capable of go to left #I am assuing input is sqaure pyramid # We need to check that are we most left position or not if cordinateX != 0: pass #traverse(cordinateY+1,cordinateX-1,sumAfter) else: # We are at the end of the pyrimid # Adding the sum to result list and returning results.append(sum+inputMatrix[cordinateY][cordinateX]) return # If we cannot go any down which can mean that # this node's down node and both diagonal nodes are prime number, # let's add to the results results.append(sum+inputMatrix[cordinateY][cordinateX]) return # Traverse thorugh the tree like strucutre traverse(0,0,0) #Printing the results print(max(results)) # NOTES It is not optimized way to do this # Code can be further optimized