In-depth study of the final algorithm for calculating N continuous numbers

zhaozj2021-02-16  127

I heard a legendary microsoft test question today, the sum of N the number of the heads of the head, and the time is minimized. Compared to all programmers, the first condition is still a reasonable and legitimate request, but the second condition seems to be very challenging. After thinking, I decided to start a "final algorithm" to explore! ! !

The initial idea is: Create an Array of N, then preset a few high numbers:

Public Static Int GetPriMERSTAl (int count, out int [] priMERS) {

Primers = new int [count];

Primers [0] = 2; Primers [1] = 3; Primers [2] = 5; priMERS [3] = 7;

Feeling that you are very shameless, let's put so much, huh, in fact, this is not a key to the key. When you test, it is a million-level data. Then I will write an algorithm to realize the current query. This seems to be a difficult thing to do anything, I will not waste your strength.

INT TOTAL = 17; int index = 3; int next = 9;

Bool isprimer;

Int current = environment.tickcount;

While (Index

For (int i = 1; i = priMERS [i]) // is only compared to the current number of small numbers {ion (next%) PRIMERS [I] == 0) {isprimer = false; Break;}} else break;

IF (isprimer) {primers [( index)] = next; Total = next;}}

Console.writeline ("IT WAS {0} MS Has Pasted.", Environment.TickCount - Current);

Return Total;

}

OK, the first version is written, huh, the test results are not bad, when the input parameter N is 10,000 (one million), the time to get the entire number is: 13438ms. (By the way: I use Dell's original business machine, 1G memory, Intel 2.4c CPU) cheers after the jump, start thinking about optimization: At first, this program has nothing to optimize, (Of course I also You can use ASM to write this thing. This is still another saying, now just consider using Csharp 2005 Express), "Open Range Operation", this operation is exposed to my eyes, if you start from the perspective of the machine code, Open root operation needs ..... (sweating, there seems to have never learned how many instructions need to be used to open the root number ^ _ ^ b), huh, but I think of an optimization method from here: 101 open roots The results of the 103 open root number If only the integer part is exactly the same, if I customize the operation of the open root number, use the static variable to record some results, this is not a "good person good thing", but, Thunder, don't think about it,

Public Static Int SQRT (INT I) {IF (i

}

I redefined the SQRT algorithm, so that only returns an integer, but the result makes me very disappointed, only 200ms is about 200ms ---- OK, quiet, drinking a glass of water, after dinner, new ideas Out: I can record their square results in an int64 array parallel PRIMERS, and then use the basis of the comparison, which is not necessary to go to the root number. Haha, the code is as follows:

Public Static int GETPRIMERSTOAL (int count, out int rt [] priMERS) {primers = new int [count];

INT64 [] Reference = New INT64 [count];

PriMERS [0] = 2; Reference [0] = 4;

PriMERS [1] = 3; Reference [1] = 9;

PriMERS [2] = 5; Reference [2] = 25;

Primers [3] = 7; Reference [3] = 49;

INT TOTAL = 17; int index = 3; int next = 9;

Bool isprimer;

Int current = environment.tickcount;

While (Index

For (INT i = 1; i Reference [i]) {if (Next% Primers [i] == 0) {isprimer = false; break;}} else if (next = = Reference [I]) {isprimer = false; break;} else break;} if (isprimer) {primers [( index)] = next; reason [index] = (long) Math.Pow (next, 2) ; Total = next;}}

Console.writeline ("IT WAS {0} MS Has Pasted.", Environment.TickCount - Current);

Return Total;

After testing, too cool, 1,000,000 (one million) data input, actually successful minus: 8359ms, yeah! ! ! Invincible. Oh, unless you use assembly, otherwise, this should be the optimal computing method in the .Net 2.0 environment. (Or you want to do a quarter of the article, oh, let me think about it, this is not good to say)

:) In short, I also put it on the road to myself on the road to pursue algorithms.

Enjoy life, enjoy code.

转载请注明原文地址:https://www.9cbs.com/read-14393.html

New Post(0)