System.Collections.HashHelpers.IsPrime C# (CSharp) Method

IsPrime() public static method

public static IsPrime ( int candidate ) : bool
candidate int
return bool
        public static bool IsPrime(int candidate)
        {
            if ((candidate & 1) != 0)
            {
                int limit = (int)Math.Sqrt(candidate);
                for (int divisor = 3; divisor <= limit; divisor += 2)
                {
                    if ((candidate % divisor) == 0)
                        return false;
                }
                return true;
            }
            return (candidate == 2);
        }

Usage Example

示例#1
0
        public static int GetPrime(int min)
        {
            if (min < 0)
            {
                throw new ArgumentException(Environment.GetResourceString("Arg_HTCapacityOverflow"));
            }
            for (int index = 0; index < HashHelpers.primes.Length; ++index)
            {
                int num = HashHelpers.primes[index];
                if (num >= min)
                {
                    return(num);
                }
            }
            int candidate = min | 1;

            while (candidate < int.MaxValue)
            {
                if (HashHelpers.IsPrime(candidate) && (candidate - 1) % 101 != 0)
                {
                    return(candidate);
                }
                candidate += 2;
            }
            return(min);
        }
All Usage Examples Of System.Collections.HashHelpers::IsPrime