Skip to content

Commit

Permalink
Merge.
Browse files Browse the repository at this point in the history
  • Loading branch information
SuprDewd committed Oct 17, 2011
2 parents d3bea26 + e80ca75 commit 21c15aa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 58 deletions.
93 changes: 41 additions & 52 deletions SharpBag/Math/BagMath.cs
Expand Up @@ -134,15 +134,10 @@ public static bool IsPrime(int n, bool checkCache = true)
if (n < 4) return true;
if (n % 2 == 0) return false;
if (n < 9) return true;
if (n % 3 == 0) return false;

int r = (int)System.Math.Sqrt(n), lastPrime = 0;
for (int i = 1; i < BagMath.SmallPrimes.Length; i++)
{
if (BagMath.SmallPrimes[i] > r) return true;
if (n % (lastPrime = BagMath.SmallPrimes[i]) == 0) return false;
}

int f = ((lastPrime - 5) / 6) * 6 + 5;
uint r = (uint)System.Math.Sqrt(n);
uint f = 5;
while (f <= r)
{
if (n % f == 0) return false;
Expand All @@ -166,16 +161,10 @@ public static bool IsPrime(long n, bool checkCache = true)
if (n < 4) return true;
if (n % 2 == 0) return false;
if (n < 9) return true;
if (n % 3 == 0) return false;

uint r = (uint)System.Math.Sqrt(n);
uint lastPrime = 0;
for (int i = 1; i < BagMath.SmallPrimes.Length; i++)
{
if (BagMath.SmallPrimes[i] > r) return true;
if (n % (lastPrime = (uint)BagMath.SmallPrimes[i]) == 0) return false;
}

uint f = ((lastPrime - 5) / 6) * 6 + 5;
uint r = (uint)S.Math.Sqrt(n);
uint f = 5;
while (f <= r)
{
if (n % f == 0) return false;
Expand All @@ -199,16 +188,11 @@ public static bool IsPrime(BigInteger n, bool checkCache = true)
if (n < 4) return true;
if (n % 2 == 0) return false;
if (n < 9) return true;
if (n % 3 == 0) return false;

BigInteger r = n.Sqrt();
uint lastPrime = 0;
for (int i = 1; i < BagMath.SmallPrimes.Length; i++)
{
if (BagMath.SmallPrimes[i] > r) return true;
if (n % (lastPrime = (uint)BagMath.SmallPrimes[i]) == 0) return false;
}
BigInteger r = n.Sqrt(),
f = 5;

BigInteger f = ((lastPrime - 5) / 6) * 6 + 5;
while (f <= r)
{
if (n % f == 0) return false;
Expand Down Expand Up @@ -243,9 +227,10 @@ public static IEnumerable<int> SieveOfEratosthenes(int upperLimit)
{
if (!PrimeBits[i])
{
yield return 2 * i + 1;
int current = 2 * i + 1;
yield return current;

for (int j = i * 2 * (i + 1); j <= sieveBound; j += 2 * i + 1)
for (int j = i * 2 * (i + 1); j <= sieveBound; j += current)
{
PrimeBits[j] = true;
}
Expand Down Expand Up @@ -279,9 +264,10 @@ public static IEnumerable<ulong> SieveOfEratosthenes(ulong upperLimit)
{
if (!PrimeBits[i])
{
yield return 2 * i + 1;
ulong current = 2 * i + 1;
yield return current;

for (ulong j = i * 2 * (i + 1); j <= sieveBound; j += 2 * i + 1)
for (ulong j = current; j <= sieveBound; j += current)
{
PrimeBits[j] = true;
}
Expand Down Expand Up @@ -318,15 +304,18 @@ public static IEnumerable<ulong> SieveOfAtkin(ulong max)
for (ulong x = 1; x <= sqrt; x++)
for (ulong y = 1; y <= sqrt; y++)
{
var n = 4 * x * x + y * y;
var xsq = x * x;
var ysq = y * y;
var n = 4 * xsq + ysq;

if (n <= max && (n % 12 == 1 || n % 12 == 5))
isPrime[n] ^= true;

n = 3 * x * x + y * y;
n = 3 * xsq + ysq;
if (n <= max && n % 12 == 7)
isPrime[n] ^= true;

n = 3 * x * x - y * y;
n = 3 * xsq - ysq;
if (x > y && n <= max && n % 12 == 11)
isPrime[n] ^= true;
}
Expand Down Expand Up @@ -473,26 +462,26 @@ public static BigInteger CollatzCount(BigInteger n)

#region SmallPrimes

private static readonly int[] _SmallPrimes = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097,
1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193,
1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499,
1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699,
1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889,
private static readonly int[] _SmallPrimes = {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97,
101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199,
211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293,
307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,
401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499,
503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599,
601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691,
701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797,
809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997,
1009, 1013, 1019, 1021, 1031, 1033, 1039, 1049, 1051, 1061, 1063, 1069, 1087, 1091, 1093, 1097,
1103, 1109, 1117, 1123, 1129, 1151, 1153, 1163, 1171, 1181, 1187, 1193,
1201, 1213, 1217, 1223, 1229, 1231, 1237, 1249, 1259, 1277, 1279, 1283, 1289, 1291, 1297,
1301, 1303, 1307, 1319, 1321, 1327, 1361, 1367, 1373, 1381, 1399,
1409, 1423, 1427, 1429, 1433, 1439, 1447, 1451, 1453, 1459, 1471, 1481, 1483, 1487, 1489, 1493, 1499,
1511, 1523, 1531, 1543, 1549, 1553, 1559, 1567, 1571, 1579, 1583, 1597,
1601, 1607, 1609, 1613, 1619, 1621, 1627, 1637, 1657, 1663, 1667, 1669, 1693, 1697, 1699,
1709, 1721, 1723, 1733, 1741, 1747, 1753, 1759, 1777, 1783, 1787, 1789,
1801, 1811, 1823, 1831, 1847, 1861, 1867, 1871, 1873, 1877, 1879, 1889,
1901, 1907, 1913, 1931, 1933, 1949, 1951, 1973, 1979, 1987, 1993, 1997, 1999 };

/// <summary>
Expand Down
Binary file not shown.
Binary file modified SharpBag/bin/Release/SharpBag.dll
Binary file not shown.
12 changes: 6 additions & 6 deletions SharpBag/bin/Release/SharpBagDocs.xml
Expand Up @@ -1712,22 +1712,22 @@
</member>
<member name="M:SharpBag.Math.MatrixBase`2.#ctor(System.Int32,System.Int32)">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.MatrixBase`2"/> class.
Initializes a new instance of the MatrixBase class.
</summary>
<param name="rows">The number of rows.</param>
<param name="columns">The number of columns.</param>
</member>
<member name="M:SharpBag.Math.MatrixBase`2.#ctor(System.Int32,System.Int32,`0)">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.MatrixBase`2"/> class.
Initializes a new instance of the MatrixBase class.
</summary>
<param name="rows">The number of rows.</param>
<param name="columns">The number of columns.</param>
<param name="def">The default value.</param>
</member>
<member name="M:SharpBag.Math.MatrixBase`2.#ctor(`0[0:,0:])">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.MatrixBase`2"/> class.
Initializes a new instance of the MatrixBase class.
</summary>
<param name="elements">The elements.</param>
</member>
Expand Down Expand Up @@ -2681,20 +2681,20 @@
</member>
<member name="M:SharpBag.Math.VectorBase`2.#ctor(System.Int32)">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.VectorBase`2"/> class.
Initializes a new instance of the VectorBase class.
</summary>
<param name="dimension">The dimension.</param>
</member>
<member name="M:SharpBag.Math.VectorBase`2.#ctor(System.Int32,`0)">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.VectorBase`2"/> class.
Initializes a new instance of the VectorBase class.
</summary>
<param name="dimension">The dimension.</param>
<param name="def">The default value.</param>
</member>
<member name="M:SharpBag.Math.VectorBase`2.#ctor(`0[])">
<summary>
Initializes a new instance of the <see cref="T:SharpBag.Math.VectorBase`2"/> class.
Initializes a new instance of the VectorBase class.
</summary>
<param name="elements">The elements.</param>
</member>
Expand Down

0 comments on commit 21c15aa

Please sign in to comment.