Computer programming has its roots firmly grounded in mathematics, with the earliest computers used to calculate results that would take average human days to do by hand. One compelling problem in the field of computer science is discovering **prime numbers**.

This post will show the most straightforward approach to calculating prime numbers using C# 9.

## What Is A Prime Number?

As a refresher for folks, a prime number is a **natural number** (a non-negative number) greater than one that is not composed of two smaller natural numbers. In school, we may have encountered our first prime numbers of 2, 3, 5, 7, and 11. Schools commonly teach **trial division** to determine the primality of any given number, in which we divide a number starting from 2 up to the square root of the number in question.

For example, if we were to determine if **100** was prime, we would divide the number from 2 up to 10. We start with lower factors as commonly, smaller numbers are composites of larger numbers. From a programming perspective, this also ensures we don’t need to perform all the calculations to determine that a number is not prime. In a sense, prime numbers will be the **most expensive** calculation we need to perform.

While the trial division is the most straightforward approach to calculating prime numbers, computer scientists and mathematicians have developed algorithms to discover larger prime numbers. These other approaches value time speed in favor of accuracy. On December 2018, mathematicians found the largest known prime number with 24,862,048 decimal digits using a different approach than trial division. Like all numbers, the possibility for a prime number is infinite.

To read more about Prime Numbers, check out the Wikipedia page.

## Calculate A Number’s Primality

Knowing the formula for trial division, we can write a function that takes a number and attempts to divide our value from **2** to **Sqrt(value)***.

```
bool IsPrime(int number)
{
// local function
bool CalculatePrime(int value)
{
// A methodical approach of checking
// the primality of a given number
// n, called trial division, tests whether n is a multiple
// of any integer between 2 and sqrt(n)
var possibleFactors = Math.Sqrt(number);
// we start with low factors (2,3,4,5,etc...)
// this makes sure we short circuit as early
// as possible during calculations
for (var factor = 2; factor <= possibleFactors; factor++)
{
if (value % factor == 0)
{
return false;
}
}
// we've exhausted all factors
// so we know this number is prime
return true;
}
// negative numbers can't be prime
// only call CalculatePrime if non-negative
return number > 1 && CalculatePrime(number);
}
```

Using programming constructs like circuit breaking, we can avoid costly calculations by returning when we find a factor that returns a remainder of **0**. The resulting remainder means we’ve found a number that *neatly* divides into our value. In our case, we use the **boolean** behavior of evaluation to only call `CalculatePrime`

when our initial value is greater than one and is a natural number.

Let’s look at the complete solution.

```
using System;
using System.Linq;
using static System.Console;
int start = 1, end = 1000;
WriteLine($"The prime numbers between {start} and {end} are :");
var numbers =
Enumerable.Range(start, end - start)
.Where(IsPrime)
.Select(number => number)
.ToList();
WriteLine(string.Join(", ", numbers));
bool IsPrime(int number)
{
// local function
bool CalculatePrime(int value)
{
// A simple but slow method of checking
// the primality of a given number
// n, called trial division, tests whether n is a multiple
// of any integer between 2 and sqrt(n)
var possibleFactors = Math.Sqrt(number);
// we start with low factors (2,3,4,5,etc...)
// this makes sure we short circuit as early
// as possible during calculations
for (var factor = 2; factor <= possibleFactors; factor++)
{
if (value % factor == 0)
{
return false;
}
}
// we've exhausted all factors
// so we know this number is prime
return true;
}
// negative numbers can't be prime
// only call CalculatePrime if non-negative
return number > 1 && CalculatePrime(number);
}
```

Running our top-level statement file, we get the following console output.

```
The prime numbers between 1 and 1000 are :
2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59
61 67 71 73 79 83 89 97 101 103 107 109 113 127
131 137 139 149 151 157 163 167 173 179 181 191
193 197 199 211 223 227 229 233 239 241 251 257
263 269 271 277 281 283 293 307 311 313 317 331
337 347 349 353 359 367 373 379 383 389 397 401
409 419 421 431 433 439 443 449 457 461 463 467
479 487 491 499 503 509 521 523 541 547 557 563
569 571 577 587 593 599 601 607 613 617 619 631
641 643 647 653 659 661 673 677 683 691 701 709
719 727 733 739 743 751 757 761 769 773 787 797
809 811 821 823 827 829 839 853 857 859 863 877
881 883 887 907 911 919 929 937 941 947 953 967
971 977 983 991 997
```

## Conclusion

Calculating prime numbers is an excellent starting problem for folks looking to get into software development. There are several ways to solve for prime numbers, but trial division is easy to understand and without calculation flaws. That said, we should not use the approach to find undiscovered prime numbers, as it’s inefficient at doing so. Leave those discoveries to the professionals. I hope you enjoyed this post, and please leave a comment below.