Advertisement
MIstrzEgiptu

Maturka dżawa

Mar 20th, 2023
589
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.27 KB | None | 0 0
  1. import java.nio.file.Files;
  2. import java.nio.file.Paths;
  3. import java.util.Collections;
  4. import java.util.LinkedList;
  5. import java.util.List;
  6. import java.util.stream.Stream;
  7.  
  8. public class Main
  9. {
  10.     static List<Integer> luckyNums = new LinkedList<>();
  11.     static List<String> input;
  12.     static int luckyNumsCounter = 0, luckyPrimesCounter = 0;
  13.     public static void main(String[] args)
  14.     {
  15.         GenerateLucky();
  16.         try(Stream<String> file = Files.lines(Paths.get("dane.txt")))
  17.         {
  18.             input = file.toList();
  19.             input.forEach(x->IsLucky(Integer.parseInt(x)));
  20.             input.forEach(x->PrimeCounter(Integer.parseInt(x)));
  21.             //file.forEach(x->System.out.println(x));
  22.         }
  23.         catch(Exception ex)
  24.         {
  25.             System.out.println(ex.toString());
  26.         }
  27.         System.out.println(luckyNumsCounter);
  28.         LongestString();
  29.         System.out.println(luckyPrimesCounter);
  30.     }
  31.     public static void IsLucky(int x)
  32.     {
  33.         if(luckyNums.contains(x))
  34.             luckyNumsCounter++;
  35.     }
  36.     public static void PrimeCounter(int x)
  37.     {
  38.         if(x < 2)
  39.             return;
  40.         for(int i = 2; i*i<x; i++)
  41.         {
  42.             if(x%i==0)
  43.                 return;
  44.         }
  45.         luckyPrimesCounter++;
  46.     }
  47.     public static void LongestString()
  48.     {
  49.         int longestLength = 0, longestIndex = 0, length = 0;
  50.         for (int i = 0; i < input.size(); i++)
  51.         {
  52.             int j = i;
  53.             length = 0;
  54.             while(luckyNums.contains(Integer.parseInt(input.get(j))))
  55.             {
  56.                 j++;
  57.                 length++;
  58.             }
  59.             if(length>longestLength)
  60.             {
  61.                 longestLength = length;
  62.                 longestIndex = j - length;
  63.             }
  64.         }
  65.         System.out.println(input.get(longestIndex) + " " + longestLength);
  66.     }
  67.     public static void GenerateLucky()
  68.     {
  69.         for(int i = 1; i < 10001; i+=2)
  70.             luckyNums.add(i);
  71.         for(int i = 1; i < luckyNums.size(); i++)
  72.         {
  73.             int toAdd = luckyNums.get(i);
  74.             for(int j = toAdd-1; j < luckyNums.size(); j += toAdd-1)
  75.             {
  76.                 luckyNums.remove(j);
  77.             }
  78.         }
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement