# # Use Eratosthenes' Sieve to find the first 10K primes # by ACHERMOV my $start = (times)[0]; sub find_prime_lim { local $quantity = shift; local @bounds; $bounds[0] = 2.0; # lower bound $bounds[1] = 4.0; # upper bound while ($bounds[0] != $bounds[1]) { if ( int ($bounds[1] / log($bounds[1])) < $quantity ) { $bounds[0] = $bounds[1]; $bounds[1] *= 1.1; } if ( int ($bounds[1] / log($bounds[1])) > $quantity ) { $bounds[1] *= 0.9; } if ( int ($bounds[1] / log($bounds[1])) == $quantity ) { $bounds[0] = $bounds[1]; } } return int $bounds[0]; } glob @sieve; push @sieve, $_ for (2..find_prime_lim (9999) ); sub sieve { local $divisor = shift; local $i = 0; #print "$_\n" foreach @sieve; foreach (@sieve) { $i++; next if $_ == $divisor; splice @sieve, $i-1, 1 if $_ % $divisor == 0; } } foreach (@sieve) { sieve($_); } print "1\n"; for (0..9998) { print "@sieve[$_]\n"; } my $end = (times)[0]; printf("RUNTIME: %.2f CPU seconds.", $end - $start);