Int64 - IsPrime

An Int64 extension method that query if '@this' is prime.

Try it

public static void Main()
{		
	Console.WriteLine("Prime numbers between 1 and 100 are:\n");

    for (Int64 val = 1; val <= 100; val++)
    {
		// C# Extension Method: Int64 - IsPrime
		if(val.IsPrime())
		{
        	Console.WriteLine(val);
		}
    }
}

View Source
using System;

public static partial class Extensions
{
    /// <summary>
    ///     An Int64 extension method that query if '@this' is prime.
    /// </summary>
    /// <param name="this">The @this to act on.</param>
    /// <returns>true if prime, false if not.</returns>
    public static bool IsPrime(this Int64 @this)
    {
        if (@this == 1 || @this == 2)
        {
            return true;
        }

        if (@this%2 == 0)
        {
            return false;
        }

        var sqrt = (Int64) Math.Sqrt(@this);
        for (Int64 t = 3; t <= sqrt; t = t + 2)
        {
            if (@this%t == 0)
            {
                return false;
            }
        }

        return true;
    }
}