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;
Path filePath = Paths.get("/Users/Giray/Desktop/test.txt");
Scanner scanner = new Scanner(filePath);
LinkedList<Integer> integersFromFile = new LinkedList<Integer>();
while (scanner.hasNext()) {
if (scanner.hasNextInt()) {
integersFromFile.add(scanner.nextInt());
} else {
scanner.next();
}
}
int sum = findSum(integersFromFile, findColumnCount(integersFromFile));
}
public static int findColumnCount(LinkedList<Integer> 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<Integer> 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;
}
}
{"html5":"htmlmixed","css":"css","javascript":"javascript","php":"php","python":"python","ruby":"ruby","lua":"text\/x-lua","bash":"text\/x-sh","go":"go","c":"text\/x-csrc","cpp":"text\/x-c++src","diff":"diff","latex":"stex","sql":"sql","xml":"xml","apl":"apl","asterisk":"asterisk","c_loadrunner":"text\/x-csrc","c_mac":"text\/x-csrc","coffeescript":"text\/x-coffeescript","csharp":"text\/x-csharp","d":"d","ecmascript":"javascript","erlang":"erlang","groovy":"text\/x-groovy","haskell":"text\/x-haskell","haxe":"text\/x-haxe","html4strict":"htmlmixed","java":"text\/x-java","java5":"text\/x-java","jquery":"javascript","mirc":"mirc","mysql":"sql","ocaml":"text\/x-ocaml","pascal":"text\/x-pascal","perl":"perl","perl6":"perl","plsql":"sql","properties":"text\/x-properties","q":"text\/x-q","scala":"scala","scheme":"text\/x-scheme","tcl":"text\/x-tcl","vb":"text\/x-vb","verilog":"text\/x-verilog","yaml":"text\/x-yaml","z80":"text\/x-z80"}