Skip to content

Instantly share code, notes, and snippets.

@LuxXx
Created April 14, 2017 02:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LuxXx/1c770bc0d6582cd5507124f08521ebd5 to your computer and use it in GitHub Desktop.
Save LuxXx/1c770bc0d6582cd5507124f08521ebd5 to your computer and use it in GitHub Desktop.
Project Euler - Problem 3
package euler;
public class Prime {
public static void main(String[] args) {
int n = 100000;
// initially assume all integers are prime
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) {
isPrime[i] = true;
}
for (int factor = 2; factor*factor <= n; factor++) {
// if factor is prime, then mark multiples of factor as non-prime
// suffices to consider multiples factor, factor+1, ..., n/factor
if (isPrime[factor]) {
for (int j = factor; factor*j <= n; j++) {
isPrime[factor*j] = false;
}
}
}
long x = 600851475143L;
for (int i = 0; i < isPrime.length; i++) {
if (!isPrime[i]) continue;
if (x % i == 0) {
x /= i;
System.out.println(i);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment