Skip to content

Instantly share code, notes, and snippets.

Created October 3, 2010 10:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/608493 to your computer and use it in GitHub Desktop.
Save anonymous/608493 to your computer and use it in GitHub Desktop.
import std.stdio;
import std.c.time;
immutable n = 100000000;
pure int calcPrimes()
{
int numPrimes=0;
bool []isPrime = new bool[n];
for(int x=0; x<n; x++)
{
isPrime[x]=true;
}
isPrime[0]=false;
isPrime[1]=false;
for(int x=0; x<n; x++)
{
if(isPrime[x])
{
for(int y=2*x; y<n; y+=x)
{
isPrime[y]=false;
}
}
}
for(int x=0; x<n; x++)
{
if(isPrime[x])
{
numPrimes++;
// printf("Prime: %d\n", x);
}
}
delete isPrime;
return numPrimes;
}
void main(string[] args)
{
writeln("My first D program!");
writeln("This program calculates primes!");
float a = clock();
immutable int primes = calcPrimes();
writeln("Primes found:\t", primes);
writeln("TimeElapsed:\t", ((clock()-a)/CLOCKS_PER_SEC));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment