Skip to content

Instantly share code, notes, and snippets.

@tiffany352
Created March 5, 2012 00:18
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 tiffany352/1975594 to your computer and use it in GitHub Desktop.
Save tiffany352/1975594 to your computer and use it in GitHub Desktop.
[tiffany@localhost scripts]$ fcc -O -release primes.nt
[tiffany@localhost scripts]$ time -p ./primes
real 3.47
user 3.46
sys 0.00
[tiffany@localhost scripts]$ time -p luajit -O3 primes.lua
real 3.78
user 3.77
sys 0.00
[tiffany@localhost scripts]$ gcc -std=c99 -o primes-c primes.c
[tiffany@localhost scripts]$ time -p ./primes-c
9999991real 1.88
user 1.88
sys 0.00
[tiffany@localhost scripts]$ gcc -O3 -std=c99 -o primes-c primes.c
[tiffany@localhost scripts]$ time -p ./primes-c
9999991real 1.82
user 1.82
sys 0.00
[tiffany@localhost scripts]$ gcc -O3 -ffast-math -march=native -std=c99 -o primes-c primes.c
[tiffany@localhost scripts]$ time -p ./primes-c
9999991real 1.83
user 1.82
sys 0.00
#include <stdio.h>
int main(int argc, char** argv) {
int primes[700000]; // 664579 primes under 10 million
int nprimes = 0;
for (int i=3; i < 10000000; i+=2) {
int prime = 1;
for (int j = 0; j < nprimes; j++) {
int p = primes[j];
if (p*p > i) break;
if (i % p == 0) {
prime = 0;
break;
}
}
if (prime) {
primes[nprimes] = i;
nprimes++;
}
}
printf("%d",primes[nprimes-1]);
}
primes = {}
for i = 3, 10000000, 2 do
prime = true;
for n = 1, #primes do
p = primes[n];
if p*p > i then break end;
if i%p == 0 then prime = false break end;
end
if prime then
primes[#primes+1] = i;
end
end
module primes;
void main() {
int[auto~] primes;
for (int i=3; i < 10_000_000; i+=2) {
bool prime = true;
for auto p <- primes {
if p*p > i break;
if i % p == 0 {
prime = false;
break;
}
}
if prime primes ~= i;
}
//writeln "$primes";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment