Facebook
From Giray Uçar, 3 Years ago, written in Java.
This paste is a reply to Orthogonal triangle from Giray Uçar - go back
Embed
Viewing differences between Orthogonal triangle and Re: Orthogonal triangle
package deneme1;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedList;
import java.util.Scanner;
 

public class Main {
        static int sum = 0;
        
        //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{
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 is :"+ sum);
                
                
println(sum);
        }
        
  // TO FIND HOW MANY COLUMNS ARE THERE IN THE FILE\n        
        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 boolean isNonPrime(int number) int findSum(LinkedList array,int column) {
                if (number <= 1) { 
            return false; 
                }
                if (number <= 3) {
            return false; 
                }
int sum = 0;
                int tempNum1 = 0;
                int tempNum2 = 0;
                int currentColumn = 1;
                
                for (int i = 2; i < number; i++) {
            
while(currentColumn != column+1) {
                        
if (number % i == 0) {
                return true; 
            }
                }
                return true;
        }
        
        
        public static void findSum(LinkedList array,int column) {
                int tempMax = 0;
                
                for (int currentColumn=1; currentColumn<=column; currentColumn++) {
                        
                            // FIRST COLUMN
                                
(currentColumn==1) {
                                
if (currentColumn == 1 && isNonPrime(array.(isPrime(array.get(0))) {
                                        sum = sum + += array.get(selectedIndex);
get(0);
                                        array.removeFirst();
                                }
                                
                                // 2 OR MORE BIGGER
                                else if (currentColumn != 1 ) {
                                        if (array.get(selectedIndex) > array.get(selectedIndex+1)) {
                                                tempMax = array.get(selectedIndex);
                                                if (isNonPrime(tempMax)) {
                                                        sum = sum  + tempMax;
                                                }
                                        }
                                        else if (array.get(selectedIndex+1) > array.get(selectedIndex)) {
                                                tempMax = array.get(selectedIndex+1);
                                                if (isNonPrime(tempMax)) {
                                                        sum = sum + tempMax;
                                                        selectedIndex = selectedIndex + 1;
                                                }
                                        }
                                        
                                        // FOR DELETING ELEMENTS FROM ARRAY
                                        for (int j=0;j                                                 array.removeFirst();
                                        }
                                }
                                else {
                                        System.out.println("First Number is a prime number");
                                        break;
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;
        }
        

}