aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PrimeNumberHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/PrimeNumberHelper.cs')
-rw-r--r--OpenSim/Framework/PrimeNumberHelper.cs98
1 files changed, 98 insertions, 0 deletions
diff --git a/OpenSim/Framework/PrimeNumberHelper.cs b/OpenSim/Framework/PrimeNumberHelper.cs
new file mode 100644
index 0000000..e4bf615
--- /dev/null
+++ b/OpenSim/Framework/PrimeNumberHelper.cs
@@ -0,0 +1,98 @@
1// --------------------------------------------------------------------------------------------------------------------
2// <copyright company="" file="PrimeNumberHelper.cs">
3//
4// </copyright>
5// <summary>
6//
7// </summary>
8//
9// --------------------------------------------------------------------------------------------------------------------
10
11using System;
12
13namespace OpenSim.Framework
14{
15 /// <summary>
16 /// Utility class that is used to find small prime numbers and test is number prime number.
17 /// </summary>
18 public static class PrimeNumberHelper
19 {
20 /// <summary>
21 /// Precalculated prime numbers.
22 /// </summary>
23 private static readonly int[] Primes = new int[]
24 {
25 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239,
26 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333,
27 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
28 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431,
29 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449,
30 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
31 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559,
32 5999471, 7199369
33 };
34
35 /// <summary>
36 /// Get prime number that is equal or larger than <see cref="min"/>.
37 /// </summary>
38 /// <param name="min">
39 /// Minimal returned prime number.
40 /// </param>
41 /// <returns>
42 /// Primer number that is equal or larger than <see cref="min"/>. If <see cref="min"/> is too large, return -1.
43 /// </returns>
44 public static int GetPrime( int min )
45 {
46 if( min <= 2 )
47 return 2;
48
49 if( Primes[ Primes.Length - 1 ] < min )
50 {
51 for( int i = min | 1 ; i < 0x7FFFFFFF ; i += 2 )
52 {
53 if( IsPrime( i ) )
54 return i;
55 }
56
57 return -1;
58 }
59
60 for( int i = Primes.Length - 2 ; i >= 0 ; i-- )
61 {
62 if( min == Primes[ i ] )
63 return min;
64
65 if( min > Primes[ i ] )
66 return Primes[ i + 1 ];
67 }
68
69 return 2;
70 }
71
72 /// <summary>
73 /// Just basic Sieve of Eratosthenes prime number test.
74 /// </summary>
75 /// <param name="candinate">
76 /// Number that is tested.
77 /// </param>
78 /// <returns>
79 /// true, if <see cref="candinate"/> is prime number; otherwise false.
80 /// </returns>
81 public static bool IsPrime( int candinate )
82 {
83 if( (candinate & 1) == 0 )
84
85 // Even number - only prime if 2
86 return candinate == 2;
87
88 int upperBound = (int) Math.Sqrt( candinate );
89 for( int i = 3 ; i < upperBound ; i += 2 )
90 {
91 if( candinate % i == 0 )
92 return false;
93 }
94
95 return true;
96 }
97 }
98}