| | | 1 | | using System.Numerics; |
| | | 2 | | |
| | | 3 | | namespace ArturRios.Util.Math; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// A generic prime number generator that generates primes of a specified integer type. |
| | | 7 | | /// Create an instance to get an independent sequence of primes. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <typeparam name="T">The integer type to generate primes for. Supported types: sbyte, byte, short, ushort, int, uint, |
| | | 10 | | public class PrimeGenerator<T> where T : struct |
| | | 11 | | { |
| | | 12 | | private T _current; |
| | 9 | 13 | | private readonly Lock _lock = new(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Initializes a new instance of the <see cref="PrimeGenerator{T}"/> class. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <exception cref="ArgumentException">Thrown if T is not a supported integer type.</exception> |
| | 9 | 19 | | public PrimeGenerator() |
| | 9 | 20 | | { |
| | 9 | 21 | | ValidateSupportedType(); |
| | 8 | 22 | | _current = GetStartValue(); |
| | 8 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Returns the next prime in the sequence (2, 3, 5, 7, ...). |
| | | 27 | | /// Thread-safe. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <returns>The next prime number of type T.</returns> |
| | | 30 | | /// <exception cref="OverflowException">Thrown when the next prime exceeds the maximum value for type T.</exception> |
| | | 31 | | public T Next() |
| | 78 | 32 | | { |
| | | 33 | | lock (_lock) |
| | 78 | 34 | | { |
| | 78 | 35 | | var candidate = Increment(_current); |
| | 77 | 36 | | EnsureAtLeastTwo(ref candidate); |
| | | 37 | | |
| | 245 | 38 | | while (!IsPrime(candidate)) |
| | 168 | 39 | | { |
| | 168 | 40 | | candidate = Increment(candidate); |
| | 168 | 41 | | } |
| | | 42 | | |
| | 77 | 43 | | _current = candidate; |
| | 77 | 44 | | return candidate; |
| | | 45 | | } |
| | 77 | 46 | | } |
| | | 47 | | |
| | | 48 | | /// <summary> |
| | | 49 | | /// Validates that T is one of the supported integer types. |
| | | 50 | | /// </summary> |
| | | 51 | | /// <exception cref="ArgumentException">Thrown if T is not a supported type.</exception> |
| | | 52 | | private static void ValidateSupportedType() |
| | 9 | 53 | | { |
| | 9 | 54 | | var typeOfT = typeof(T); |
| | 9 | 55 | | if (typeOfT != typeof(sbyte) && typeOfT != typeof(byte) && |
| | 9 | 56 | | typeOfT != typeof(short) && typeOfT != typeof(ushort) && |
| | 9 | 57 | | typeOfT != typeof(int) && typeOfT != typeof(uint) && |
| | 9 | 58 | | typeOfT != typeof(long) && typeOfT != typeof(ulong) && |
| | 9 | 59 | | typeOfT != typeof(BigInteger)) |
| | 1 | 60 | | { |
| | 1 | 61 | | throw new ArgumentException( |
| | 1 | 62 | | $"Type '{typeof(T).Name}' is not supported for PrimeGenerator. " + |
| | 1 | 63 | | $"Supported types are: sbyte, byte, short, ushort, int, uint, long, ulong, and BigInteger."); |
| | | 64 | | } |
| | 8 | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Gets the starting value (one less than the first prime). |
| | | 69 | | /// </summary> |
| | | 70 | | private static T GetStartValue() |
| | 8 | 71 | | { |
| | 8 | 72 | | if (typeof(T) == typeof(sbyte)) |
| | 1 | 73 | | { |
| | 1 | 74 | | return (T)(object)(sbyte)1; |
| | | 75 | | } |
| | | 76 | | |
| | 7 | 77 | | if (typeof(T) == typeof(byte)) |
| | 1 | 78 | | { |
| | 1 | 79 | | return (T)(object)(byte)1; |
| | | 80 | | } |
| | | 81 | | |
| | 6 | 82 | | if (typeof(T) == typeof(short)) |
| | 0 | 83 | | { |
| | 0 | 84 | | return (T)(object)(short)1; |
| | | 85 | | } |
| | | 86 | | |
| | 6 | 87 | | if (typeof(T) == typeof(ushort)) |
| | 0 | 88 | | { |
| | 0 | 89 | | return (T)(object)(ushort)1; |
| | | 90 | | } |
| | | 91 | | |
| | 6 | 92 | | if (typeof(T) == typeof(int)) |
| | 4 | 93 | | { |
| | 4 | 94 | | return (T)(object)1; |
| | | 95 | | } |
| | | 96 | | |
| | 2 | 97 | | if (typeof(T) == typeof(uint)) |
| | 0 | 98 | | { |
| | 0 | 99 | | return (T)(object)1U; |
| | | 100 | | } |
| | | 101 | | |
| | 2 | 102 | | if (typeof(T) == typeof(long)) |
| | 1 | 103 | | { |
| | 1 | 104 | | return (T)(object)1L; |
| | | 105 | | } |
| | | 106 | | |
| | 1 | 107 | | if (typeof(T) == typeof(ulong)) |
| | 0 | 108 | | { |
| | 0 | 109 | | return (T)(object)1UL; |
| | | 110 | | } |
| | | 111 | | |
| | 1 | 112 | | if (typeof(T) == typeof(BigInteger)) |
| | 1 | 113 | | { |
| | 1 | 114 | | return (T)(object)BigInteger.One; |
| | | 115 | | } |
| | | 116 | | |
| | 0 | 117 | | throw new InvalidOperationException("Unexpected type in GetStartValue"); |
| | 8 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <summary> |
| | | 121 | | /// Increments the given value by one, throwing on overflow. |
| | | 122 | | /// </summary> |
| | | 123 | | private static T Increment(T value) |
| | 246 | 124 | | { |
| | 246 | 125 | | if (typeof(T) == typeof(sbyte)) |
| | 127 | 126 | | { |
| | 127 | 127 | | var sbyteValue = (sbyte)(object)value; |
| | 127 | 128 | | if (sbyteValue == sbyte.MaxValue) |
| | 1 | 129 | | { |
| | 1 | 130 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 131 | | } |
| | | 132 | | |
| | 126 | 133 | | return (T)(object)(sbyte)(sbyteValue + 1); |
| | | 134 | | } |
| | | 135 | | |
| | 119 | 136 | | if (typeof(T) == typeof(byte)) |
| | 28 | 137 | | { |
| | 28 | 138 | | var byteValue = (byte)(object)value; |
| | 28 | 139 | | if (byteValue == byte.MaxValue) |
| | 0 | 140 | | { |
| | 0 | 141 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 142 | | } |
| | | 143 | | |
| | 28 | 144 | | return (T)(object)(byte)(byteValue + 1); |
| | | 145 | | } |
| | | 146 | | |
| | 91 | 147 | | if (typeof(T) == typeof(short)) |
| | 0 | 148 | | { |
| | 0 | 149 | | var shortValue = (short)(object)value; |
| | 0 | 150 | | if (shortValue == short.MaxValue) |
| | 0 | 151 | | { |
| | 0 | 152 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 153 | | } |
| | | 154 | | |
| | 0 | 155 | | return (T)(object)(short)(shortValue + 1); |
| | | 156 | | } |
| | | 157 | | |
| | 91 | 158 | | if (typeof(T) == typeof(ushort)) |
| | 0 | 159 | | { |
| | 0 | 160 | | var ushortValue = (ushort)(object)value; |
| | 0 | 161 | | if (ushortValue == ushort.MaxValue) |
| | 0 | 162 | | { |
| | 0 | 163 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 164 | | } |
| | | 165 | | |
| | 0 | 166 | | return (T)(object)(ushort)(ushortValue + 1); |
| | | 167 | | } |
| | | 168 | | |
| | 91 | 169 | | if (typeof(T) == typeof(int)) |
| | 35 | 170 | | { |
| | 35 | 171 | | var intValue = (int)(object)value; |
| | 35 | 172 | | if (intValue == int.MaxValue) |
| | 0 | 173 | | { |
| | 0 | 174 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 175 | | } |
| | | 176 | | |
| | 35 | 177 | | return (T)(object)(intValue + 1); |
| | | 178 | | } |
| | | 179 | | |
| | 56 | 180 | | if (typeof(T) == typeof(uint)) |
| | 0 | 181 | | { |
| | 0 | 182 | | var uintValue = (uint)(object)value; |
| | 0 | 183 | | if (uintValue == uint.MaxValue) |
| | 0 | 184 | | { |
| | 0 | 185 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 186 | | } |
| | | 187 | | |
| | 0 | 188 | | return (T)(object)(uintValue + 1U); |
| | | 189 | | } |
| | | 190 | | |
| | 56 | 191 | | if (typeof(T) == typeof(long)) |
| | 28 | 192 | | { |
| | 28 | 193 | | var longValue = (long)(object)value; |
| | 28 | 194 | | if (longValue == long.MaxValue) |
| | 0 | 195 | | { |
| | 0 | 196 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 197 | | } |
| | | 198 | | |
| | 28 | 199 | | return (T)(object)(longValue + 1L); |
| | | 200 | | } |
| | | 201 | | |
| | 28 | 202 | | if (typeof(T) == typeof(ulong)) |
| | 0 | 203 | | { |
| | 0 | 204 | | var ulongValue = (ulong)(object)value; |
| | 0 | 205 | | if (ulongValue == ulong.MaxValue) |
| | 0 | 206 | | { |
| | 0 | 207 | | throw new OverflowException($"Cannot increment beyond maximum value for {typeof(T).Name}"); |
| | | 208 | | } |
| | | 209 | | |
| | 0 | 210 | | return (T)(object)(ulongValue + 1UL); |
| | | 211 | | } |
| | | 212 | | |
| | 28 | 213 | | if (typeof(T) == typeof(BigInteger)) |
| | 28 | 214 | | { |
| | 28 | 215 | | var bigIntegerValue = (BigInteger)(object)value; |
| | 28 | 216 | | return (T)(object)(bigIntegerValue + BigInteger.One); |
| | | 217 | | } |
| | | 218 | | |
| | 0 | 219 | | throw new InvalidOperationException("Unexpected type in Increment"); |
| | 245 | 220 | | } |
| | | 221 | | |
| | | 222 | | /// <summary> |
| | | 223 | | /// Ensures the candidate is at least 2 (the first prime). |
| | | 224 | | /// </summary> |
| | | 225 | | private static void EnsureAtLeastTwo(ref T candidate) |
| | 77 | 226 | | { |
| | 77 | 227 | | if (typeof(T) == typeof(sbyte)) |
| | 31 | 228 | | { |
| | 31 | 229 | | var sbyteValue = (sbyte)(object)candidate; |
| | 31 | 230 | | if (sbyteValue < 2) |
| | 0 | 231 | | { |
| | 0 | 232 | | candidate = (T)(object)(sbyte)2; |
| | 0 | 233 | | } |
| | 31 | 234 | | } |
| | 46 | 235 | | else if (typeof(T) == typeof(byte)) |
| | 10 | 236 | | { |
| | 10 | 237 | | var byteValue = (byte)(object)candidate; |
| | 10 | 238 | | if (byteValue < 2) |
| | 0 | 239 | | { |
| | 0 | 240 | | candidate = (T)(object)(byte)2; |
| | 0 | 241 | | } |
| | 10 | 242 | | } |
| | 36 | 243 | | else if (typeof(T) == typeof(short)) |
| | 0 | 244 | | { |
| | 0 | 245 | | var shortValue = (short)(object)candidate; |
| | 0 | 246 | | if (shortValue < 2) |
| | 0 | 247 | | { |
| | 0 | 248 | | candidate = (T)(object)(short)2; |
| | 0 | 249 | | } |
| | 0 | 250 | | } |
| | 36 | 251 | | else if (typeof(T) == typeof(ushort)) |
| | 0 | 252 | | { |
| | 0 | 253 | | var ushortValue = (ushort)(object)candidate; |
| | 0 | 254 | | if (ushortValue < 2) |
| | 0 | 255 | | { |
| | 0 | 256 | | candidate = (T)(object)(ushort)2; |
| | 0 | 257 | | } |
| | 0 | 258 | | } |
| | 36 | 259 | | else if (typeof(T) == typeof(int)) |
| | 16 | 260 | | { |
| | 16 | 261 | | var intValue = (int)(object)candidate; |
| | 16 | 262 | | if (intValue < 2) |
| | 0 | 263 | | { |
| | 0 | 264 | | candidate = (T)(object)2; |
| | 0 | 265 | | } |
| | 16 | 266 | | } |
| | 20 | 267 | | else if (typeof(T) == typeof(uint)) |
| | 0 | 268 | | { |
| | 0 | 269 | | var uintValue = (uint)(object)candidate; |
| | 0 | 270 | | if (uintValue < 2) |
| | 0 | 271 | | { |
| | 0 | 272 | | candidate = (T)(object)2U; |
| | 0 | 273 | | } |
| | 0 | 274 | | } |
| | 20 | 275 | | else if (typeof(T) == typeof(long)) |
| | 10 | 276 | | { |
| | 10 | 277 | | var longValue = (long)(object)candidate; |
| | 10 | 278 | | if (longValue < 2) |
| | 0 | 279 | | { |
| | 0 | 280 | | candidate = (T)(object)2L; |
| | 0 | 281 | | } |
| | 10 | 282 | | } |
| | 10 | 283 | | else if (typeof(T) == typeof(ulong)) |
| | 0 | 284 | | { |
| | 0 | 285 | | var ulongValue = (ulong)(object)candidate; |
| | 0 | 286 | | if (ulongValue < 2) |
| | 0 | 287 | | { |
| | 0 | 288 | | candidate = (T)(object)2UL; |
| | 0 | 289 | | } |
| | 0 | 290 | | } |
| | 10 | 291 | | else if (typeof(T) == typeof(BigInteger)) |
| | 10 | 292 | | { |
| | 10 | 293 | | var bigIntegerValue = (BigInteger)(object)candidate; |
| | 10 | 294 | | if (bigIntegerValue < BigInteger.One + BigInteger.One) |
| | 0 | 295 | | { |
| | 0 | 296 | | candidate = (T)(object)(BigInteger.One + BigInteger.One); |
| | 0 | 297 | | } |
| | 10 | 298 | | } |
| | 77 | 299 | | } |
| | | 300 | | |
| | | 301 | | /// <summary> |
| | | 302 | | /// Tests if the candidate is prime using PrimeUtils.IsPrimeNumber. |
| | | 303 | | /// </summary> |
| | | 304 | | private static bool IsPrime(T candidate) |
| | 245 | 305 | | { |
| | 245 | 306 | | if (typeof(T) == typeof(sbyte)) |
| | 126 | 307 | | { |
| | 126 | 308 | | return PrimeUtils.IsPrimeNumber((sbyte)(object)candidate); |
| | | 309 | | } |
| | | 310 | | |
| | 119 | 311 | | if (typeof(T) == typeof(byte)) |
| | 28 | 312 | | { |
| | 28 | 313 | | return PrimeUtils.IsPrimeNumber((byte)(object)candidate); |
| | | 314 | | } |
| | | 315 | | |
| | 91 | 316 | | if (typeof(T) == typeof(short)) |
| | 0 | 317 | | { |
| | 0 | 318 | | return PrimeUtils.IsPrimeNumber((short)(object)candidate); |
| | | 319 | | } |
| | | 320 | | |
| | 91 | 321 | | if (typeof(T) == typeof(ushort)) |
| | 0 | 322 | | { |
| | 0 | 323 | | return PrimeUtils.IsPrimeNumber((ushort)(object)candidate); |
| | | 324 | | } |
| | | 325 | | |
| | 91 | 326 | | if (typeof(T) == typeof(int)) |
| | 35 | 327 | | { |
| | 35 | 328 | | return PrimeUtils.IsPrimeNumber((int)(object)candidate); |
| | | 329 | | } |
| | | 330 | | |
| | 56 | 331 | | if (typeof(T) == typeof(uint)) |
| | 0 | 332 | | { |
| | 0 | 333 | | return PrimeUtils.IsPrimeNumber((uint)(object)candidate); |
| | | 334 | | } |
| | | 335 | | |
| | 56 | 336 | | if (typeof(T) == typeof(long)) |
| | 28 | 337 | | { |
| | 28 | 338 | | return PrimeUtils.IsPrimeNumber((long)(object)candidate); |
| | | 339 | | } |
| | | 340 | | |
| | 28 | 341 | | if (typeof(T) == typeof(ulong)) |
| | 0 | 342 | | { |
| | 0 | 343 | | return PrimeUtils.IsPrimeNumber((ulong)(object)candidate); |
| | | 344 | | } |
| | | 345 | | |
| | 28 | 346 | | if (typeof(T) == typeof(BigInteger)) |
| | 28 | 347 | | { |
| | 28 | 348 | | return PrimeUtils.IsPrimeNumber((BigInteger)(object)candidate); |
| | | 349 | | } |
| | | 350 | | |
| | 0 | 351 | | throw new InvalidOperationException("Unexpected type in IsPrime"); |
| | 245 | 352 | | } |
| | | 353 | | } |