Java 107 Bytes, 26 minutes, naive approach
y->{int i=1,j,n,r=0;for(j=2,n=1000000;(r+=++i>=j?1:0)!=n;j+=j%i==0?i=1:0)System.out.print(i>=j?j+"\n":"");}
ungolfed
y->{
int i=1,j,n,r=0;
for(j=2,n=1000000;
(r+=((++i>=j)?1:0))!=n;
j+=((j%i==0)?i=1:0)) {
System.out.print(i>=j?j+"\n":"");
}
}
Worstcase Runtime is O(n)
divisons for primes as it tests everything in [2,i[
and looks if anything divides i
and prints it if it's divisorless or continues if a divisor is found. n*O(n)
would make it O(n^2)
, but due to distribution of divisors and primes, it is something along O(n^2/log(n))+O(n*log(n))
divisons. In practice this takes something along 26 minutes apparently.
Java ungolfed 1601 Bytes, adaptive wheel sieve, 1.6 seconds
public class Sieve {
ArrayList<Integer> primes = new ArrayList<>();
ArrayList<Integer> candidates = new ArrayList<>();
int target = Integer.MAX_VALUE;
int product = 1;
int nextEvolve = 0;
int multiplier = 1;
int iteration = 0;
boolean evolve = true;
Sieve(int n) {
this.candidates.add(1);
this.target = n;
}
int next() {
final int toTest = this.product*this.multiplier+this.candidates.get(this.iteration);
//System.out.println("try: "+toTest+" p:"+this.product+" m:"+this.multiplier+" i:"+this.iteration);
for(int i = this.nextEvolve; i < this.primes.size() && toTest/this.primes.get(i)>=this.primes.get(i); ++i) {
if(toTest%this.primes.get(i)==0) {
++this.iteration;
if((this.iteration%=this.candidates.size())==0) {
++this.multiplier;
}
return this.next();
}
}
this.primes.add(toTest);
++this.iteration;
if((this.iteration%=this.candidates.size())==0) {
++this.multiplier;
if(this.evolve && this.multiplier%this.primes.get(this.nextEvolve)==0) {
if(this.target/this.product<toTest) {
this.evolve = false;
}else {
final int size = this.candidates.size();
for(int i = 1; i < this.primes.get(this.nextEvolve); i++) {
for(int j = 0; j < size; j++) {
if((i*this.product+this.candidates.get(j))%this.primes.get(this.nextEvolve)!=0) {
this.candidates.add(i*this.product+this.candidates.get(j));
}
}
}
this.product*=this.primes.get(this.nextEvolve);
this.multiplier=this.multiplier/this.primes.get(this.nextEvolve);
++this.nextEvolve;
}
}
}
return toTest;
}
public static void main(String[] args) {
try {
System.in.read();
} catch (final IOException e) {
e.printStackTrace();
}
final Sieve s = new Sieve(1_000_000);
for(int prime = s.next(); prime < 1_000_000; prime = s.next()) {
System.out.println(prime);
}
}
}
Java golfed 883 Bytes, 16 seconds
class S{ArrayList<Integer> p=new ArrayList<>(),c=new ArrayList<>();int t,q,n,m,i;boolean e=true;S(int n){this.c.add(1);this.t=n;q=m=1;n=i=0;}int next(){int toTest=this.q*this.m+this.c.get(this.i);for(int i=this.n;i<this.p.size();++i)if(toTest%this.p.get(i)==0){++this.i;if((this.i%=this.c.size())==0)++this.m;return this.next();}this.p.add(toTest);++this.i;if((this.i%=this.c.size())==0){++this.m;if(this.e && this.m%this.p.get(this.n)==0){if(this.t/this.q<toTest) this.e=false;else{int size=this.c.size();for(int i=1;i<this.p.get(this.n);i++)for(int j=0;j<size;j++)if((i*this.q+this.c.get(j))%this.p.get(this.n)!=0)this.c.add(i*this.q+this.c.get(j));this.q*=this.p.get(this.n);this.m=this.m/this.p.get(this.n);++this.n;}}}return toTest;}public static void main(String[] args){S s=new S(1_000_000);for(int prime=s.next();prime<1_000_000;prime=s.next()){System.out.println(prime);}}}
10^6
is even shorter ;) \$\endgroup\$1e6
:-D \$\endgroup\$