Advertisement
MIstrzEgiptu

Maturka dżawa

Mar 20th, 2023
671
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.24 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.         }
  22.         catch(Exception ex)
  23.         {
  24.             System.out.println(ex.toString());
  25.         }
  26.         System.out.println(luckyNumsCounter);
  27.         LongestString();
  28.         System.out.println(luckyPrimesCounter);
  29.     }
  30.     public static void IsLucky(int x)
  31.     {
  32.         if(luckyNums.contains(x))
  33.             luckyNumsCounter++;
  34.     }
  35.     public static void PrimeCounter(int x)
  36.     {
  37.         if(x < 2 || !(luckyNums.contains(x)))
  38.             return;
  39.         for(int i = 2; i*i<=x; i++)
  40.         {
  41.             if(x%i==0)
  42.                 return;
  43.         }
  44.         luckyPrimesCounter++;
  45.     }
  46.     public static void LongestString()
  47.     {
  48.         int longestLength = 0, longestIndex = 0, length = 0;
  49.         for (int i = 0; i < input.size(); i++)
  50.         {
  51.             int j = i;
  52.             length = 0;
  53.             while(luckyNums.contains(Integer.parseInt(input.get(j))))
  54.             {
  55.                 j++;
  56.                 length++;
  57.             }
  58.             if(length>longestLength)
  59.             {
  60.                 longestLength = length;
  61.                 longestIndex = j - length;
  62.             }
  63.         }
  64.         System.out.println(input.get(longestIndex) + " " + longestLength);
  65.     }
  66.     public static void GenerateLucky()
  67.     {
  68.         for(int i = 1; i < 10001; i+=2)
  69.             luckyNums.add(i);
  70.         for(int i = 1; i < luckyNums.size(); i++)
  71.         {
  72.             int toAdd = luckyNums.get(i);
  73.             for(int j = toAdd-1; j < luckyNums.size(); j += toAdd-1)
  74.             {
  75.                 luckyNums.remove(j);
  76.             }
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement