daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Jan 16th, 2019 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. Metoda którą dajesz pod Button by ci pokazała 10 liczb pierwszych (w przedziale 2-30 jest dokładnie 10 liczb)
  4. public void PrimeNumbers(View view){
  5.         List primeNumbers = sieveOfEratosthenes(30);
  6.         TextView textView = findViewById(R.id.textView);
  7.         textView.setText(TextUtils.join("\n", primeNumbers.subList(0, 10)));
  8.     }
  9.  
  10.  
  11.  
  12. Tu masz algorytm do liczb pierwszych - Sito Erastotenesa (wylicza ilość liczb pierwszych w przedziale 2..N)
  13.  
  14. List<Integer> sieveOfEratosthenes(int n) {
  15.         boolean prime[] = new boolean[n + 1];
  16.         Arrays.fill(prime, true);
  17.         for (int p = 2; p * p <= n; p++) {
  18.             if (prime[p]) {
  19.                 for (int i = p * 2; i <= n; i += p) {
  20.                     prime[i] = false;
  21.                 }
  22.             }
  23.         }
  24.         List<Integer> primeNumbers = new LinkedList<>();
  25.         for (int i = 2; i <= n; i++) {
  26.             if (prime[i]) {
  27.                 primeNumbers.add(i);
  28.             }
  29.         }
  30.         return primeNumbers;
  31.     }
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