Java examples for java.lang:Math Number
Gets all prime numbers below max using the sieve of sundaram method
//package com.java2s; import java.util.*; public class Main { /**//from ww w . j a v a 2s .c o m * Gets all prime numbers below <em>max</em> using the sieve of sundaram method * * @param max Upper limit * @return An array of prime numbers up until <em>max</em> */ public static int[] sieveOfSundaram(int max) { int n = max / 2; boolean[] isPrime = new boolean[max]; Arrays.fill(isPrime, true); for (int i = 1; i < n; i++) { for (int j = i; j <= (n - i) / (2 * i + 1); j++) { isPrime[i + j + 2 * i * j] = false; } } int[] primes = new int[max]; int found = 0; if (max > 2) { primes[found++] = 2; } for (int i = 1; i < n; i++) { if (isPrime[i]) { primes[found++] = i * 2 + 1; } } return Arrays.copyOf(primes, found); } }