代码拉取完成,页面将自动刷新
/**
* Use the sieve of eratosthenes to find all the prime numbers up to a certain limit.
*
* <p>Time Complexity: O(nloglogn)
*
* @author William Fiset, william.alexandre.fiset@gmail.com
*/
package com.williamfiset.algorithms.math;
public class SieveOfEratosthenes {
// Gets all primes up to, but NOT including limit (returned as a list of primes)
public static int[] sieve(int limit) {
if (limit <= 2) return new int[0];
// Find an upper bound on the number of prime numbers up to our limit.
// https://en.wikipedia.org/wiki/Prime-counting_function#Inequalities
final int numPrimes = (int) (1.25506 * limit / Math.log((double) limit));
int[] primes = new int[numPrimes];
int index = 0;
boolean[] isComposite = new boolean[limit];
final int sqrtLimit = (int) Math.sqrt(limit);
for (int i = 2; i <= sqrtLimit; i++) {
if (!isComposite[i]) {
primes[index++] = i;
for (int j = i * i; j < limit; j += i) isComposite[j] = true;
}
}
for (int i = sqrtLimit + 1; i < limit; i++) if (!isComposite[i]) primes[index++] = i;
return java.util.Arrays.copyOf(primes, index);
}
public static void main(String[] args) {
// Generate all the primes up to 29 not inclusive
int[] primes = sieve(29);
// Prints [2, 3, 5, 7, 11, 13, 17, 19, 23]
System.out.println(java.util.Arrays.toString(primes));
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。