Guest User

Untitled

a guest
Jul 18th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.33 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. public class Prime {
  5.     public int current = 0;
  6.  
  7.     public int nextPrime() {
  8.         while (!Prime.isPrime(++this.current)) {
  9.         }
  10.         return this.current;
  11.     }
  12.  
  13.     public static double sumOfPrimesBelowN(int n) {
  14.         Prime p = new Prime();
  15.         double total = 0;
  16.         double current = p.nextPrime();
  17.         while (current < n) {
  18.             total += current;
  19.             current = p.nextPrime();
  20.         }
  21.         return total;
  22.     }
  23.  
  24.     public static Integer[] primesBelow(int i) {
  25.         List<Integer> primes = new ArrayList<Integer>();
  26.         Prime p = new Prime();
  27.         double current = p.nextPrime();
  28.         while (current < i) {
  29.             primes.add((int) current);
  30.             current = p.nextPrime();
  31.         }
  32.         return primes.toArray(new Integer[primes.size()]);
  33.     }
  34.  
  35.     public static double largestPrimeFactor(double n) {
  36.         Prime p = new Prime();
  37.         double largest = 0;
  38.         double current = p.nextPrime();
  39.         while (current <= n) {
  40.             if (n % current == 0) {
  41.                 n /= current;
  42.                 if (current > largest) {
  43.                     largest = current;
  44.                 }
  45.             }
  46.             current = p.nextPrime();
  47.         }
  48.         return largest;
  49.     }
  50.  
  51.     public static double nthPrime(int i) {
  52.         int counter = 1;
  53.         Prime p = new Prime();
  54.         double current = p.nextPrime();
  55.         while (counter < i) {
  56.             current = p.nextPrime();
  57.             counter++;
  58.         }
  59.         return current;
  60.     }
  61.  
  62.     public static double largestPrimeUnder(int i) {
  63.         Prime p = new Prime();
  64.         double largest = 0;
  65.         double current = p.nextPrime();
  66.         while (current < i) {
  67.             if (current > largest) {
  68.                 largest = current;
  69.                 current = p.nextPrime();
  70.             }
  71.         }
  72.         return largest;
  73.     }
  74.  
  75.     public static boolean isPrime (int n) {
  76.         if (n <= 1) {
  77.             return false;
  78.         } else if (n == 2) {
  79.             return true;
  80.         } else if (n % 2 == 0) {
  81.             return false;
  82.         }
  83.         for (int i = 3; i <= Math.sqrt(n); i+=2) {
  84.             if (n % i == 0)
  85.                 return false;
  86.         }
  87.         return true;
  88.     }
  89. }
Add Comment
Please, Sign In to add comment