Index/Frog Frog Recommendation: Demo Index indicator usage (C #)
This demo tests the number of prime numbers in a continuous natural number, except that the prime numbers are divisible by 1 and that it can be divided into one.
Sorry, as a child, math is not good, about how to determine the algorithm of prime numbers I do not understand, but this does not affect you to understand the establishment and use of index indicators, everyone who knows, trouble pointing oh
Note More, if you remove all annotations, the code is still very compact, no way, now have to do some very basic exercises.
Using System; Import namespaces
Class BitArray//define a category BitArray
{
Int[] bits; Defines an array of int
int length; Define an int variable, note: A variable with no modifiers defaults to a private variable
public BitArray (int length) {//define the constructor for this class, use the same name as the class in C # To do the constructor, vb.net the new keyword to define the
if (length<0) throw new ArgumentException (); Throw a ArgumentException exception if the length is less than 0
Bits=new int[((length-1) >>5) +1]; Assign value to bits array, and subtract length minus 1 to right shift 5 and then add 1.
This.length=length; Assign a value of length to the private variable, note the use of this keyword, which indicates the class being referenced, that is, class Bitarray,this and other uses
}
public int Length {//Set a read-only property,
get {return length;}//property returns the value of the private variable length, note the case Oh, quack.
}
public bool This[int Index] {//To build a read-write index indicator
get {//Set get indicator
if (index<0 | | index>=length) {//If the passed index value is less than 0 or greater than the array bounds, a IndexOutOfRangeException exception is thrown
throw new IndexOutOfRangeException ();
}
Return (Bits[index>>5]&1<<index)!=0; , if is the prime number returns 1, if is not the prime number returns 1, does not ask me this bit algorithm and the prime number relations Oh, I do not understand the bit and the assembly and so on the bottom thing.
}
set {//Set indicator
if (index<0 | | index>=length) {//With Get access, check that the input index is within the right range, and that it should be responsible for these potential error handling in actual programming, which enhances the robustness of the program
throw new IndexOutOfRangeException ();
}
if (value) {
BITS[INDEX>>5] |= 1<<index;
}
else {
BITS[INDEX>>5] &= ~ (1<<index);
}
}
}
}
Class Sample//Create a sample class
{
static int Count (int max) {//method for defining a static int type
BitArray flags=new BitArray (max+1); Instantiates the BitArray class and initializes it with the Max parameter.
int count=1; Define a count variable of type int and initialize to 1
for (int i=2;i<=max;i++) {///define a loop, range is from 2-max, because each number must be divisible by 1, so skip.
if (!flags[i]) {//Call the index indicator of the instance flags of the BitArray class,
for (int j=i*2;j<=max;j+=i) flags[j]=true; Don't understand
count++; Once per loop, let count plus 1,
}
}
return count; return count
}
static void Main (string[] args) {//Set the main function, note the use of the array as a parameter
int Max=int. Parse (Args[0]); Gets the first value of an array parameter
int Count=count (max); Call the Count method to handle Max,
Console.WriteLine ("from 1 to {0} is a total of {1} prime numbers", max,count); Prints the processing results to the screen.
}
}