opensim-development – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 eva 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 */
27  
28 using System;
29  
30 namespace OpenSim.Framework
31 {
32 /// <summary>
33 /// Utility class that is used to find small prime numbers and test is number prime number.
34 /// </summary>
35 public static class PrimeNumberHelper
36 {
37 /// <summary>
38 /// Precalculated prime numbers.
39 /// </summary>
40 private static readonly int[] Primes = new int[]
41 {
42 3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239,
43 293, 353, 431, 521, 631, 761, 919, 1103, 1327, 1597, 1931, 2333,
44 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
45 17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431,
46 90523, 108631, 130363, 156437, 187751, 225307, 270371, 324449,
47 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
48 1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559,
49 5999471, 7199369
50 };
51  
52 /// <summary>
53 /// Get prime number that is equal or larger than <see cref="min"/>.
54 /// </summary>
55 /// <param name="min">
56 /// Minimal returned prime number.
57 /// </param>
58 /// <returns>
59 /// Primer number that is equal or larger than <see cref="min"/>. If <see cref="min"/> is too large, return -1.
60 /// </returns>
61 public static int GetPrime(int min)
62 {
63 if (min <= 2)
64 return 2;
65  
66 if (Primes[ Primes.Length - 1 ] < min)
67 {
68 for (int i = min | 1 ; i < 0x7FFFFFFF ; i += 2)
69 {
70 if (IsPrime(i))
71 return i;
72 }
73  
74 return -1;
75 }
76  
77 for (int i = Primes.Length - 2 ; i >= 0 ; i--)
78 {
79 if (min == Primes[ i ])
80 return min;
81  
82 if (min > Primes[ i ])
83 return Primes[ i + 1 ];
84 }
85  
86 return 2;
87 }
88  
89 /// <summary>
90 /// Just basic Sieve of Eratosthenes prime number test.
91 /// </summary>
92 /// <param name="candinate">
93 /// Number that is tested.
94 /// </param>
95 /// <returns>
96 /// true, if <see cref="candinate"/> is prime number; otherwise false.
97 /// </returns>
98 public static bool IsPrime(int candinate)
99 {
100 if ((candinate & 1) == 0)
101  
102 // Even number - only prime if 2
103 return candinate == 2;
104  
105 int upperBound = (int) Math.Sqrt(candinate);
106 for (int i = 3 ; i < upperBound ; i += 2)
107 {
108 if (candinate % i == 0)
109 return false;
110 }
111  
112 return true;
113 }
114 }
115 }