A small program for calculating prime numbers

Source: Internet
Author: User
Tags datetime
The program is just beginning to learn C #, which is the first very small small program: calculating prime numbers. Do worse, don't laugh!

Http://true5460.muicc.com/other/calPN.zip

The following is the source code:

Using System;

Class Test
{
static void Main () {
ULONG I;
while (true) {
Console.Write ("Please enter the prime number to calculate (0 exit):");
I=convert.touint64 (Console.ReadLine ());
if (i==0) break;
DateTime T1 = DateTime.Now;
switch (i) {
Case 1:console.writeline ("1 is not prime!") ");
Case 2:console.writeline ("2 is prime!") ");
Default:cal (i);
}
DateTime t2 = DateTime.Now;
Console.WriteLine ("Time: {0} ms \ n", (T2-T1). TICKS/10000F);
}
}

The following are part of the function

static void Cal (ULONG x) {
ULONG Sum=1;
BYTE row=1;
Console.Write ("\ n");
for (ulong a=3;a<x+1;a++) {
BOOL Flag=true;
for (ULONG b=2;b< (A/2) +1;b++) {if (a%b!=0) continue;
Flag=false;
Break
}
if (flag) {
if (row==10) {Console.WriteLine (); row=0;}
if (sum==1) Console.Write ("{0,7}", 2);
Console.Write ("{0,7}", a);
sum++;row++;
}

}
Console.WriteLine ("\n\n{0} has a total of {1} prime numbers \ n", x,sum);

}

}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.