| | | 1 | | using System.Numerics; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.Math; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Static convenience wrapper that shares a single thread-safe generator. |
| | | 7 | | /// </summary> |
| | | 8 | | public static class PrimeUtils |
| | | 9 | | { |
| | | 10 | | /// <summary> |
| | | 11 | | /// Determines whether the specified 8-bit signed integer is prime. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <param name="value">The value to test for primality.</param> |
| | | 14 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 15 | | public static bool IsPrimeNumber(sbyte value) |
| | 132 | 16 | | { |
| | 132 | 17 | | return IsPrimeNumber((int)value); |
| | 132 | 18 | | } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Determines whether the specified 8-bit unsigned integer is prime. |
| | | 22 | | /// </summary> |
| | | 23 | | /// <param name="value">The value to test for primality.</param> |
| | | 24 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 25 | | public static bool IsPrimeNumber(byte value) |
| | 32 | 26 | | { |
| | 32 | 27 | | return IsPrimeNumber((int)value); |
| | 32 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Determines whether the specified 16-bit signed integer is prime. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="value">The value to test for primality.</param> |
| | | 34 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 35 | | public static bool IsPrimeNumber(short value) |
| | 5 | 36 | | { |
| | 5 | 37 | | return IsPrimeNumber((int)value); |
| | 5 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Determines whether the specified 16-bit unsigned integer is prime. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="value">The value to test for primality.</param> |
| | | 44 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 45 | | public static bool IsPrimeNumber(ushort value) |
| | 4 | 46 | | { |
| | 4 | 47 | | return IsPrimeNumber((int)value); |
| | 4 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Determines whether the specified 32-bit unsigned integer is prime. |
| | | 52 | | /// </summary> |
| | | 53 | | /// <param name="value">The value to test for primality.</param> |
| | | 54 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 55 | | public static bool IsPrimeNumber(uint value) |
| | 5 | 56 | | { |
| | 5 | 57 | | return value <= int.MaxValue ? IsPrimeNumber((int)value) : IsPrimeNumber((ulong)value); |
| | 5 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Optimized primality check for 32-bit integers using trial division. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <param name="value">The value to test for primality.</param> |
| | | 64 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 65 | | public static bool IsPrimeNumber(int value) |
| | 230 | 66 | | { |
| | 230 | 67 | | if (value < 2) |
| | 12 | 68 | | { |
| | 12 | 69 | | return false; |
| | | 70 | | } |
| | | 71 | | |
| | 218 | 72 | | if (value == 2) |
| | 9 | 73 | | { |
| | 9 | 74 | | return true; |
| | | 75 | | } |
| | | 76 | | |
| | 209 | 77 | | if (value % 2 == 0) |
| | 95 | 78 | | { |
| | 95 | 79 | | return false; |
| | | 80 | | } |
| | | 81 | | |
| | 114 | 82 | | var limit = (int)System.Math.Sqrt(value); |
| | | 83 | | |
| | 47576 | 84 | | for (var divisor = 3; divisor <= limit; divisor += 2) |
| | 23720 | 85 | | { |
| | 23720 | 86 | | if (value % divisor == 0) |
| | 46 | 87 | | { |
| | 46 | 88 | | return false; |
| | | 89 | | } |
| | 23674 | 90 | | } |
| | | 91 | | |
| | 68 | 92 | | return true; |
| | 230 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <summary> |
| | | 96 | | /// Optimized primality check for 64-bit integers using deterministic Miller-Rabin for 64-bit range. |
| | | 97 | | /// </summary> |
| | | 98 | | /// <param name="value">The value to test for primality.</param> |
| | | 99 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 100 | | public static bool IsPrimeNumber(long value) |
| | 36 | 101 | | { |
| | 36 | 102 | | var candidate = (ulong)value; |
| | 36 | 103 | | var smallPrimeResult = CheckSmallPrimesAndDivisors(candidate); |
| | | 104 | | |
| | 36 | 105 | | if (smallPrimeResult.HasValue) |
| | 24 | 106 | | { |
| | 24 | 107 | | return smallPrimeResult.Value; |
| | | 108 | | } |
| | | 109 | | |
| | 12 | 110 | | ulong[] witnesses = [2UL, 325UL, 9375UL, 28178UL, 450775UL, 9780504UL, 1795265022UL]; |
| | | 111 | | |
| | 12 | 112 | | var d = candidate - 1UL; |
| | 12 | 113 | | var s = 0; |
| | | 114 | | |
| | 29 | 115 | | while ((d & 1UL) == 0UL) |
| | 17 | 116 | | { |
| | 17 | 117 | | d >>= 1; |
| | 17 | 118 | | s++; |
| | 17 | 119 | | } |
| | | 120 | | |
| | 12 | 121 | | var candidateBigInteger = new BigInteger(candidate); |
| | | 122 | | |
| | 175 | 123 | | foreach (var witnessValue in witnesses) |
| | 71 | 124 | | { |
| | 71 | 125 | | if (witnessValue % candidate == 0UL) |
| | 2 | 126 | | { |
| | 2 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | |
| | 69 | 130 | | var witnessBigInteger = new BigInteger(witnessValue % candidate); |
| | 69 | 131 | | var exponentBigInteger = new BigInteger(d); |
| | 69 | 132 | | var x = BigInteger.ModPow(witnessBigInteger, exponentBigInteger, candidateBigInteger); |
| | | 133 | | |
| | 69 | 134 | | if (x == BigInteger.One || x == candidateBigInteger - BigInteger.One) |
| | 56 | 135 | | { |
| | 56 | 136 | | continue; |
| | | 137 | | } |
| | | 138 | | |
| | 13 | 139 | | var roundPassed = false; |
| | 42 | 140 | | for (var iteration = 1; iteration < s; iteration++) |
| | 20 | 141 | | { |
| | 20 | 142 | | x = x * x % candidateBigInteger; |
| | | 143 | | |
| | 20 | 144 | | if (x == candidateBigInteger - BigInteger.One) |
| | 12 | 145 | | { |
| | 12 | 146 | | roundPassed = true; |
| | | 147 | | |
| | 12 | 148 | | break; |
| | | 149 | | } |
| | 8 | 150 | | } |
| | | 151 | | |
| | 13 | 152 | | if (!roundPassed) |
| | 1 | 153 | | { |
| | 1 | 154 | | return false; |
| | | 155 | | } |
| | 12 | 156 | | } |
| | | 157 | | |
| | 9 | 158 | | return true; |
| | 36 | 159 | | } |
| | | 160 | | |
| | | 161 | | /// <summary> |
| | | 162 | | /// Determines whether the specified 64-bit unsigned integer is prime. |
| | | 163 | | /// </summary> |
| | | 164 | | /// <param name="value">The value to test for primality.</param> |
| | | 165 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 166 | | public static bool IsPrimeNumber(ulong value) |
| | 9 | 167 | | { |
| | 9 | 168 | | var smallPrimeResult = CheckSmallPrimesAndDivisors(value); |
| | 9 | 169 | | if (smallPrimeResult.HasValue) |
| | 6 | 170 | | { |
| | 6 | 171 | | return smallPrimeResult.Value; |
| | | 172 | | } |
| | | 173 | | |
| | 3 | 174 | | ulong[] witnesses = [2UL, 325UL, 9375UL, 28178UL, 450775UL, 9780504UL, 1795265022UL]; |
| | | 175 | | |
| | 3 | 176 | | var d = value - 1UL; |
| | 3 | 177 | | var s = 0; |
| | 7 | 178 | | while ((d & 1UL) == 0UL) |
| | 4 | 179 | | { |
| | 4 | 180 | | d >>= 1; |
| | 4 | 181 | | s++; |
| | 4 | 182 | | } |
| | | 183 | | |
| | 3 | 184 | | var candidateBigInteger = new BigInteger(value); |
| | | 185 | | |
| | 51 | 186 | | foreach (var witnessValue in witnesses) |
| | 21 | 187 | | { |
| | 21 | 188 | | if (witnessValue % value == 0UL) |
| | 0 | 189 | | { |
| | 0 | 190 | | return true; |
| | | 191 | | } |
| | | 192 | | |
| | 21 | 193 | | var witnessBigInteger = new BigInteger(witnessValue % value); |
| | 21 | 194 | | var exponentBigInteger = new BigInteger(d); |
| | | 195 | | |
| | 21 | 196 | | var x = BigInteger.ModPow(witnessBigInteger, exponentBigInteger, candidateBigInteger); |
| | | 197 | | |
| | 21 | 198 | | if (x == BigInteger.One || x == candidateBigInteger - BigInteger.One) |
| | 16 | 199 | | { |
| | 16 | 200 | | continue; |
| | | 201 | | } |
| | | 202 | | |
| | 5 | 203 | | var roundPassed = false; |
| | | 204 | | |
| | 10 | 205 | | for (var iteration = 1; iteration < s; iteration++) |
| | 5 | 206 | | { |
| | 5 | 207 | | x = (x * x) % candidateBigInteger; |
| | | 208 | | |
| | 5 | 209 | | if (x == candidateBigInteger - BigInteger.One) |
| | 5 | 210 | | { |
| | 5 | 211 | | roundPassed = true; |
| | 5 | 212 | | break; |
| | | 213 | | } |
| | 0 | 214 | | } |
| | | 215 | | |
| | 5 | 216 | | if (!roundPassed) |
| | 0 | 217 | | { |
| | 0 | 218 | | return false; |
| | | 219 | | } |
| | 5 | 220 | | } |
| | | 221 | | |
| | 3 | 222 | | return true; |
| | 9 | 223 | | } |
| | | 224 | | |
| | | 225 | | /// <summary> |
| | | 226 | | /// Determines whether the specified BigInteger is a prime number |
| | | 227 | | /// </summary> |
| | | 228 | | /// <param name="value">The value to test for primality.</param> |
| | | 229 | | /// <returns>True if <paramref name="value"/> is prime; otherwise false.</returns> |
| | | 230 | | public static bool IsPrimeNumber(BigInteger value) |
| | 35 | 231 | | { |
| | | 232 | | // Numbers less than 2 are not prime |
| | 35 | 233 | | if (value < 2) |
| | 3 | 234 | | { |
| | 3 | 235 | | return false; |
| | | 236 | | } |
| | | 237 | | |
| | 32 | 238 | | if (value == 2) |
| | 2 | 239 | | { |
| | 2 | 240 | | return true; |
| | | 241 | | } |
| | | 242 | | |
| | 30 | 243 | | if (value % 2 == 0) |
| | 14 | 244 | | { |
| | 14 | 245 | | return false; |
| | | 246 | | } |
| | | 247 | | |
| | 16 | 248 | | var limit = BigIntegerSqrt(value); |
| | | 249 | | |
| | 32666 | 250 | | for (BigInteger divisor = 3; divisor <= limit; divisor += 2) |
| | 16322 | 251 | | { |
| | 16322 | 252 | | if (value % divisor == 0) |
| | 5 | 253 | | { |
| | 5 | 254 | | return false; |
| | | 255 | | } |
| | 16317 | 256 | | } |
| | | 257 | | |
| | 11 | 258 | | return true; |
| | 35 | 259 | | } |
| | | 260 | | |
| | | 261 | | /// <summary> |
| | | 262 | | /// Computes the integer square root (floor) of the specified non-negative <see cref="BigInteger"/>. |
| | | 263 | | /// </summary> |
| | | 264 | | /// <param name="n">The non-negative value whose integer square root is to be computed.</param> |
| | | 265 | | /// <returns>The greatest integer less than or equal to the square root of <paramref name="n"/>.</returns> |
| | | 266 | | public static BigInteger BigIntegerSqrt(BigInteger n) |
| | 29 | 267 | | { |
| | 29 | 268 | | if (n < 0) |
| | 1 | 269 | | { |
| | 1 | 270 | | throw new ArithmeticException("Cannot compute square root of a negative number."); |
| | | 271 | | } |
| | | 272 | | |
| | 28 | 273 | | if (n == 0) |
| | 1 | 274 | | { |
| | 1 | 275 | | return 0; |
| | | 276 | | } |
| | | 277 | | |
| | 27 | 278 | | BigInteger low = 1; |
| | | 279 | | |
| | 27 | 280 | | var high = n; |
| | | 281 | | |
| | 216 | 282 | | while (low <= high) |
| | 197 | 283 | | { |
| | 197 | 284 | | var mid = (low + high) >> 1; // divide by 2 |
| | 197 | 285 | | var square = mid * mid; |
| | 197 | 286 | | var compareResult = square.CompareTo(n); |
| | | 287 | | |
| | 197 | 288 | | if (compareResult == 0) |
| | 8 | 289 | | { |
| | 8 | 290 | | return mid; |
| | | 291 | | } |
| | | 292 | | |
| | 189 | 293 | | if (compareResult < 0) |
| | 59 | 294 | | { |
| | 59 | 295 | | low = mid + 1; |
| | 59 | 296 | | } |
| | | 297 | | else |
| | 130 | 298 | | { |
| | 130 | 299 | | high = mid - 1; |
| | 130 | 300 | | } |
| | 189 | 301 | | } |
| | | 302 | | |
| | 19 | 303 | | return high; |
| | 28 | 304 | | } |
| | | 305 | | |
| | | 306 | | /// <summary> |
| | | 307 | | /// Checks if a number is a small prime (2, 3, 5) or divisible by small primes. |
| | | 308 | | /// </summary> |
| | | 309 | | /// <param name="value">The value to test.</param> |
| | | 310 | | /// <returns>True if prime (2, 3, or 5), false if composite (divisible by 2, 3, or 5, or less than 2), null to conti |
| | | 311 | | private static bool? CheckSmallPrimesAndDivisors(ulong value) |
| | 45 | 312 | | { |
| | 45 | 313 | | if (value < 2UL) |
| | 3 | 314 | | { |
| | 3 | 315 | | return false; |
| | | 316 | | } |
| | | 317 | | |
| | 42 | 318 | | if (value is 2UL or 3UL or 5UL) |
| | 5 | 319 | | { |
| | 5 | 320 | | return true; |
| | | 321 | | } |
| | | 322 | | |
| | 37 | 323 | | if ((value & 1UL) == 0UL || value % 3UL == 0UL || value % 5UL == 0UL) |
| | 22 | 324 | | { |
| | 22 | 325 | | return false; |
| | | 326 | | } |
| | | 327 | | |
| | 15 | 328 | | return null; |
| | 45 | 329 | | } |
| | | 330 | | } |