The beauty of programming 2.20 program understanding and Time Analysis

Source: Internet
Author: User

I recently read the beauty of programming to prepare for job interviews. In this book, I did not answer the question of 2.20 program understanding and time analysis, so I wrote my own answer briefly.

The questions are as follows:
Read the following C # code to answer questions:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
Using system;
Using system. Collections. Generic;
Using system. text;

Namespace findthenumber
{
Class Program
{
Static void main (string [] ARGs)
{
Int [] Rg =
,
18, 19, 20, 21, 24, 25, 26, 27, 28, 29, 30, 31 };

For (int64 I = 1; I <int64.maxvalue; I ++)
{
Int hit = 0;
Int hit1 =-1;
Int hit2 =-1;
For (Int J = 0; (j <RG. Length) & (hit <= 2); j ++)
{
If (I % RG [J])! = 0)
{
Hit ++;
If (hit = 1)
{
Hit1 = J;
}
Else if (hit = 2)
{
Hit2 = J;
}
Else
Break;

}
}

If (hit = 2) & (hit1 + 1 = hit2 ))
{
Console. writeline ("found {0}", I );
}

}
}
}
}

1> what conditions does this program look?
2> does this number exist? What is the minimum number that meets this condition?
3> how long will it take for you to run this program on your computer to output the first result? Accurate Time to minutes (Computer Configuration: Single Core CPU GHz, sufficient memory and hard disk resources)

My answer:

 

  • 1>The first problem is not difficult. if you carefully analyze the program, you can see that the program is looking for such a number, this number cannot be divisible by RG [k] and RG [k + 1] (0 <= k <n-1), and can be divided by all other numbers (that is, RG [0],…, RG [k-1] and R [K + 2],..., R [n-1]) division.
  • 2>Start with RG [k] and RG [k + 1].

First, RG [k] must be greater than 15. If RG [k] <= 15, then RG [k] * 2 is also in the RG array and cannot be fully divided by I. Therefore, such I cannot be found.

Secondly, RG [k] and RG [k + 1] cannot be multiplied by the number combination in the remaining RG arrays. For example, 18 can be obtained by multiplying 2 by 9, therefore, if I can divide 2 and 9, it must be able to divide 18. as a result, we can get:

16 = 2*8, 18 = 2*9, 20 = 4*5, 22 = 2*11, 24 = 3*8, 26 = 2*13, 28 = 4*7, 30 = 2*15.

At first glance, it seems that RG [k] and RG [k + 1] do not meet the conditions, but in the above equations, 16 = 2*8, 2 is the factor of 8, so as long as I can divide 8, it will be able to divide 2, so there is no need to require I to be able to divide 2*8. In the rest of the equations, there is no relationship between the two multiplier. Therefore, if I can divide the two multiplier, it will certainly be able to divide the product.

Therefore, we obtain the unique RG [k] and RG [k + 1] that meet the conditions, that is, 16,17.

In this way, the remaining problem is to find the smallest integer in all other numbers that cannot divide 16, 17. First, we will list the prime numbers from 2 to 31 (except 17): {2, 3, 5, 7, 11, 13, 19,23, 29,31 }. The numbers in 2 to 31 (except 16 and 17) are obtained by multiplying these prime numbers as a factor combination. To get 8, at least 3 2, to get 27, we need at least three, 25, and at least two five, and only one other prime factor is required.

Therefore, the minimum number is the product of 2 ^ 3, 3 ^ 3, 5 ^ 2, 7, 11, 13, 19, 23, 29, 31. The answer is 2123581660200. (Because the question requires no use of the computer, I have to calculate it twice, and I have to calculate it with a calculator ).

  • 3>To estimate the time, first determine an atomic operation (or the atomic process is more appropriate). Here we take the entire if statement block in the inner for loop, this program mainly includes a modulo operation and a judgment. If you enter the if statement, it also includes an addition operation, 1 ~ Two judgments and one value assignment operation.

We know that addition, judgment, and other operations can be completed within several clock cycles, while Division operations require dozens of clock cycles, the modulo operation is also obtained through the division operation (remember that in the assembly language, after the division operation is executed, the result is saved in one register and the remainder in the other register). In addition, the division of 64-bit integers is obviously slower than that of 32-bit integers. Based on these factors, we can assume that the atomic operation requires 100 clock cycles. Therefore, a 2 GHz CPU can run 2*10 ^ 9/100 = 2*10 ^ 7, that is, 20 million atomic operations within 1 second. After doing ACM, you will have an intuitive concept, this is similar to the number of computations we estimate when we usually do a question with a time limit of 1 s.

Next, estimate the number of atomic operations: the outer loop runs 2123581660200 times, and the inner loop depends on I. When I is an odd number, the inner can run up to 5 times to end, because 2, 4, if I is an even number, it is more complicated, but it can be analyzed in detail one by one. Here we roughly estimate that even if the inner loop can run 10 times on average, the outer loop runs less, and the zero header is removed, the total atomic operation is performed 2*10 ^ 13 times.

Therefore, it takes 2*10 ^ 13/(2*10 ^ 7) = 10 ^ 6 seconds for about 277 hours.

Additional reading:

1. Bop Douban home page: http://book.douban.com/subject/3004255/

2. Internet purchase link: http://www.china-pub.com/38070

3. "Beauty of programming", mandatory for IT job interview "link:
Http://www.google.com.hk/search? Complete = 1 & HL = ZH-CN & newwindow = 1 & Q = beauty of programming +-+ Microsoft technical interview experiences + Yan Xin & meta = & AQ = F & OQ =

 

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.