Programming beauty-2.20 (Program understanding and Time Analysis)

Source: Internet
Author: User
This is the 2.20 question in the beauty of programming. It provides a C # code that requires no computer, understanding the program, and answering questions. The following is the c ++ code rewritten from the C # code:
#include <iostream>#include <limits>using namespace std;int main() {     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 < numeric_limits<__int64>::max(); i++) {         int hit = 0;         int hit1 = -1;         int hit2 = -1;         for(int j = 0; j < sizeof(rg)/sizeof(*rg) && (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) {              cout << "find " << i << endl;              break;         }     }     return 0;}
Reference: http://blog.csdn.net/jcwKyl/article/details/3889802
1. What conditions are met before I is output? First check the condition hit = 2, which indicates: (I % RG [J])! = 0) It must be valid only twice. Then look at the conditions Hit1 + 1 = hit2,This condition can be used to obtain the condition (I % RG [J])! = 0). J1 and J2 must be adjacent. Based on the above analysis, we can conclude that the output result of the program I should meet the following requirements: I cannot be divisible by two adjacent numbers in the 30 numbers from 2 to 31, but can be divisible by the remaining 28 numbers. That is to say, I must be an integer multiple of the smallest public multiples of the other 28 numbers.
How can we find the two numbers that meet the preceding conditions? In simple consideration, prime numbers meet the requirements, because Prime numbers cannot be divisible by the multiplication machines of other natural numbers. Is there any other number that meets the requirements?
I cannot be divisible by two adjacent numbers. Therefore, it must be a matter factor after I is decomposed, or the quality factor of I does not include these two numbers, or the number of times the I prime factor is less than the number of times the two numbers share the same prime factor. For example, assume that 8 is one of the two numbers that meet the condition (that is, 8 cannot be divisible by the product of the other number). Obviously, we can find an inverse example -- 16, because a factor of 16i, because 16 can be divisible by 8, I must be divisible by 8. If this is not true, 8 is not the number we want. But 16 is acceptable. Think about why?
Then, we only need to break down the prime factor for the 30 numbers from 2 to 31 to find out whether there are such two adjacent numbers, or their quality factors have quality factors not available for other numbers, either for the same prime factor, these two numbers contain this prime factor more times than all others (note that only the number of times the prime factor is higher than the number of other RG numbers, instead of the sum of the number of all other times of this factor, the reason is that the minimum public factor is needed ). Create a table as follows:



As shown in the preceding table, only the numbers 16, 17, 19, 23, 25, 27, 29, and 31 contain the most frequently used quality factors. The adjacent values are only 16 and 17. Therefore, the number I required by this program is that it cannot be divisible by 16 or 17, but it can be divisible by the other 28 in 30. The smallest I is the least common multiple of the other 28, as shown in the preceding table, the smallest I is: 8 (3 to the power of 2) * 27 (3 to the power of 3) * 25 (2 to the power of 5) * 7*11*13*19*23*29*31. Use a calculator to calculate the value 2123581660200. We can initialize the I in the for loop in the above program into this number to test.
Reference: http://hi.baidu.com/silverxinger/item/4fd323b7c4803a98194697a5
Extended reading 1: How is the minimum public multiple obtained? A prime number is a number that cannot be divisible by one and other numbers. A prime number X can only be divisible by the power below the N-1 of X, and 1 and its own number.
Therefore, in the request of A, B, C, D, E ,..., For the minimum public multiples of Z, you only need to divide these numbers into the product between the nth power of the prime number, and take the product of the highest power of each prime factor, that is, the minimum public multiple of these numbers.
Example:
What are the minimum public multiples of, and?
Because 756 = 2*2*3*3*7, 4400 = 2*2*2*2*5*5*11, 19845 = 3*3*3*3*5*7*7,9000 = 2*2*2*3*3*5*5*5, here there are prime numbers 2, 3, 5, 7, 11.2 up to 4 power 16, 3 up to 4 Power 8, 5 up to 3 power 12, 7 up to 2 power 49, and prime 11. the minimum public multiple is 16*81*125*49*11 = 87318000. reference: http://baike.baidu.com/view/341375.htm
Extended reading 2: joj's 2042 question is also a program understanding question. This question is very interesting. It provides the following C ++ source code and requires the final output result to 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
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.

Reference: http://blog.csdn.net/jcwKyl/article/details/3889802

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.