Get Prime, Is prime : Math « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Design Patterns
8.Development Class
9.Event
10.File Stream
11.Generics
12.GUI Windows Form
13.Language Basics
14.LINQ
15.Network
16.Office
17.Reflection
18.Regular Expressions
19.Security
20.Services Event
21.Thread
22.Web Services
23.Windows
24.Windows Presentation Foundation
25.XML
26.XML LINQ
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » Development Class » MathScreenshots 
Get Prime, Is prime
 
//GNU Library General Public License (LGPL)
//http://dac.codeplex.com/license
using System;
using System.Runtime.ConstrainedExecution;

namespace RaisingStudio.Collections.Generic
{
    internal static class HashHelpers
    {
        internal static readonly int[] primes = new int[] { 
            37110x110x170x1d0x250x2f0x3b0x470x590x6b0x830xa30xc50xef
            0x1250x1610x1af0x2090x2770x2f90x3970x44f0x52f0x63d0x78b0x91d0xaf10xd2b0xfd10x12fd
            0x16cf0x1b650x20e30x27770x2f6f0x38ff0x446f0x521f0x628d0x76550x8e010xaa6b0xcc890xf5830x126a70x1619b
            0x1a8570x1fd3b0x263150x2dd670x3701b0x420230x4f3610x5f0ed0x721250x88e310xa443b0xc51eb0xec8c10x11bdbf0x154a3f0x198c4f
            0x1ea8670x24ca190x2c25c10x34fa1b0x3f928f0x4c49870x5b8b6f0x6dda89
         };

        internal static int GetMinPrime()
        {
            return primes[0];
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        internal static int GetPrime(int min)
        {
            for (int i = 0; i < primes.Length; i++)
            {
                int num2 = primes[i];
                if (num2 >= min)
                {
                    return num2;
                }
            }
            for (int j = min | 1; j < 0x7fffffff; j += 2)
            {
                if (IsPrime(j))
                {
                    return j;
                }
            }
            return min;
        }

        [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)]
        internal static bool IsPrime(int candidate)
        {
            if ((candidate & 1== 0)
            {
                return (candidate == 2);
            }
            int num = (int)Math.Sqrt((double)candidate);
            for (int i = 3; i <= num; i += 2)
            {
                if ((candidate % i== 0)
                {
                    return false;
                }
            }
            return true;
        }
    }
}

   
  
Related examples in the same category
1.Demonstrate Math.Sin(), Math.Cos(), and Math.Tan(). Demonstrate Math.Sin(), Math.Cos(), and Math.Tan().
2.Find the radius of a circle given its area. Find the radius of a circle given its area.
3.Math operators
4.Use Math.Round
5.Math.Sign
6.Math.Abs
7.Math.Min
8.Converts a degrees angle into a radians angle.
9.Converts a radians angle into a degrees angle.
10.Converts degrees to radians.
11.Gets the mean value from a list
12.Gets the median from the list
13.Gets the standard deviation
14.Statistical functions: Mean
15.Statistical functions: Standard Deviation
16.Statistical functions: Standard Deviation
17.The Method converts the temperature in Fahrenheit to Celcius
18.The Method converts the temperature in Celcius to Fahrenheit
19.Returns the maximum value of three numbers
20.Returns the minimum value of three numbers
21.rounds val to the nearest fractional value
22.Combines two input numbers in some proportion.
23.Normalise Angle
24.wraps the mod result to avoid negative results.
25.Method that calculates the Greatest Common Divisor (GCD) of two positive integer numbers.
26.Calculates the floor of the log, base 2, of 'x'.
27.Calculates the Least Common Multiple (LCM) of two strictly positive integer numbers.
28.Same as System.Math.Acos but if angle is out of range, the result is clamped.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.