# The prime spiral (colored)

Hello, I just watched this video https://www.youtube.com/watch?v=iFuR97YcSLM on Numberphile and I tried making it on Processing with some colours

If you want to see the output, run it

``````void  setup()
{
size(640, 640);

background(51);
}

int red = 255;
int green = 0;
int blue = 255;

boolean isPrime(int n)
{
if (n%2==0)
return false;
for(int i=3;i*i<=n;i+=2)
{
if(n%i==0)
return false;
}
{
if (green < 254)
{
green++;
red--;
}
else
{
}
}
{
if (blue < 254)
{
blue++;
green--;
}
else
{
}
}
{
if (red < 254)
{
red++;
blue--;
}
else
{
}
}
stroke(red, green, blue);
return true;
}

int  change = 1;
int  count = 0;
int dir = 0;
int  x = 320;
int  y = 320;
int nb = 1;
int mod = 1;

void  draw()
{
if (isPrime(nb))
point(x, y);
count++;
if (dir == 0)
y++;
if (dir == 1)
x--;
if (dir == 2)
y--;
if (dir == 3)
x++;
if (count == change)
{
if (dir < 3)
dir++;
else
dir = 0;
count = 0;
mod++;
if (mod % 2 == 0)
change++;
}
nb++;
}
``````

And there is a faster version

``````void  setup()
{
size(640, 640);

background(51);
}

int red = 255;
int green = 0;
int blue = 255;

boolean isPrime(int n)
{
if (n%2==0)
return false;
for(int i=3;i*i<=n;i+=2)
{
if(n%i==0)
return false;
}
{
if (green < 254)
{
green++;
red--;
}
else
{
}
}
{
if (blue < 254)
{
blue++;
green--;
}
else
{
}
}
{
if (red < 254)
{
red++;
blue--;
}
else
{
}
}
stroke(red, green, blue);
return true;
}

int  change = 1;
int  count = 0;
int dir = 0;
int  x = 320;
int  y = 320;
int nb = 1;
int mod = 1;

void  draw()
{
while (!isPrime(nb))
{
count++;
if (dir == 0)
y++;
if (dir == 1)
x--;
if (dir == 2)
y--;
if (dir == 3)
x++;
if (count == change)
{
if (dir < 3)
dir++;
else
dir = 0;
count = 0;
mod++;
if (mod % 2 == 0)
change++;
}
nb++;
}
point(x, y);
nb++;
}
``````

Thank you for sharing!

Can you share something about what changes in the code lead one version to be slow and the other to be faster?

Yes

The fast one doesnâ€™t work (I realized it after)

But itâ€™s faster