aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/PrimeNumberHelper.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/PrimeNumberHelper.cs114
1 files changed, 0 insertions, 114 deletions
diff --git a/OpenSim/Framework/PrimeNumberHelper.cs b/OpenSim/Framework/PrimeNumberHelper.cs
deleted file mode 100644
index 39079de..0000000
--- a/OpenSim/Framework/PrimeNumberHelper.cs
+++ /dev/null
@@ -1,114 +0,0 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSimulator Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27using System;
28
29namespace OpenSim.Framework
30{
31 /// <summary>
32 /// Utility class that is used to find small prime numbers and test is number prime number.
33 /// </summary>
34 public static class PrimeNumberHelper
35 {
36 /// <summary>
37 /// Precalculated prime numbers.
38 /// </summary>
39 private static readonly int[] Primes = new int[]
40 {
41 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239,
42 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333,
43 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
44 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431,
45 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449,
46 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
47 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559,
48 5999471, 7199369
49 };
50
51 /// <summary>
52 /// Get prime number that is equal or larger than <see cref="min"/>.
53 /// </summary>
54 /// <param name="min">
55 /// Minimal returned prime number.
56 /// </param>
57 /// <returns>
58 /// Primer number that is equal or larger than <see cref="min"/>. If <see cref="min"/> is too large, return -1.
59 /// </returns>
60 public static int GetPrime( int min )
61 {
62 if( min <= 2 )
63 return 2;
64
65 if( Primes[ Primes.Length - 1 ] < min )
66 {
67 for( var i = min | 1 ; i < 0x7FFFFFFF ; i += 2 )
68 {
69 if( IsPrime( i ) )
70 return i;
71 }
72
73 return -1;
74 }
75
76 for( var i = Primes.Length - 2 ; i >= 0 ; i-- )
77 {
78 if( min == Primes[ i ] )
79 return min;
80
81 if( min > Primes[ i ] )
82 return Primes[ i + 1 ];
83 }
84
85 return 2;
86 }
87
88 /// <summary>
89 /// Just basic Sieve of Eratosthenes prime number test.
90 /// </summary>
91 /// <param name="candinate">
92 /// Number that is tested.
93 /// </param>
94 /// <returns>
95 /// true, if <see cref="candinate"/> is prime number; otherwise false.
96 /// </returns>
97 public static bool IsPrime( int candinate )
98 {
99 if( (candinate & 1) == 0 )
100
101 // Even number - only prime if 2
102 return candinate == 2;
103
104 var upperBound = (int) Math.Sqrt( candinate );
105 for( var i = 3 ; i < upperBound ; i += 2 )
106 {
107 if( candinate % i == 0 )
108 return false;
109 }
110
111 return true;
112 }
113 }
114}