Jypeli 10
The simple game programming library
PrimeNumberGenerator.cs
Siirry tämän tiedoston dokumentaatioon.
1#region MIT License
2/*
3 * Copyright (c) 2005-2008 Jonathan Mark Porter. http://physics2d.googlepages.com/
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights to
8 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9 * the Software, and to permit persons to whom the Software is furnished to do so,
10 * subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
16 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
17 * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22#endregion
23
24
25
26#if UseDouble
27using Scalar = System.Double;
28#else
29using Scalar = System.Single;
30#endif
31using System;
32using System.Collections.Generic;
33
34namespace AdvanceMath
35{
36
40 public sealed class PrimeNumberGenerator
41 {
42 int[] primes;
45 {
46 if (maxNumber < 2)
47 {
48 throw new ArgumentOutOfRangeException("maxNumber");
49 }
50 List<int> primes = new List<int>(Math.Max((int)Math.Sqrt(maxNumber), 10));
51 this.maxNumber = maxNumber;
52 for (int pos = 2; pos <= maxNumber; ++pos)
53 {
54 if (IsPrimeInternal(pos))
55 {
56 primes.Add(pos);
57 }
58 }
59 this.primes = primes.ToArray();
60 }
61 public int[] Primes
62 {
63 get { return primes; }
64 }
65 public int MaxNumber
66 {
67 get { return maxNumber; }
68 }
69 private bool IsPrimeInternal(int number)
70 {
71 int length = primes.Length;
72 for (int pos = 0; pos < length; ++pos)
73 {
74 if (number % primes[pos] == 0)
75 {
76 return false;
77 }
78 }
79 return true;
80 }
81 public bool IsPrime(int number)
82 {
83 if (number > maxNumber || number < 1)
84 {
85 throw new ArgumentOutOfRangeException("number");
86 }
87 return IsPrimeInternal(number);
88 }
89 }
90}
System.Single Scalar
Definition: Clamped.cs:29
Generates prime numbers. Just felt like writting one.