Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# prime spiral spin

By: a guest on Apr 29th, 2014  |  syntax: None  |  size: 1.81 KB  |  views: 166  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1. int A[]; // prime number container
2. int count=1;// no of primes allocated
3. int i;// loop variable declared global to preserve memory
4. int x;//co ordinate
5. int y;//co ordinate
6. int c = 1;// starting point
7. int step=1;// the upper limit that describes how far must (x,y) traverse to make the outer edge of the spiral
8. int direction = -1; // simple variable that reverses the direction after every horizontal and vertical spiral edge draw.  (x++,y) X //step, (x,y++) X step. then (x--,y) X step, (x,y--) X step step++ and repeat.
9. int s = 0;// remove for v1.2, keep for v 1.1
10. int imgno=1;//  offset
11. int p; // spiral overflow variable
12.
13. void setup()
14. {
15.   size(600, 600);
16.   orientation(LANDSCAPE);
17.   background(255);
18.   A = new int[4000000];
19.   A[0]=2;
20.   x=width/2;
21.   y=height/2;
22.
23.   /*while ( (s++)<c)
24.    {
25.    prime(s);
26.    //println(s);
27.    }*/
28. }
29.
30. void draw()
31. {
32.   background(255);
33.   p=1;
34.   c=imgno;
35.   //s=0;
36.   x=width/2;
37.   y=height/2;
38.   step = 1;
39.   println("point1");
40.   if (prime(c))
41.     point(x, y);
42.
43.
44.   while (p<=360200)
45.   {
46.     direction*=-1;
47.
48.     for (int d=0;d<step;d++)
49.     {
50.       x=x+direction;
51.       c=c+1;
52.       p++;
53.       if (prime(c))
54.         point(x, y);
55.       println("point = "+x+","+y);
56.     }
57.     for (int d=0;d<step;d++)
58.     {
59.       y=y+direction;
60.       c=c+1;
61.       p++;
62.       if (prime(c))
63.         point(x, y);
64.       println("point = "+x+","+y);
65.     }
66.     println("step= "+step);
67.     step++;
68.   }
69.   imgno++;
70.   //save("prime"+imgno+".jpeg");
71.   delay(20);
72. }
73.
74. boolean prime(int n)
75. {
76.   if (n==1)
77.     return false;
78.   else if (n==2)
79.     return true;
80.   else
81.   {
82.     for (int i=0;((i<count)&&(A[i]<=sqrt(n)));i++)
83.     {
84.       println("A["+i+"]"+"="+A[i]+"    n= "+n);
85.       if (n%A[i]==0)
86.         return false;
87.     }
88.   }
89.   A[count] = n;
90.   count++;
91.   return true;
92. }
clone this paste RAW Paste Data
Top