Tag Archives: prime numbers

Use LINQ with an embedded lambda expression to find prime numbers in C# (Part 3 of 3)

The example Use more efficient LINQ to find prime numbers in C# (Part 2 of 3) uses a LINQ query to find prime numbers. It creates a delegate variable named IsOddPrime referring to a lambda expression and uses it to … Continue reading

Posted in algorithms, LINQ, mathematics | Tagged , , , , , , , , , , | 4 Comments

Use more efficient LINQ to find prime numbers in C# (Part 2 of 3)

The previous post showed how you can use LINQ to find prime numbers. For every number between 1 and a maximum, the LINQ query called the IsPrime method to see if the number should be part of the selection. That … Continue reading

Posted in algorithms, LINQ, mathematics | Tagged , , , , , , , , , , | 1 Comment

Use LINQ to find prime numbers in C# (Part 1 of 3)

This example uses LINQ to find prime numbers between 1 and a maximum value that you enter. It defines the following IsPrime method to determine whether a number is prime. // Return true if the number is prime. private bool … Continue reading

Posted in algorithms, LINQ, mathematics | Tagged , , , , , , , , , | 1 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