Tag Archives: primes

Find Mersenne primes in C#

Mersenne primes are prime numbers of the from 2n – 1 for some integer n. For example, 22 – 1 = 4 – 1 = 3 and 3 is prime, so 3 is a Mersenne prime. This example uses a … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , | Leave a comment

Use PLINQ to select prime numbers from an array in C#

The example Use PLINQ to select even numbers from an array in C# uses PLINQ (Parallel LINQ) to find the even numbers in an array of random values. This example uses a similar technique to do something slightly more interesting: … Continue reading

Posted in LINQ, performance | Tagged , , , , , , , , , , , , , | Leave a comment

Find random prime numbers in C#

The example Probabilistically determine whether a number is prime in C# explains an algorithm for determining whether a number is prime with any desired level of certainty. After you add that method to your algorithmic toolkit, finding large prime numbers … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , , , , | Leave a comment

Determine whether a number is prime in C#

This is a cool little algorithm that uses some clever mathematics. This algorithm and several related algorithms are described in my book Essential Algorithms: A Practical Approach to Computer Algorithms. I think it’s a really good book (and it’s gotten … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , , , , , , | 2 Comments

Use yield to generate prime numbers in C#

  This example is based on a lesson about interfaces that I recently recorded for O’Reilly Video Training.   This example shows how to use the yield return statement to generate prime numbers. More importantly it explains how yield return … Continue reading

Posted in algorithms, mathematics, syntax | Tagged , , , , , , , , , , , , , , | Leave a comment

Draw a colored fractal curve generated by prime numbers in C#

This example shows how to draw a colored fractal curve generated by prime numbers. See the example Draw a fractal curve generated by prime numbers in C# for the basic algorithm. This example assigns colors to line segments based on … Continue reading

Posted in algorithms, fractals, graphics, mathematics | Tagged , , , , , , , , , , , , , | Leave a comment

Draw a fractal curve generated by prime numbers in C#

This example shows how to draw a fractal curve generated by prime numbers. It’s a modification of the earlier example Draw a fractal generated by prime numbers in C#. The basic idea is follow these rules: Start at the point … Continue reading

Posted in algorithms, fractals, graphics, mathematics | Tagged , , , , , , , , , , , | 1 Comment

Use a bitmap to visualize composite runs in C#

I promise this is my last post on primes for a while. (Sometimes one example leads to another, which leads to another, and so on.) This example draws a bitmap to help visualize composite runs and prime numbers. The basic … Continue reading

Posted in algorithms, graphics, image processing, mathematics | Tagged , , , , , , , , , , , , , | Leave a comment

Find runs of composite numbers (non-primes) in C#

I recently had a birthday and my age is a prime number, so like any normal person I was wondering how long a run of consecutive composite numbers comes after my current age. (Doesn’t everyone wonder such things?) This example … Continue reading

Posted in algorithms, mathematics | Tagged , , , , , , , , , , | Leave a comment

Use a bitmap to visualize odd primes in C#

This example modifies the program in Use a bitmap to visualize primes in C# to let you visualize odd primes. It creates an is_prime array like the previous example does, but it makes the array twice as large as before. … Continue reading

Posted in algorithms, graphics, image processing, mathematics | Tagged , , , , , , , , , , , , , | 1 Comment