Question: Please write the code to print the prime number within 10000 for efficiency!
To be honest, such simple pen-level questions are rare in the past, but I am lucky to have met them today! Although I am not happy at all, because I know that the test is good and the interview is difficult! I don't know if it is a back-to-back relationship. Every interview always feels good, but there is always no response afterwards. I can find the reason from myself. For example, if you have project experience, but have no experience in large-scale project development, you may not have much experience in the project.
There are also industry reasons, such as entering a game company, having no experience in Game Development (I am talking about large ones); for example, entering a cad Development Company, apart from knowing a lisp, what's the most hateful is that the interviewer asked me to write the DP equation for a search question. After thinking about N for a long time, I finally had no choice but to say: You can also use search and pruning. He said: The Search complexity is too high, or DP is better. I said, yes! And then look at each other. After half a day, he may suddenly think that it was not a pair of eyes. Sorry, he said to me: Go back and wait for the message! I checked the question and found that it is still classified into the NP problem! I hate that the update speed of university textbooks cannot keep up with the development of the enterprise. I'm even more excited. How nice would it be if I had more than 200 smart transactions! However, I should go to the Chinese Emy of Sciences with an IQ of 200! Haha!
The interview is a mess. In fact, the interviewers in many companies are still pretty good and don't make you difficult. You just need to show everything you will to him. If he thinks it is appropriate, he should be hired. Unfortunately, few interviewers have considered me appropriate. The IT industry spans too much, from medical care to funeral. I think that if the computer is simple enough to allow doctors to develop medical software in their free time, they will certainly be able to fully understand the needs. The rest of the computer science classes only need to study how to make programming easier. Unfortunately, even I think this idea is absurd and not practical.
I accidentally ran the question! Success!
There is an algorithm for determining whether a number is a prime number. The complexity is the root power of N. Using this algorithm to implement the above question is really natural, but it is also inefficient. Note that 10000 is not very large, and it seems that it is not unacceptable to open an array to store which are prime numbers, that is, 10 K. However, early programmers must feel bad,, the computer memory, just like the RMB, has suffered a severe devaluation. Today's programmers do not hesitate to open a 10 m continuous space!
Now the array is ready, just waiting to determine which are prime numbers and which are not. The Code is as follows:
#define N 10005bool isprimer[N];void get_primer(){ int i = 0, j = 0; for (i = 0; i < N; ++i) isprimer[i] = true; isprimer[0] = false; isprimer[1] = false; for (i = 2; i < N; ++i) if (isprimer[i]) for (j = 2; i * j < N; ++j) isprimer[i * j] = false;}
Note that the first layer of the get_primer function adds a prime number judgment in the for loop. It does not matter if you do not add this judgment, but it will perform a lot of unnecessary calculations. You can't draw a line. Do you mean no? Why is the same result as that obtained without a judgment? Because a sum of numbers can always be divided into the product of a prime number and another integer, it is self-evident what this means!
The main program will not be written!