Facebook
From Suat Kuran, 3 Years ago, written in C#.
Embed
Download Paste or View Raw
Hits: 120
  1. using System;
  2.  
  3. class Program
  4. {
  5.     static void Main(string[] args)
  6.     {
  7.  
  8.  
  9.         //get input
  10.         var input = GetInput();
  11.  
  12.         string[] arrayOfRowsByNewlines = input.Split('\n');
  13.  
  14.         var tableHolder = FlattenTheTriangleIntoTable(arrayOfRowsByNewlines);
  15.  
  16.         var result = WalkThroughTheNode(arrayOfRowsByNewlines, tableHolder);
  17.  
  18.         Console.WriteLine($"The Maximum Total Sum Of Non-Prime Numbers From Top To Bottom Is:  {result[0,0]}");
  19.  
  20.         Console.ReadKey();
  21.     }
  22.  
  23.     private static string GetInput()
  24.     {
  25.  
  26.             const string input = @"   215
  27.                                   193 124
  28.                                  117 237 442
  29.                                218 935 347 235
  30.                              320 804 522 417 345
  31.                            229 601 723 835 133 124
  32.                          248 202 277 433 207 263 257
  33.                        359 464 504 528 516 716 871 182
  34.                      461 441 426 656 863 560 380 171 923
  35.                     381 348 573 533 447 632 387 176 975 449
  36.                   223 711 445 645 245 543 931 532 937 541 444
  37.                 330 131 333 928 377 733 017 778 839 168 197 197
  38.                131 171 522 137 217 224 291 413 528 520 227 229 928
  39.              223 626 034 683 839 053 627 310 713 999 629 817 410 121
  40.            924 622 911 233 325 139 721 218 253 223 107 233 230 124 233";
  41.         return input;
  42.     }
  43.  
  44.     private static int[,] WalkThroughTheNode(string[] arrayOfRowsByNewlines, int[,] tableHolder)
  45.     {
  46.         // walking through the non-prime node
  47.         for (int i = arrayOfRowsByNewlines.Length - 2; i >= 0; i--)
  48.         {
  49.             for (int j = 0; j < arrayOfRowsByNewlines.Length; j++)
  50.             {
  51.                 //only sum through the non-prime node
  52.                 if ((!IsPrime(tableHolder[i, j])))
  53.                 {
  54.                     tableHolder[i, j] = Math.Max(tableHolder[i, j] + tableHolder[i + 1, j],
  55.                         tableHolder[i, j] + tableHolder[i + 1, j + 1]);
  56.                 }
  57.             }
  58.         }
  59.         return tableHolder;
  60.     }
  61.  
  62.     private static int[,] FlattenTheTriangleIntoTable(string[] arrayOfRowsByNewlines)
  63.     {
  64.         int[,] tableHolder = new int[arrayOfRowsByNewlines.Length, arrayOfRowsByNewlines.Length + 1];
  65.  
  66.         for (int row = 0; row < arrayOfRowsByNewlines.Length; row++)
  67.         {
  68.             var eachCharactersInRow = arrayOfRowsByNewlines[row].Trim().Split(' ');
  69.  
  70.             for (int column = 0; column < eachCharactersInRow.Length; column++)
  71.             {
  72.                 int number;
  73.                 int.TryParse(eachCharactersInRow[column], out number);
  74.                 tableHolder[row, column] = number;
  75.             }
  76.         }
  77.         return tableHolder;
  78.     }
  79.  
  80.     public static bool IsPrime(int number)
  81.     {
  82.         // Test whether the parameter is a prime number.
  83.         if ((number & 1) == 0)
  84.         {
  85.             if (number == 2)
  86.             {
  87.                 return true;
  88.             }
  89.             return false;
  90.         }
  91.  
  92.         for (int i = 3; (i * i) <= number; i += 2)
  93.         {
  94.             if ((number % i) == 0)
  95.             {
  96.                 return false;
  97.             }
  98.         }
  99.         return number != 1;
  100.     }
  101.  
  102.  
  103.  
  104.  
  105. }