(20140707 homework) As soon as you enter it, it is like a sea. From now on, it is a passer-by !~ 9*9 multiplication table & prime number within a thousand

Source: Internet
Author: User

This is the first day of training for our boss. In the future, almost all assignments will be recorded here.

2014-07-07

Requirements:

C # basic syntax to implement the 9-9 multiplication table and all prime numbers within 1000

 

A few new colleagues, who have just graduated from college, have an electromechanical background, and have hardware.

You may have different ideas, so you can brainstorm. For good ideas, I will learn from and retain them here.

 

Code of the bigbench:

1 // calculate the prime number less than 2 using system; 3 using system. collections. generic; 4 using system. LINQ; 5 using system. text; 6 using system. threading. tasks; 7 8 namespace Sushu 9 {10 class program11 {12 static void main (string [] ARGs) 13 {14 datetime d1 = system. datetime. now; // The current running time 15 console. writeline ("Start Time: {0}", D1); 16 console. writeline (); 17 18 int n = 10000; // defined range: 19 int m = 0; 20 bool T = false; // defines the existence variable of the factor, initially fa Lse21 int COUNT = 0; // count the number of variables 22 23 for (INT I = 1; I <= N; I ++) 24 {25 if (I = 2 | I = 3) // process 2 326 {27 COUNT = count + 1; 28 console. write ("{0}", I); 29} 30 31 if (I % 2! = 0) // exclude 32 {33 34 for (int A = 2; A <= I/2; A ++) // process a factor other than 1 (if there is a factor, there will inevitably be a factor smaller than n/2) 35 {36 T = false; 37 m = I %; 38 If (M = 0) 39 break; // The entire division exit loop 40 else41 T = true; // Key Identifier 42} 43 44 If (t = true) // judge the prime number, and then output the statistical quantity 45 {46 COUNT = count + 1; 47 console. write ("{0}", I); 48 if (count % 12 = 0) // output number typographical 12 columns 49 {50 console. writeline (); 51} 52} 53 54} 55 56} 57 console. writeline (); 58 console. writeline ("the total number of prime numbers within {0} is: {1}", N, count); // output prime number statistics 59 console. writeline (); 60 timespan Ts = datetime. now-D1; // obtain the current time and calculate the time consumption 61 console. writeline ("Time consumed: {0} seconds, {1} milliseconds", ts. seconds, ts. milliseconds); // output time-consuming data 62 63 64 console. readkey (); 65 66} 67} 68}
View code

 

Comment from the boss: (Here we only record what I understood at the time. The original words are not clearly remembered)

Its advantage is to minimize the number of cycles and eliminate certain non-prime numbers.

 

Xiao Yong's code:

1 static void main (string [] ARGs) 2 3 {4 5 Int I = 0, j = 0; 6 for (I = 3; I <= 10000; I = I + 2) // I + 2 remove the elements, reduce the calculation workload by 7 {8 // re-reduce the calculation workload, and combine element decomposition, the minimum factor must be 9 int K = (INT) math. SQRT (I); 10 for (j = 2; j <= K; j ++) 11 {12 // judge the prime number 13 if (I % J) = 0) 14 {15 break; 16} 17} 18 // If j <K, it indicates that no judgment is complete. Send it back and Judge 19 if (j> K) 20 {21 console. write (I. tostring () + ""); 22} 23} 24 console. read (); 25 26}
View code

 

Here we use a mathematical method (kaifang ):

1 int K = (INT) math. SQRT (I );

 

After asking the boss about the efficiency of running the function, the result is that the function uses a very good algorithm (not specifically remembered) in ancient China, but the speed is the slowest.

 

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.