[Beauty of programming] 2.20 program understanding and Time Analysis

Source: Internet
Author: User

From: http://blog.csdn.net/zhuhuiby/article/details/6742980

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

 
using System;using System.Collections.Generic;using System.Text; namespace FindTheNumber{     class Program     {          static void Main(string[] args)          {               int [] rg =               {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};                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)

----------------------------------------------------

When I did it myself, I thought it was the minimum public factor of 2-29. I didn't consider that 30 can be divided into 2*3*5.

---------------------------------------------------------------------------

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.

 

 

Joj's 2042 question is also a program understanding question. This question is very interesting. The following section of C ++ source code is provided, and the final output result must be calculated. The source code is as follows:

#include<cstdio>   int main(void)  {       int x = 987654321, c = 0, d = 1, e = 6;       while(x--){           c += d,           d += e,           e += 6;       }       printf("%d/n", c);       return 0;  }


 

The original question is as follows:

We can use axioms to calculate programs just like what we do in algebras. dijkstra is the one who advocates such constructive approach whereby a program is designed together with its correctness proof. in short, one has to start from a given postcondition Q and then look for a program that establishes Q from the precondition. often, analyzing Q provides interesting hints to finding the program. this approach is quite different from the well known Hoare logic.

 

For example, the following program is calculated by Dijkstra's approach. unfortunately, its annotation is lost so that its function is hard to grasp. you are to help with finding the final value of the variable C. note that the program is designed under 128-bit architecture.

The Code is the above section.

We can see the rule through small data computing: x = 1, C = 1; X = 2, c = 8; X = 3, C = 27; X = 4, C = 64, so we can guess that this program is used to calculate x ^ 3. Use a calculator to calculate 987654321 ^ 3, and submit the code to the AC.

This question is from siyee, a super bull. I learned a lot from the description of the question. We know that a cube of numbers can be calculated in this way. Unfortunately, it's a pity that I have poor math skills and I don't know how to deduce it in mathematics.

Bytes -----------------------------------------------------------------------------------------------

I introduced the formula myself:

Initial conditions:

      

It is easy to conclude that:

      

Then we can conclude that:

      

Finally, it is concluded that:

      

 

[Beauty of programming] 2.20 program understanding and Time Analysis

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.