Skip to content

Commit 7dae698

Browse files
author
Oren (electricessence)
committed
Updated with fixes.
1 parent f3d0034 commit 7dae698

File tree

4 files changed

+84
-11
lines changed

4 files changed

+84
-11
lines changed

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,55 @@
33
Methods and extensions for prime number detection and discovery.
44

55
[![NuGet](https://img.shields.io/nuget/v/Open.Numeric.Primes.svg)](https://www.nuget.org/packages/Open.Numeric.Primes/)
6+
7+
## Examples
8+
9+
### Importing
10+
11+
```cs
12+
using Open.Numeric.Primes;
13+
```
14+
15+
### Primality Test
16+
17+
```cs
18+
Number.IsPrime(8592868089022906369) // true
19+
```
20+
21+
### Factors
22+
23+
```cs
24+
Prime.Factors(12) // 2, 2, 3
25+
```
26+
27+
### Common Factors
28+
29+
```cs
30+
Prime.CommonFactors(84, 756, 108) // 2, 2, 3
31+
```
32+
33+
### Greatest Factor
34+
35+
```cs
36+
Prime.GreatestFactor(84, 756, 108) // 12
37+
```
38+
39+
### Prime Discovery
40+
41+
```cs
42+
// Will list the first 1000 primes.
43+
foreach(var prime in Prime.Numbers.Take(1000))
44+
{
45+
Console.Write(prime);
46+
}
47+
```
48+
49+
or
50+
51+
```cs
52+
// Will list the first 1000 primes greater than (or equal to) 10,000.
53+
foreach(var prime in Prime.Numbers.StartingAt(10000).Take(100))
54+
{
55+
Console.Write(prime);
56+
}
57+
```

source/Open.Numeric.Primes.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@
1111

1212
Part of the "Open" set of libraries.
1313
</Description>
14-
<PackageTags>prime numbers;primes;prime factorization;prime detection;prime discovery</PackageTags>
14+
<PackageTags>prime numbers;primes;prime factorization;prime detection;prime discovery;primality;primality test;factors;common factors;greatest factor;</PackageTags>
1515
<Copyright>© electricessence (Oren F.) All rights reserved.</Copyright>
1616
<PackageProjectUrl>https://github.com/Open-NET-Libraries/Open.Numeric.Primes/</PackageProjectUrl>
1717
<RepositoryUrl>https://github.com/Open-NET-Libraries/Open.Numeric.Primes/</RepositoryUrl>
1818
<RepositoryType>git</RepositoryType>
19-
<Version>1.6.0</Version>
19+
<Version>2.0.0</Version>
2020
<PackageReleaseNotes></PackageReleaseNotes>
2121
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2222
<PublishRepositoryUrl>true</PublishRepositoryUrl>

source/Polynomial.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ namespace Open.Numeric.Primes
44
{
55
public static class Polynomial
66
{
7-
const ulong MAX_ULONG_DIVISOR = 4294967296UL;
7+
const uint MAX_UINT_DIVISOR = 65536U;
88

9-
internal static bool IsPrimeInternal(in ulong value)
9+
internal static bool IsPrimeInternal(uint value)
1010
{
11-
ulong divisor = 6;
11+
uint divisor = 6;
1212
while (divisor * divisor - 2 * divisor + 1 <= value)
1313
{
1414
if (value % (divisor - 1) == 0)
@@ -19,17 +19,19 @@ internal static bool IsPrimeInternal(in ulong value)
1919

2020
divisor += 6;
2121

22-
if (divisor > MAX_ULONG_DIVISOR)
23-
return IsPrime(value, divisor);
22+
if (divisor > MAX_UINT_DIVISOR)
23+
return IsPrimeInternal(value, divisor);
2424
}
2525
return true;
2626
}
2727

28-
const uint MAX_UINT_DIVISOR = 65536U;
28+
const ulong MAX_ULONG_DIVISOR = 4294967296UL;
2929

30-
internal static bool IsPrimeInternal(uint value)
30+
internal static bool IsPrimeInternal(in ulong value, ulong divisor = 6)
3131
{
32-
uint divisor = 6;
32+
if (divisor > MAX_ULONG_DIVISOR)
33+
return IsPrime(value, divisor);
34+
3335
while (divisor * divisor - 2 * divisor + 1 <= value)
3436
{
3537
if (value % (divisor - 1) == 0)
@@ -40,9 +42,10 @@ internal static bool IsPrimeInternal(uint value)
4042

4143
divisor += 6;
4244

43-
if (divisor > MAX_UINT_DIVISOR)
45+
if (divisor > MAX_ULONG_DIVISOR)
4446
return IsPrime(value, divisor);
4547
}
48+
4649
return true;
4750
}
4851

source/Primes.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,15 @@ public static long GreatestFactor(IEnumerable<long> values)
479479
public static ulong GreatestFactor(IEnumerable<ulong> values)
480480
=> CommonFactors(values).Aggregate(1UL, (p, c) => p * c);
481481

482+
/// <summary>
483+
/// Returns the greatest common (positive) factor (GCF) of all the provided values.
484+
/// Returns 1 if none found.
485+
/// </summary>
486+
/// <param name="values">The values to find common prime factors from.</param>
487+
/// <returns>The greatest common factor or 1 if none found.</returns>
488+
public static ulong GreatestFactor(params ulong[] values)
489+
=> CommonFactors(values).Aggregate(1UL, (p, c) => p * c);
490+
482491
/// <summary>
483492
/// Returns the greatest common (positive) factor (GCF) of all the provided values.
484493
/// Returns 1 if none found.
@@ -487,6 +496,15 @@ public static ulong GreatestFactor(IEnumerable<ulong> values)
487496
/// <returns>The greatest common factor or 1 if none found.</returns>
488497
public static BigInteger GreatestFactor(IEnumerable<BigInteger> values)
489498
=> CommonFactors(values).Aggregate(BigInteger.One, (p, c) => p * c);
499+
500+
/// <summary>
501+
/// Returns the greatest common (positive) factor (GCF) of all the provided values.
502+
/// Returns 1 if none found.
503+
/// </summary>
504+
/// <param name="values">The values to find common prime factors from.</param>
505+
/// <returns>The greatest common factor or 1 if none found.</returns>
506+
public static BigInteger GreatestFactor(params BigInteger[] values)
507+
=> CommonFactors(values).Aggregate(BigInteger.One, (p, c) => p * c);
490508
}
491509

492510

0 commit comments

Comments
 (0)