C # code Analysis (third week)

Source: Internet
Author: User

To read the following procedure, please answer the following questions:

Question 1: What are the criteria for this program?

Question 2: Does such a number exist? What is the minimum number that meets this condition?

Question 3: How long do you expect to be able to output the first result when running this program on a computer? The time is accurate to minutes (computer: Single core CPU 4.0G Hz, memory and hard disk resources are sufficient).

Question 4: How can I improve the efficiency of this program on multi-core computers?

1 usingSystem;2 3 usingSystem.Collections.Generic;4 5 usingSystem.Text;6 7 namespaceFindthenumber8 9 {Ten     class Program One { A       Static voidMain (string[] args) - { -         int[] RG = the{2,3,4,5,6,7,8,9,Ten, One, A, -, -, the, -, -, -, +, -               -, +, A, at, -, -, -, -, -, in, -, to}; -          for(Int64 i =1; i < Int64.maxvalue; i++) - { +           intHit =0; -           intHit1 =-1; +           intHit2 =-1; A            for(intj =0; (J < RG. Length) && (hit <=2) ; J + +) at { -             if((i% rg[j])! =0) - { -hit++; -               if(hit = =1) - { inHit1 =J; - } to               Else if(hit = =2) + { -Hit2 =J; the } *               Else $                  Break;Panax Notoginseng } -  the } +           if(hit = =2) && (hit1+1==hit2)) A { theConsole.WriteLine ("found {0}", i); + } - } $ } $ } -}
Code Analysis

Every time I analyze the code, I basically write down the program's execution, and of course, it's a good idea to perform one-step debugging on the program. Slowly discover the execution rules of the program, judge the function of the code to complete. In this program, Int64 is a signed 64-bit integer data type that represents an integer value between 263 and 263. This is a very large number, so we are not able to execute the program manually, and we can still simulate the process of execution of the previous procedures to determine the program execution law.

I=1

j = 0 Hit=1 hit1=0 hit2=-1
j = 1 hit=2 hit1=0 hit2=1
j = 2 Hit=3 hit1=0 hit2=1 break
Compare hit!=2

i=2

j = 0 Hit=0 hit1=-1 hit2=-1
j = 1 Hit=1 hit1=1 hit2=-1
j = 2 hit=2 hit1=1 hit2=2
j = 3 Hit=3 hit1=1 hit2=2 break
Compare hit!=2

I=3

j = 0 Hit=1 hit1=0 hit2=-1
j = 1 Hit=1 hit1=0 hit2=-1
j = 2 hit=2 hit1=0 hit2=2
j = 3 Hit=3 hit1=0 hit2=2 break
Compare hit!=2

I=4

j = 0 Hit=0 hit1=-1 hit2=-1
j = 1 Hit=1 hit1=1 hit2=-1
j = 2 Hit=1 hit1=1 hit2=-1
j = 3 hit=2 hit1=1 hit2=3
j = 4 Hit=3 hit1=1 hit2=3 break
Compare hit!=2

We can continue to run, but to this, we are not difficult to find out for program output conditions (hit = = 2) && (HIT1+1==HIT2) in the hit1+1==hit is to be in the RG The adjacent two numbers are found in the array, and hit = = 2 requires that the two numbers be divisible by I at the same time. 2 ~ 31 is stored in RG . So the ultimate goal of the program is to find out that a number i,i cannot be divisible by the two numbers adjacent to the 2~31, but can be divisible by the remaining 28 numbers.

Question 1: What are the criteria for this program?

Find out that a number i,i cannot be divisible by the two numbers adjacent to the 2~31, but can be divisible by the remaining 28 numbers.

Question 2: Does such a number exist? What is the minimum number that meets this condition?

Mathematics is not good, rely on Baidu, I found this number: 23*33*52*7*11*13*19*23*29*31=2123581660200, in fact, is 16,17 and other 28 number of least common multiple.

Question 3: How long do you expect to be able to output the first result when running this program on a computer? The time is accurate to minutes (computer: Single core CPU 4.0G Hz, memory and hard disk resources are sufficient).

I can't figure it out.

Question 4: How can I improve the efficiency of this program on multi-core computers?

Since it is a multi-core computer, it should be possible to use multi-threaded way to execute the program in parallel, improve the efficiency of the operation.

C # code Analysis (third week)

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.