Facebook
From Hasan Basri Balaban, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 141
  1.  
  2. public class Pioneers {
  3.  
  4.     static int[][] input = {{215},
  5.                             {193, 124},
  6.                             {117, 237, 442},
  7.                             {218, 935, 347, 235},
  8.                             {320, 804, 522, 417, 345},
  9.                             {229, 601, 723, 835, 133, 124},
  10.                             {248, 202, 277, 433, 207, 263, 257},
  11.                             {359, 464, 504, 528, 516, 716, 871, 182},
  12.                             {461, 441, 426, 656, 863, 560, 380, 171, 923},
  13.                             {381, 348, 573, 533, 447, 632, 387, 176, 975, 449},
  14.                             {223, 711, 445, 645, 245, 543, 931, 532, 937, 541, 444},
  15.                             {330, 131, 333, 928, 377, 733, 17, 778, 839, 168, 197, 197},
  16.                             {131, 171, 522, 137, 217, 224, 291, 413, 528, 520, 227, 229, 928},
  17.                             {223, 626, 34, 683, 839, 53, 627, 310, 713, 999, 629, 817, 410, 121},
  18.                             {924, 622, 911, 233, 325, 139, 721, 218, 253, 223, 107, 233, 230, 124, 233}};
  19.  
  20.     static boolean isPrime(int n) {
  21.         if(n == 2)
  22.             return true;
  23.  
  24.         // if n is even or equals to 1, return false
  25.         if(n % 2 == 0 || n == 1)
  26.             return  false;
  27.  
  28.         // else check for odd dividers
  29.         for(int i = 3; i <= Math.sqrt(n); i+=2) {
  30.             if(n % i == 0)
  31.                 return false;
  32.         }
  33.  
  34.         return true;
  35.     }
  36.  
  37.     static int increment(int i, int j, int rowC) {
  38.         //check for primality
  39.         if(isPrime(input[i][j]))
  40.             return 0;
  41.  
  42.         //check for the end of the array
  43.         if(i+1 == rowC)
  44.             return input[i][j];
  45.  
  46.         int max = 0;
  47.  
  48.         //check down left
  49.         if(j != 0)
  50.             max = Math.max(max, increment(i+1, j-1, rowC));
  51.  
  52.         //check down
  53.         max = Math.max(max, increment(i+1, j, rowC));
  54.  
  55.         //check down right
  56.         max = Math.max(max, increment(i+1, j+1, rowC));
  57.  
  58.         return max + input[i][j];
  59.     }
  60.  
  61.     public static void main(String[] args) {
  62.         System.out.println(increment(0, 0, input.length));
  63.     }
  64.  
  65. }
  66.