Facebook
From Giray Uçar, 3 Years ago, written in Java.
This paste is a reply to Orthogonal triangle from Giray Uçar - view diff
Embed
Download Paste or View Raw
Hits: 194
  1. package deneme1;
  2. import java.nio.file.Path;
  3. import java.nio.file.Paths;
  4. import java.util.LinkedList;
  5. import java.util.Scanner;
  6.  
  7.  
  8. public class Main {
  9.        
  10.         //SINCE BECAUSE OF GIVEN RULES,NEXT INDEX CAN BE ONLY OLD INDEX OR +1 OF OLD INDEX
  11.         static int selectedIndex = 0;
  12.        
  13.         public static void main(String[] args) throws Exception {
  14.                
  15.                 Path filePath = Paths.get("/Users/Giray/Desktop/test.txt");
  16.                 Scanner scanner = new Scanner(filePath);
  17.                
  18.                 LinkedList<Integer> integersFromFile = new LinkedList<Integer>();
  19.                 while (scanner.hasNext()) {
  20.                     if (scanner.hasNextInt()) {
  21.                         integersFromFile.add(scanner.nextInt());
  22.                     } else {
  23.                         scanner.next();
  24.                     }
  25.                 }
  26.                
  27.                 int sum = findSum(integersFromFile, findColumnCount(integersFromFile));
  28.                 System.out.println(sum);
  29.         }
  30.        
  31.        
  32.         public static int findColumnCount(LinkedList<Integer> array) {
  33.                 int totalItemCount = array.size();
  34.                 int firstNumber = 1;
  35.                 int countOfColumns = 0;
  36.                 while (totalItemCount != 0 ) {
  37.                         countOfColumns++;
  38.                         totalItemCount = totalItemCount - firstNumber;
  39.                         firstNumber++;
  40.                 }
  41.                 return countOfColumns;
  42.         }
  43.        
  44.         public static boolean isPrime(int n) {
  45.         if (n <= 1) {
  46.                 return true;
  47.         }
  48.         for (int i = 2; i < n; i++) {
  49.                 if (n % i == 0) {
  50.                         return true;
  51.                 }
  52.         }
  53.         return false;
  54.     }
  55.  
  56.        
  57.         public static int findSum(LinkedList<Integer> array,int column) {
  58.                 int sum = 0;
  59.                 int tempNum1 = 0;
  60.                 int tempNum2 = 0;
  61.                 int currentColumn = 1;
  62.                
  63.                 while(currentColumn != column+1) {
  64.                         if (currentColumn==1) {
  65.                                 if (isPrime(array.get(0))) {
  66.                                         sum += array.get(0);
  67.                                         array.removeFirst();
  68.                                 }
  69.                                 else {
  70.                                         return 0;
  71.                                 }
  72.                         }
  73.                         else {
  74.                                 tempNum1 = array.get(selectedIndex);
  75.                                 tempNum2 = array.get(selectedIndex+1);
  76.                                
  77.                                 //BOTH NOT PRIME AND TEMP1 BIGGER THAN TEMP2
  78.                                 if (isPrime(tempNum1) && isPrime(tempNum2) && tempNum1> tempNum2) {
  79.                                         sum  += tempNum1;
  80.                                 }
  81.                                 // BOTH NOT PRIME A ND TEMP2 BIGGER THAN TEMP1
  82.                                 else if  (isPrime(tempNum1) && isPrime(tempNum2) && tempNum2 > tempNum1) {
  83.                                         sum += tempNum2;
  84.                                         selectedIndex += 1;
  85.                                 }
  86.                                 // BOTH PRIME
  87.                                 else if (!isPrime(tempNum1) &&  !isPrime(tempNum2)) {
  88.                                         return sum;
  89.                                 }
  90.                                 // ONLY TEMP2 NOT PRIME
  91.                                 else if (!isPrime(tempNum1) && isPrime(tempNum2)) {
  92.                                         sum += tempNum2;
  93.                                         selectedIndex += 1;
  94.                                 }
  95.                                 // ONLY TEMP1 NOT PRIME
  96.                                 else if (!isPrime(tempNum2) && isPrime(tempNum1)) {
  97.                                         sum += tempNum1;
  98.                                 }
  99.                                 for (int i=1;i<=currentColumn;i++) {
  100.                                         array.removeFirst();
  101.                                 }
  102.                         }
  103.                         currentColumn += 1;
  104.                 }
  105.                 return sum;
  106.         }
  107.        
  108.  
  109. }
  110.  
  111.  
  112.