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