Calculate prime numbers This is a program to calculate the largest prime number.  Are there any possible improvements in style/speed/accuracy?

public static int? LargestPrime(int max)
{
    if (max &lt; 0)
        return null;
    int largestPrime = 0;
    for (int i = 0; i &lt;= max; i++)
    {              
        bool? isPrime = IsPrime(i);
        if ((bool)isPrime)
            largestPrime = i;
        if (i % 100000 == 0)
            Console.WriteLine(largestPrime.ToString("N0"));
    }
    return largestPrime;
}
private static List&lt;int&gt; listPrimes = new List&lt;int&gt;() { 2, 3 };
private static int listPrimeMax;
private static int listPrimesCount = 100000;
private static bool firstA = true;
private static bool firstB = true;
public static bool? IsPrime(int n)
{    
    if (listPrimes.Count &lt; listPrimesCount)
    {
        // from wiki
        // function is_prime(n : integer)
        // if n ≤ 1
        //     return false
        // else if n ≤ 3
        //     return true
        // else if n mod 2 = 0 or n mod 3 = 0
        //     return false
        // let i ← 5
        // while i×i ≤ n
        //     if n mod i = 0 or n mod(i + 2) = 0
        //         return false
        //     i ← i + 6
        //return true
        if (n &lt; 0)
            return null;
        if (n &lt;= 1)
            return false;
        if (n &lt;= 3)
            return true;
        if (n % 2 == 0 || n % 3 == 0)
            return false;
        for (int i = 5; i*i &lt;= n; i += 6)
        {
            if (n % i == 0 || n % (i + 2) == 0)
                return false;
        }
        if (listPrimes.Count &lt; listPrimesCount)
        {
            listPrimes.Add(n);
            listPrimeMax = n;
        }
        return true;
    }
    else
    {
        if(firstA)
        {
            Console.WriteLine("in listPrimes");
            firstA = false;
        }
        foreach (int i in listPrimes)
        {
            if (n % i == 0)
                return false;
            if (i * i &gt; n)
                break;
        }
        for (int i = listPrimeMax + 2; i*i &lt;= n; i += 2)
        {
            if (n % i == 0)
                return false;
        }
        if (listPrimes.Count &lt; 100 * listPrimesCount)
        {
            listPrimes.Add(n);
            listPrimeMax = n;
        }
        else if(firstB)
        {
            Console.WriteLine("in 100*listPrimes");
            firstB = false;
        }
        return true;
    }           
}
public static int? LargestPrime(int max)
{
    if (max &lt; 0)
        return null;
    int largestPrime = 0;
    for (int i = 0; i &lt;= max; i++)
    {              
        bool? isPrime = IsPrime(i);
        if ((bool)isPrime)
            largestPrime = i;
        if (i % 100000 == 0)
            Console.WriteLine(largestPrime.ToString(&quot;N0&quot;));
    }
    return largestPrime;
}  



Omitting braces {}, although they might be optional, won't do you good in the long run because it makes your code error-prone.

If an argument of a method isn't correct like max &lt; 0 you should throw either an ArgumentException or better an ArgumentOutOfRangeException.

You are iterating from 0 to max to check if the value is a prime, but I wonder what else could the number be? Either it is a prime or it isn't a prime, there is no third way so it doesn't make sense that IsPrime() returns a nullable bool.



bool? IsPrime(int n)
Here you are iterating from 5 to i*i&lt;n for each number which is passed to the method.
So let us assume that we pass max = 10.000.000 (the dots are there for clearity) this

    if (n % i == 0 || n % (i + 2) == 0)
        return false;


in the for loop in the IsPrime() method is executed 16.194.513 times.
By using a sieve you could pretty much speed this thing up like so
static int CalculateLargestPrime(int maxPrime)
{
    bool[] isComposite = new bool[maxPrime + 1];
    for (int x = 2; x * x &lt;= maxPrime; x++)
    {
        if (!isComposite[x])
        {
            for (int y = x * x; y &lt;= maxPrime; y = y + x)
            {
                isComposite[y] = true;
            }
        }
    }
    for (int i = maxPrime; i &gt;= 0; i--)
    {
        if (!isComposite[i])
        {
            return i;
        }
    }

    return -1;
}  

taken and adjusted from https://codereview.stackexchange.com/a/62158/29371