/// <summary> ///Prime number Help class///this class extracts the corresponding code from the. NET source class internal The static class Hashhelpers class/// https://referencesource.microsoft.com/#mscorlib/system/collections/hashtable.cs,e8668bf19da49963///Hashtable.cs./// </summary> Public classPrimehelper {/// <summary> ///given some primes (except 2)/// </summary> Public Static ReadOnly int[] Primes = { 3,7, One, -, at, in,Panax Notoginseng, -, -, in, the,107,131,163,197,239,293,353,431,521,631,761,919, 1103,1327,1597,1931,2333,2801,3371,4049,4861,5839,7013,8419,10103,12143,14591, 17519,21023,25229,30293,36353,43627,52361,62851,75431,90523,108631,130363,156437, 187751,225307,270371,324449,389357,467237,560689,672827,807403,968897,1162687,1395263, 1674319,2009191,2411033,2893249,3471899,4166287,4999559,5999471,7199369}; /// <summary> ///determine if it is a prime number/// </summary> /// <param name= "candidate" ></param> /// <returns></returns> Public Static BOOLIsPrime (intcandidate) { //The even number is not prime number, if((Candidate &1) !=0) { intLimit = (int) math.sqrt (candidate); for(intdivisor =3; Divisor <= limit; Divisor + =2) { if((candidate% divisor) = =0) return false; } return true; } //2 is prime return(Candidate = =2); } /// <summary> ///minimum prime number greater than a given/// </summary> /// <param name= "min" ></param> /// <returns></returns> Public Static intGetprime (intmin) { if(Min <0) { Throw NewArgumentException ("the given number must be a natural number"); } //first, look for the given array. foreach(intPrimeinchPrimes) { if(Prime >=min) { returnPrime; } } //there is no corresponding prime in the given array, the loop is searched//If Min is an even number, the starting value is plus 1, otherwise the starting value is itself for(inti = (min |1); i < Int32.MaxValue; i + =2) { if(IsPrime (i))returni; } returnmin; } /// <summary> ///gets the minimum prime number (except 2)/// </summary> /// <returns></returns> Public Static intGetminprime () {returnprimes[0]; } }
C # Tool class prime Extension class