Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.nio.file.Files;
- import java.nio.file.Paths;
- import java.util.Collections;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.stream.Stream;
- public class Main
- {
- static List<Integer> luckyNums = new LinkedList<>();
- static List<String> input;
- static int luckyNumsCounter = 0, luckyPrimesCounter = 0;
- public static void main(String[] args)
- {
- GenerateLucky();
- try(Stream<String> file = Files.lines(Paths.get("dane.txt")))
- {
- input = file.toList();
- input.forEach(x->IsLucky(Integer.parseInt(x)));
- input.forEach(x->PrimeCounter(Integer.parseInt(x)));
- }
- catch(Exception ex)
- {
- System.out.println(ex.toString());
- }
- System.out.println(luckyNumsCounter);
- LongestString();
- System.out.println(luckyPrimesCounter);
- }
- public static void IsLucky(int x)
- {
- if(luckyNums.contains(x))
- luckyNumsCounter++;
- }
- public static void PrimeCounter(int x)
- {
- if(x < 2 || !(luckyNums.contains(x)))
- return;
- for(int i = 2; i*i<=x; i++)
- {
- if(x%i==0)
- return;
- }
- luckyPrimesCounter++;
- }
- public static void LongestString()
- {
- int longestLength = 0, longestIndex = 0, length = 0;
- for (int i = 0; i < input.size(); i++)
- {
- int j = i;
- length = 0;
- while(luckyNums.contains(Integer.parseInt(input.get(j))))
- {
- j++;
- length++;
- }
- if(length>longestLength)
- {
- longestLength = length;
- longestIndex = j - length;
- }
- }
- System.out.println(input.get(longestIndex) + " " + longestLength);
- }
- public static void GenerateLucky()
- {
- for(int i = 1; i < 10001; i+=2)
- luckyNums.add(i);
- for(int i = 1; i < luckyNums.size(); i++)
- {
- int toAdd = luckyNums.get(i);
- for(int j = toAdd-1; j < luckyNums.size(); j += toAdd-1)
- {
- luckyNums.remove(j);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement