using System.Collections.Generic;
public class NonPrimeMaxNumTotal
public static Dictionary<int, bool> PrimeCache = new Dictionary<int, bool>();
public static void Main(string[] args)
var input = GetInputPyramid();
string[] rowsByLine = input.Split('\n');
var table = TriangleToTable(rowsByLine);
var setprimetozero = ResetAllPrimeNumbers(table);
var result = ProcessTree(setprimetozero);
Console.WriteLine(result);
private static string GetInputPyramid()
const string input = @" 215
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 053 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233";
private static int ProcessTree(int[,] table)
var length = table.GetLength(0);
for (var i = length - 2; i >= 0; i--)
for (var j = 0; j < length; j++)
var c = tempresult[i, j];
var a = tempresult[i + 1, j];
var b = tempresult[i + 1, j + 1];
if ((!IsPrime(c) && !IsPrime(a)) || (!IsPrime(c) && !IsPrime(b)))
table[i, j] = c + Math.Max(a, b);
private static int[,] TriangleToTable(string[] rowsByLine)
int[,] table = new int[rowsByLine.Length, rowsByLine.Length + 1];
for (int row = 0; row < rowsByLine.Length; row++)
var eachCharactersInRow = rowsByLine[row].Trim().Split(' ');
for (int column = 0; column < eachCharactersInRow.Length; column++)
int.TryParse(eachCharactersInRow[column], out number);
table[row, column] = number;
private static int[,] ResetAllPrimeNumbers(int[,] tableHolder)
var length = tableHolder.GetLength(0);
for (var i = 0; i < length; i++)
for (var j = 0; j < length; j++)
if (tableHolder[i, j] == 0) continue;
if (IsPrime(tableHolder[i, j]))
public static bool IsPrime(int num)
for (int i = 2; i < num; i++)
if (num % i == 0 ) return false;