package Pi; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.Scanner; public class Pı { public static boolean isPrime(int num) { if (num < 2){ return false; } if (num == 2 || num == 3) { return true; } if (num % 2 == 0 || num % 3 == 0) { return false; } for (int i = 3; i < Math.sqrt(num); i += 2) { if (num % i == 0 || num % Math.sqrt(num) == 0) { return false; } } return true; } public static int[] convertInt(String[] arr){ int[] intArr = new int[arr.length]; for(int i = 0; i < arr.length; i++){ intArr[i] = Integer.parseInt(arr[i]); } return intArr; } public static String inputPath(){ Scanner myObj = new Scanner(System.in); System.out.println("Enter the directory of the txt file. Ex: C:\\Users\\Mustafa\\Desktop\\deneme.txt "); String dirPath = myObj.nextLine(); return dirPath; } public static ArrayList readFile(String textName){ String line; String[] numberLine; ArrayList arrList = new ArrayList<>(); try { File file = new File(textName); FileReader fr = new FileReader(file); BufferedReader br = new BufferedReader(fr); while((line = br.readLine()) != null){ numberLine = line.split(" "); arrList.add(convertInt(numberLine)); } } catch (FileNotFoundException e){ System.out.println("File not found."); } catch (IOException ex) { System.out.println("IO exception."); } return arrList; } public static void indicatePrime(ArrayList arr){ Iterator iter = arr.iterator(); while(iter.hasNext()){ int[] tempArr = iter.next(); for(int i = 0; i < tempArr.length; i++){ if(isPrime(tempArr[i])){ tempArr[i] = -1; } } } } public static void printMax(int[] arr){ int max = -1; for(int i = 0; i < arr.length; i++){ if(arr[i] > max) max = arr[i]; } System.out.println("Max sum: " + max); } public static void deactivateNonRoad(int[] arr1, int[] arr2){ for(int i = 0; i < arr1.length; i++){ if(arr1[i] == arr2[i]){ arr1[i] = -1; } } } public static void walkDownwards(ArrayList arrList){ for(int i = 0; i < arrList.size(); i++){ if(i == arrList.size() - 1){ System.out.println("Max depth: " + i); printMax(arrList.get(i)); break; } int[] tempArray = new int[arrList.get(i+1).length]; System.arraycopy(arrList.get(i+1), 0, tempArray, 0, tempArray.length); for(int j = 0; j < arrList.get(i).length; j++){ if(j == 0 && arrList.get(i)[j] != -1){ if(arrList.get(i+1)[j] != -1){ if(arrList.get(i+1)[j] == tempArray[j]){ arrList.get(i+1)[j] += arrList.get(i)[j]; } } if(arrList.get(i+1)[j+1] != -1){ if(arrList.get(i+1)[j+1] == tempArray[j+1]){ arrList.get(i+1)[j+1] += arrList.get(i)[j]; } } } if(j > 0 && arrList.get(i)[j] != -1){ if(arrList.get(i+1)[j-1] != -1){ if(arrList.get(i+1)[j-1] == tempArray[j-1]){ arrList.get(i+1)[j-1] += arrList.get(i)[j]; } else{ if(arrList.get(i+1)[j-1] < (tempArray[j-1] + arrList.get(i)[j])){ arrList.get(i+1)[j-1] = tempArray[j-1] + arrList.get(i)[j]; } } } if(arrList.get(i+1)[j] != -1){ if(arrList.get(i+1)[j] == tempArray[j]){ arrList.get(i+1)[j] += arrList.get(i)[j]; } else{ if(arrList.get(i+1)[j] < (tempArray[j] + arrList.get(i)[j])){ arrList.get(i+1)[j] = tempArray[j] + arrList.get(i)[j]; } } } if(arrList.get(i+1)[j+1] != -1){ if(arrList.get(i+1)[j+1] == tempArray[j+1]){ arrList.get(i+1)[j+1] += arrList.get(i)[j]; } else{ if(arrList.get(i+1)[j+1] < (tempArray[j+1] + arrList.get(i)[j])){ arrList.get(i+1)[j+1] = tempArray[j+1] + arrList.get(i)[j]; } } } } } if(Arrays.equals(tempArray, arrList.get(i+1))){ System.out.println("Max depth: " + (i+1)); printMax(arrList.get(i)); break; } deactivateNonRoad(arrList.get(i+1), tempArray); } } public static void main(String[] args) { String input = inputPath(); ArrayList triangle = readFile(input); indicatePrime(triangle); walkDownwards(triangle); } }