The program code given in the topic is as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacefindthenumber{classProgram {Static voidMain (string[] args) { int[] RG ={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; for(intj =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); } } } }}
The problem is as follows:
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?
I will run this program for 2 hours without results, and then read the question, and really is to ask for hands-on estimation ...
Because I do not know how much int64.maxvalue, so I changed a small program to look at a bit, such as
Results show
So int64.maxvalue=9223372036854775807=2^63-1
My answer is:
Question 1: What are the criteria for this program?
A: First there are two for loops, it is obvious that the first for loop is to find the number, the range of this number is from 1~2^63-1, a large range, so I can now estimate that this time will be very long, and cannot run out, need to calculate manually. Second, the second for loop is to look for the condition of this number, starting with the first number of the RG array, one to the last number, if ((i% rg[j]) = 0), that is, to find a number that cannot be divisible by a number in the array, accumulate the number hit+1=1, and then loop the array, looking for a second An array of divisible integers, if found, hit+1=2, then determines if hit is equal to 2, if equal to 2, and the array cannot be divisible by the two numbers adjacent to I, and the remainder of the array can be divisible by I, that is, to find this number I. In short, it is looking for a number that cannot be divisible by two contiguous numbers in the array rg and can be divisible by the other numbers in the array, because the range is large, so it is not too small!
Question 2: Does such a number exist? What is the minimum number that meets this condition?
A: Because if the large number can be evenly divisible, then the number of factors, that is, many of the decimal number of the composition can be divided into, according to this theory, my idea is to start from a small number, because the most likely to divide the decimal, so can be first ruled out, and the larger the number is not easy to divide, so one of the
My method is as follows (only for my huangguan to translate personal ideas, may not be correct, do not refer to, O (∩_∩) o), the general idea is divided into two parts, a large number of parts and fractional parts:
① first divides the array into two parts, a large number and a decimal, an array of {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}, where 30 divided by 2 is 15, so 30 can be divisible, then 15 can be divisible, so according to this principle 2,3,4,5,6,7,8,9,10,11,12,13,14,15 can be excluded, because they are belong to decimals.
② at this time the remaining {16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31}, the next method is to see whether the two decimal numbers can be divisible, if it does not need a large number, if not, then the large number must exist, for example 30 by the 10 composition, 3 and 10 are fractional must be divisible, then 30 must exist, with this method, 18,20,21,22,24,25,26,28,30 can be excluded.
③ at this time {16,17,19,23,27,29,31}, because the prime number is definitely required, so the primes can be excluded, and their product is the base value.
④ at this time {16,27}, that is, two hit1, two hit2 16+1=17 and 27+1=28, or 27-1=26, because 26 and 28 must be divisible, so 27 of the adjacent number does not meet the requirements, only 16 and 17 left.
⑤ so finally found two neighbors {16,17}, because the product of the prime number is the base value, that is, the 5*7*11*13*19*23*29*31=11797675890, the calculator results.
This is the base value, which needs to be divisible by {16,17} and divisible by other numbers, so the 11797675890*4*9=424716332040 will continue to *4*9.
So the number is 424716332040.
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).
A: 4ghz=4*10^9hz, every 12 clock to complete a mechanical cycle, each for loop about 12 mechanical cycles, the outer for loop 2^63-1 times, the outer layer on average up to 16/2=8 times, so the total need
[(2^63-1) *8*12*12]/[4*10^9]=2562047788015215.5022 hours
I do not know the method of calculation is wrong.
My computer is outdated, here is my Computer configuration and I use the Cpu-z software to analyze my CPU's knowledge of my CPU is 4 threads.
Based on my CPU, I made a rough estimate: My CPU is 2.3GHz, the dual core 4 thread is fake quad core, my computer runs this program when CPU utilization
The maximum utilization rate is 43%, which is calculated at the fastest speed and requires:
{[(2^63-1) *8*12*12]/[2.3*10^9*4]}/43%=2685875780195.3488s=746076605.6098 hours
Question 4: How can I improve the efficiency of this program on multi-core computers?
A: ① as little as possible to start other applications to occupy the CPU, so that the CPU can provide more threads to run the program and speed up the calculation.
② I used my computer to do the contrast, so from the hardware to improve speed, in the multi-core case, provide multi-threading, multi-channel operation will increase the speed.
Summary: This procedure tests our ability to read the program, first understand the program, then to think about the results, the program did not understand what needs to do, completely do not know what to do next. In the later estimates, there must be a lot of mistakes, I did my best to calculate, but my own limited capacity, not to make accurate calculations, there are many shortcomings, but also hope that teachers to criticize.
Read the program to answer questions