root/trunk/qd/primes.d

Revision 318, 1.5 kB (checked in by FeepingCreature, 4 years ago)
  • Disabled scaling
Line 
1 module primes;
2
3 import tools.base, qd, std.math;
4 bool isPrimeNaïve(int i, int start = 2) {
5   auto sqi = sqrt(cast(float) i);
6   for (int k = start; k <= sqi; ++k) {
7     if ((i%k) == 0) return false;
8   }
9   return true;
10 }
11
12 bool isPrime(int i) {
13   static int[] list;
14   if (!list.length) {
15     int cur = 2;
16     while (list.length < 200) {
17       if (isPrimeNaïve(cur)) list ~= cur;
18       cur++;
19     }
20   }
21   auto sqi = sqrt(cast(float) i);
22   foreach (smallprime; list) {
23     if (smallprime>sqi) return true; // bound for divisors
24     if ((i%smallprime) == 0) return false;
25   }
26   return isPrimeNaïve(i, list[$-1]+1);
27 }
28
29
30 import tools.array2d;
31 void main() {
32   screen(640, 480);
33   cls(White);
34   const scale = 1;
35   int limit = 480*480*scale*scale;
36   int current;
37   auto field = array2d!(ubyte)(480, 480);
38   Stuple!(int, int) pos = stuple((field.width/2)*scale, (field.height/2)*scale);
39   void up () { pos._0 --; ; } void down () { pos._0 ++; }
40   void left () { pos._1 --; } void right () { pos._1 ++; }
41   void check () { current++; if (isPrime(current)) field[pos.x/scale, pos.y/scale] = field[pos.x/scale, pos.y/scale] + 1; }
42   int step;
43   flip=false;
44   while (current<limit) {
45     Range[++step].each={ right; check; };
46     Range[step].each={ up; check; };
47     Range[++step].each={ left; check; };
48     Range[step].each={ down; check; };
49   }
50   for (int x = 80; x < 560; ++x) for (int y = 0; y < 480; ++y) {
51     float value = field[x-80, y] / (scale*scale*1.0);
52     pset(x, y, White.blend(Black, value));
53   }
54   while (true) { events; flip; }
55 }
Note: See TracBrowser for help on using the browser.