Facebook
From Giray Uçar, 3 Years ago, written in Java.
Embed
Download Paste or View Raw
Hits: 177
  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.         static int sum = 0;
  10.         static int selectedIndex = 0;
  11.        
  12.         public static void main(String[] args) throws Exception{
  13.                
  14.                 Path filePath = Paths.get("/Users/Giray/Desktop/test.txt");
  15.                 Scanner scanner = new Scanner(filePath);
  16.                
  17.                
  18.                 LinkedList<Integer> integersFromFile = new LinkedList<Integer>();
  19.                
  20.                 while (scanner.hasNext()) {
  21.                     if (scanner.hasNextInt()) {
  22.                         integersFromFile.add(scanner.nextInt());
  23.                     } else {
  24.                         scanner.next();
  25.                     }
  26.                 }
  27.                
  28.                 findSum(integersFromFile, findColumnCount(integersFromFile));
  29.                 System.out.println("Sum is :"+ sum);
  30.                
  31.                
  32.         }
  33.        
  34.   // TO FIND HOW MANY COLUMNS ARE THERE IN THE FILE
  35.         public static int findColumnCount(LinkedList<Integer> array) {
  36.                 int totalItemCount = array.size();
  37.                 int firstNumber = 1;
  38.                 int countOfColumns = 0;
  39.                 while (totalItemCount != 0 ) {
  40.                         countOfColumns++;
  41.                         totalItemCount = totalItemCount - firstNumber;
  42.                         firstNumber++;
  43.                 }
  44.                 return countOfColumns;
  45.         }
  46.        
  47.        
  48.         public static boolean isNonPrime(int number) {
  49.                 if (number <= 1) {
  50.             return false;
  51.                 }
  52.                 if (number <= 3) {
  53.             return false;
  54.                 }
  55.                
  56.                 for (int i = 2; i < number; i++) {
  57.             if (number % i == 0) {
  58.                 return true;
  59.             }
  60.                 }
  61.                 return true;
  62.         }
  63.        
  64.        
  65.         public static void findSum(LinkedList<Integer> array,int column) {
  66.                 int tempMax = 0;
  67.                
  68.                 for (int currentColumn=1; currentColumn<=column; currentColumn++) {
  69.                        
  70.                             // FIRST COLUMN
  71.                                 if (currentColumn == 1 && isNonPrime(array.get(0))) {
  72.                                         sum = sum + array.get(selectedIndex);
  73.                                         array.removeFirst();
  74.                                 }
  75.                                
  76.                                 // 2 OR MORE BIGGER
  77.                                 else if (currentColumn != 1 ) {
  78.                                         if (array.get(selectedIndex) > array.get(selectedIndex+1)) {
  79.                                                 tempMax = array.get(selectedIndex);
  80.                                                 if (isNonPrime(tempMax)) {
  81.                                                         sum = sum  + tempMax;
  82.                                                 }
  83.                                         }
  84.                                         else if (array.get(selectedIndex+1) > array.get(selectedIndex)) {
  85.                                                 tempMax = array.get(selectedIndex+1);
  86.                                                 if (isNonPrime(tempMax)) {
  87.                                                         sum = sum + tempMax;
  88.                                                         selectedIndex = selectedIndex + 1;
  89.                                                 }
  90.                                         }
  91.                                        
  92.                                         // FOR DELETING ELEMENTS FROM ARRAY
  93.                                         for (int j=0;j<currentColumn;j++) {
  94.                                                 array.removeFirst();
  95.                                         }
  96.                                 }
  97.                                 else {
  98.                                         System.out.println("First Number is a prime number");
  99.                                         break;
  100.                                 }
  101.                        
  102.                 }
  103.         }
  104.        
  105.  
  106. }
  107.  
  108.  
  109.  

Replies to Orthogonal triangle rss

Title Name Language When
Re: Orthogonal triangle Giray Uçar java 3 Years ago.