public static void Main()
string filename = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "\\input.txt";
int[,] inputTriangle = readInput(filename);
if (inputTriangle != null)
int posSolutions = (int)Math.Pow(2, inputTriangle.GetLength(0) - 1);
int lines = inputTriangle.GetLength(0);
for (int i = 0; i <= posSolutions; i++)
tempSum = inputTriangle[0, 0];
for (int j = 0; j < inputTriangle.GetLength(0) - 1; j++)
index = index + (i >> j & 1);
tempSum += inputTriangle[j + 1, index];
if (tempSum > largestSum)
Console.WriteLine("The largest sum through the triangle is: {0}", largestSum);
Console.WriteLine("Please enter your input in correct format!");
private static int[,] readInput(string filename)
StreamReader r = new StreamReader(filename);
while ((line = r.ReadLine()) != null)
int[,] inputTriangle = new int[lines, lines];
r.BaseStream.Seek(0, SeekOrigin.Begin);
while ((line = r.ReadLine()) != null)
linePieces = line.Split(' ');
for (int i = 0; i < linePieces.Length; i++)
if (isPrime(int.Parse(linePieces[i])))
inputTriangle[j, i] = int.Parse(linePieces[i]);
public static bool isPrime(int number)
if (number == 1) return false;
if (number == 2) return true;
var boundary = (int)Math.Floor(Math.Sqrt(number));
for (int i = 2; i <= boundary; ++i)
if (number % i == 0) return false;