package deneme1; import java.nio.file.Path; import java.nio.file.Paths; import java.util.LinkedList; import java.util.Scanner; public class Main { //SINCE BECAUSE OF GIVEN RULES,NEXT INDEX CAN BE ONLY OLD INDEX OR +1 OF OLD INDEX static int selectedIndex = 0; public static void main(String[] args) throws Exception { Path filePath = Paths.get("/Users/Giray/Desktop/test.txt"); Scanner scanner = new Scanner(filePath); LinkedList integersFromFile = new LinkedList(); while (scanner.hasNext()) { if (scanner.hasNextInt()) { integersFromFile.add(scanner.nextInt()); } else { scanner.next(); } } int sum = findSum(integersFromFile, findColumnCount(integersFromFile)); System.out.println(sum); } public static int findColumnCount(LinkedList array) { int totalItemCount = array.size(); int firstNumber = 1; int countOfColumns = 0; while (totalItemCount != 0 ) { countOfColumns++; totalItemCount = totalItemCount - firstNumber; firstNumber++; } return countOfColumns; } public static boolean isPrime(int n) { if (n <= 1) { return true; } for (int i = 2; i < n; i++) { if (n % i == 0) { return true; } } return false; } public static int findSum(LinkedList array,int column) { int sum = 0; int tempNum1 = 0; int tempNum2 = 0; int currentColumn = 1; while(currentColumn != column+1) { if (currentColumn==1) { if (isPrime(array.get(0))) { sum += array.get(0); array.removeFirst(); } else { return 0; } } else { tempNum1 = array.get(selectedIndex); tempNum2 = array.get(selectedIndex+1); //BOTH NOT PRIME AND TEMP1 BIGGER THAN TEMP2 if (isPrime(tempNum1) && isPrime(tempNum2) && tempNum1> tempNum2) { sum += tempNum1; } // BOTH NOT PRIME A ND TEMP2 BIGGER THAN TEMP1 else if (isPrime(tempNum1) && isPrime(tempNum2) && tempNum2 > tempNum1) { sum += tempNum2; selectedIndex += 1; } // BOTH PRIME else if (!isPrime(tempNum1) && !isPrime(tempNum2)) { return sum; } // ONLY TEMP2 NOT PRIME else if (!isPrime(tempNum1) && isPrime(tempNum2)) { sum += tempNum2; selectedIndex += 1; } // ONLY TEMP1 NOT PRIME else if (!isPrime(tempNum2) && isPrime(tempNum1)) { sum += tempNum1; } for (int i=1;i<=currentColumn;i++) { array.removeFirst(); } } currentColumn += 1; } return sum; } }