You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/// Returns an enumerable that will iterate every prime starting at the starting value.
125
+
/// </summary>
126
+
/// <param name="value">Allows for skipping ahead any integer before checking for inclusive and subsequent primes. Passing a negative number here will produce a negative set of prime numbers.</param>
127
+
/// <returns>An enumerable that will iterate every prime starting at the starting value</returns>
// For larger numbers, a quick prime check can prevent large iterations.
181
+
if(IsFactorable(value))
182
+
{
183
+
foreach(varpinthis)
184
+
{
185
+
varstop=value/last;// The list of possibilities shrinks for each test.
186
+
if(p>stop)break;// Exceeded possibilities?
187
+
while((value%p)==0U)
188
+
{
189
+
value/=p;
190
+
yieldreturnp;
191
+
if(value==1U)yieldbreak;
192
+
}
193
+
last=p;
194
+
}
195
+
}
196
+
}
197
+
198
+
yieldreturnvalue;
199
+
}
200
+
201
+
/// <summary>
202
+
/// Iterates the prime factors of the provided value.
203
+
/// First multiple is always 0, 1 or -1.
204
+
/// </summary>
205
+
/// <param name="value">The value to factorize.</param>
206
+
/// <returns>An enumerable that contains the prime factors of the provided value starting with 0, 1, or -1 for sign retention.</returns>
207
+
publicIEnumerable<int>Factors(intvalue)
208
+
{
209
+
if(value!=0L)
210
+
{
211
+
yieldreturnvalue<0?-1:1;
212
+
if(value<0)value=Math.Abs(value);
213
+
if(value==1)
214
+
yieldbreak;
215
+
216
+
varlast=1;
217
+
218
+
// For larger numbers, a quick prime check can prevent large iterations.
219
+
if(IsFactorable(value))
220
+
{
221
+
foreach(varpinStartingAt(2))
222
+
{
223
+
varstop=value/last;// The list of possibilities shrinks for each test.
224
+
if(p>stop)break;// Exceeded possibilities?
225
+
while((value%p)==0)
226
+
{
227
+
value/=p;
228
+
yieldreturnp;
229
+
if(value==1)yieldbreak;
230
+
}
231
+
last=p;
232
+
}
233
+
}
234
+
}
235
+
yieldreturnvalue;
236
+
}
237
+
238
+
protectedboolIsFactorable(intvalue)
239
+
=>!IsPrime(value);
240
+
241
+
/// <inheritdoc />
242
+
publicoverrideuintNext(inuintafter)
243
+
=>StartingAt(after+1).First();
244
+
245
+
/// <summary>
246
+
/// Finds the next prime number after the number given.
247
+
/// </summary>
248
+
/// <param name="after">The excluded lower boundary to start with. If this number is negative, then the result will be the next greater magnitude value prime as negative number.</param>
249
+
/// <returns>The next prime after the number provided.</returns>
250
+
publicintNext(inintafter)
251
+
=>StartingAt(after+1).First();
252
+
253
+
/// <inheritdoc />
254
+
publicsealedoverrideboolIsPrime(inuintvalue)
255
+
{
256
+
switch(value)
257
+
{
258
+
// 0 and 1 are not prime numbers
259
+
case0U:
260
+
case1U:
261
+
returnfalse;
262
+
case2U:
263
+
case3U:
264
+
returntrue;
265
+
266
+
default:
267
+
268
+
if(value%2U==0||value%3U==0)
269
+
returnfalse;
270
+
271
+
returnIsPrimeInternal(value);
272
+
}
273
+
}
274
+
275
+
/// <summary>
276
+
/// Returns true if the value provided is prime.
277
+
/// </summary>
278
+
/// <param name="value">The value to validate.</param>
279
+
/// <returns>True if the value provided is prime</returns>
280
+
publicboolIsPrime(intvalue)
281
+
=>IsPrime(Convert.ToUInt32(Math.Abs(value)));
282
+
283
+
/// <summary>
284
+
/// Should only check for primes that aren't divisible by 2 or 3.
0 commit comments