# VBForums CodeBank > CodeBank - C# >  IsPrime in C#

## Kasracer

This might be useful to someone out there.

This function takes in an integer (32bit but you shouldn't need to worry about that). Then, it uses The Sieve of Eratosthenes method to find out if the number supplied to it is a prime number or not. It's very quick compared to conventional methods.

I was working on this function and found this article to optimize it using the Sieve of Eratosthenes method.

This function may not be _that_ useful to anyone but I thought I would throw it in here.

VB Code:
public static bool IsPrime(Int32 ToBeChecked)
{
    System.Collections.BitArray numbers = new System.Collections.BitArray(ToBeChecked+1, true);
    for (Int32 i = 2; i < ToBeChecked+1; i++)
        if (numbers[[b][/b]i])
        {
            for (Int32 j = i * 2; j < ToBeChecked+1; j += i)
                numbers[j] = false;
            if (numbers[i])
            {
                if (ToBeChecked == i)
                {
                    return true;
                }
            }
        }
    return false;
}

----------


## Hack

Post a little explanation regarding what it does and how it could be used (new folks would probably not have a clue what is going on here.   :Smilie:  )

----------


## Kasracer

> Post a little explanation regarding what it does and how it could be used (new folks would probably not have a clue what is going on here.   )


Hopefully I did a good enough job. I'm not very good at explaining things.

----------


## Hack

> Hopefully I did a good enough job. I'm not very good at explaining things.


It looks just fine to me.   Thanks!   :Thumb:

----------


## wossname

Eratosthenes is primarily used to generate a long list of primes and not just test primality of a single number.  It would be MUCH faster just to try to divide the number by all odd integers <= sqrt(number).  Also discard number if *(number & 1) == 0*.

----------


## PT Exorcist

anyways, kasracer put a link that explained it

----------

