# The prime spiral (colored)

**URL:** <https://discourse.processing.org/t/the-prime-spiral-colored/585>\
**Category:** Gallery\
**Created:** [June 1, 2018, 12:05am UTC](https://discourse.processing.org/t/the-prime-spiral-colored/585 "2018-06-01T00:05:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bux](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bux/32/286_2.png) [@Bux](https://discourse.processing.org/u/Bux)\
**Post date:** [June 1, 2018, 12:05am UTC](https://discourse.processing.org/t/the-prime-spiral-colored/585/1 "2018-06-01T00:05:33Z")

</div>

Hello, I just watched this video [https://www.youtube.com/watch?v=iFuR97YcSLM](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 😃

```auto
void setup()
{
  size(640, 640);
  
  background(51);
}

int red = 255;
int green = 0;
int blue = 255;
int addGreen = 1;
int addBlue = 0;
int addRed = 0;

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 (addGreen == 1)
    {
      if (green < 254)
      {
        green++;
        red--;
      }
      else
      {
        addGreen = 0;
        addBlue = 1;
      }
    }
    else if (addBlue == 1)
    {
      if (blue < 254)
      {
        blue++;
        green--;
      }
      else
      {
        addBlue = 0;
        addRed = 1;
      }
    }
    else if (addRed == 1)
    {
      if (red < 254)
      {
        red++;
        blue--;
      }
      else
      {
        addRed = 0;
        addGreen = 1;
      }
    }
    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

```auto
void setup()
{
  size(640, 640);
  
  background(51);
}

int red = 255;
int green = 0;
int blue = 255;
int addGreen = 1;
int addBlue = 0;
int addRed = 0;

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 (addGreen == 1)
    {
      if (green < 254)
      {
        green++;
        red--;
      }
      else
      {
        addGreen = 0;
        addBlue = 1;
      }
    }
    else if (addBlue == 1)
    {
      if (blue < 254)
      {
        blue++;
        green--;
      }
      else
      {
        addBlue = 0;
        addRed = 1;
      }
    }
    else if (addRed == 1)
    {
      if (red < 254)
      {
        red++;
        blue--;
      }
      else
      {
        addRed = 0;
        addGreen = 1;
      }
    }
    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++;
}

```

---

<div class="post-metadata">

**Author:** ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)\
**Post date:** [June 2, 2018, 4:44pm UTC](https://discourse.processing.org/t/the-prime-spiral-colored/585/2 "2018-06-02T16:44:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Bux](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/bux/32/286_2.png) [@Bux](https://discourse.processing.org/u/Bux)\
**Post date:** [June 2, 2018, 8:04pm UTC](https://discourse.processing.org/t/the-prime-spiral-colored/585/3 "2018-06-02T20:04:01Z")

</div>

Yes

The fast one doesn’t work (I realized it after)

But it’s faster
