SHARE
TWEET

Untitled

a guest Nov 20th, 2019 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.ArrayList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class ErastoSive {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.         List<Boolean> number = new ArrayList<Boolean>();
  9.         String ans = "t";
  10.         while (ans.equalsIgnoreCase("t")) {
  11.  
  12.             System.out.println("Podaj liczbę do której mam " +
  13.                     "wyznaczyć liczby pierwsze: ");
  14.             int max = scanner.nextInt();
  15.  
  16.             for (int i = 0; i <= max; i++) {
  17.                 number.add(true);
  18.             }
  19.  
  20.             for (int i = 2; i * i <= max; i++) {
  21.  
  22.                 if (number.get(i) == true) {
  23.  
  24.                     for (int j = 2; j * i <= max; j++) {
  25.                         number.set(j * i, false);
  26.                     }
  27.                 }
  28.             }
  29.  
  30.             System.out.println("W zakresie do " + max + " liczby pierwsze to:");
  31.  
  32.             for (int n = 2; n < number.size(); n++) {
  33.                 if (number.get(n) == true) {
  34.                     System.out.print(n + " ");
  35.                 }
  36.             }
  37.             number.clear();
  38.             System.out.println("\nJeszcze raz? (t/n)");
  39.             ans = scanner.next();
  40.         }
  41.  
  42.         System.out.println("Koniec");
  43.     }
  44.   }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top