Advertisement
thornik

Bench 'Sieve' for D

Dec 5th, 2017
2,662
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 0.60 KB | None | 0 0
  1. // ldc2.exe -release -m64 -O sieve.d
  2.  
  3. import std.stdio;
  4. import std.conv;
  5.  
  6. void main(string[] args)
  7. {
  8.     int n = args.length < 2 ? 1 : to!int(args[1]);
  9.     char[8192 + 1] flags;
  10.     int count = 0;
  11.  
  12.     while (n--) {
  13.         count = 0;
  14.         for (int i = 2; i <= 8192; i++)
  15.             flags[i] = 1;
  16.  
  17.         for (int i = 2; i <= 8192; i++) {
  18.             if (flags[i]) {
  19.                 // remove all multiples of prime: i
  20.                 for (int k = i+i; k <= 8192; k += i)
  21.                 flags[k] = 0;
  22.                 count++;
  23.             }
  24.         }
  25.     }
  26.     writeln(count);
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement