# Use a bitmap to visualize primes in C# This example makes a bitmap that helps you visualize primes. The example uses the techniques described in the post Use Euler’s Sieve to find prime numbers in C# to build a table of prime numbers. It then makes a bitmap coloring non-prime pixels black and prime pixels red.

The following code shows how the program creates the bitmap.

```// Make a bitmap showing the primes.
private Bitmap MakeBitmap(bool[] is_prime, int wid, int hgt)
{
// Make the bitmap.
Bitmap bm = new Bitmap(wid, hgt);

// Set the pixels.
int index = 0;
for (int y = 0; y < hgt; y++)
{
for (int x = 0; x < wid; x++)
{
if ((index % 2 == 1) && (is_prime[index]))
bm.SetPixel(x, y, Color.Red);
else
bm.SetPixel(x, y, Color.Black);
index++;
}
}

// Fix 0, 1, and 2. (Assumes num_cols >= 3.)
bm.SetPixel(0, 0, Color.Blue);
bm.SetPixel(1, 0, Color.Blue);
bm.SetPixel(2, 0, Color.Red);

return bm;
}```

This method creates a bitmap with the required numbers of rows and columns. It then loops through the is_prime array and sets the pixels’ colors appropriately. After processing the array, the code sets the pixels for 0 and 1 to blue, because they’re a bit weird.

If you look at the result, you’ll see columns of black running through the data. Multiples of 2 give 1-pixel wide columns. Every other multiple of 5 (5, 15, 25, etc.) gives 3-pixel wide columns (1 pixel for the multiple of 5 and 1 for the adjacent even numbers).

Other combinations produce other black vertical stripes depending on how they divide into the bitmap’s width, in this example, 300. For example, 3 and 5 both divide evenly into 300. That means 300 × N + 3 and 300 × N + 5 are both non-prime. Add in their adjacent even numbers, and all of the values between 300 × N + 2 and 300 × N + 6 are non-prime. That produces a vertical band of black that’s at least 5 pixels wide.

You can experiment with the number of columns to see how the stripes of black line up.

There are two other interesting things to note about the picture. First, as you move to later rows, you can find longer and longer runs of consecutive non-primes.

Second, no matter how far down the image you go, you can still find lots of primes. (The fact that there are lots of primes even for large numbers is important for cryptographic techniques that use large prime numbers. Basically it means you can find them by guessing.)    Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, graphics, image processing, mathematics and tagged , , , , , , , , , , , , . Bookmark the permalink.

### 7 Responses to Use a bitmap to visualize primes in C#

1. Müslüm says:
• RodStephens says:

Sorry but I don’t understand your problem.

2. Müslüm İncedal says:

1.serial port data split (‘ ‘) pixel value
2.pixel value to picturebox setpixel

• RodStephens says:

Sorry but I don’t know how to do that. It will depend on the format used by the camera. For example, many cameras compress their data into png or some other format.

I would try to find a way to just save the camera data as a png or some other format and then load it.

This site uses Akismet to reduce spam. Learn how your comment data is processed.