For the design of this algorithm, we can most easily think of traversing all the numbers from 1 to sn cyclically, and whether the total starting point of each number recycling calculation is exactly sn. The time complexity of this algorithm is about O (n * log2n). That is to say, if this calculation is performed, about 20 million cycles are required when the sn100 million times. The efficiency is naturally relatively low. Is there a more efficient way than the above method? The answer is yes.
For example, when sn = 100, the continuous positive integer series with the sum of 100 has
10018 19 20 21 229 10 11 12 13 14 15 16
For the design of this algorithm, we can most easily think of traversing all the numbers from 1 to sn cyclically, and whether the total starting point of each number recycling calculation is exactly sn. The time complexity of this algorithm is about O (n * log2n). That is to say, when the sn is 1 million, it takes about 20 million cycles. The efficiency is naturally relatively low. Is there a more efficient way than the above method? The answer is yes.
First, let's look at the formula for calculating the sum of the arithmetic difference series: Sn = n (a1 + an)/2 = na1 + n (n-1)/2
From this formula, we can easily see that when Sn and n are fixed, finding a1 is a linear function: a1 = (Sn-n (n-1)/2)/n
With this function, it is very easy to optimize this algorithm. we only need to traverse n from 1 until (Sn-n (n-1)/2) <n, we can find all the continuous sequences that meet the conditions. The Algorithm complexity is the square root of 2N, that is, when Sn = 1 million, you only need to loop 1414 times to get all the series.
Question: In 1 ~ In the 500 integers of 500, find the number of continuous addition equal to 500?
Brief analysis: int [] X = {1, 2, I ,............ 499}
Condition: I + (I + 1) + ...... + (I + k) = 500 (1 type)
Use the sum formula of the same deviation series: (k + 1) * I + (k + 1) * k/2 = 500 (2 formula)
I and k have a hidden relationship. I * k <500 (3)
Therefore, we naturally obtain the following solution:
Private static void GetSomeInt (int maxInt) {for (int I = 1; I <(maxInt-1); I ++) {for (int k = 1; k <(maxInt/I); k ++) {if (k + 1) * I + k * (k + 1)/2) = maxInt) {/******************* output result set **************** * ***/string result = "xi = "; for (int s = 0; s <(k + 1); s ++) {result + = (I + s ). toString () + ";";} result = result. trimEnd (';'); Console. writeLine (result ); /*************************************** *********/}}}}
Result:
xi=8;9;10;11;12;13;14;15;16;17;18;19;20;21;22;23;24;25;26;27;28;29;30;31;32xi=59;60;61;62;63;64;65;66xi=98;99;100;101;102
When the algorithm sn is 1 million, the number of loops is 12970034, which is nearly 10 thousand times less efficient than the previously mentioned algorithm.
The following shows the approximate algorithm code.
Static void ListSequence (int sn) {// Ignore if (sn <= 0) {return;} int n = 1 when sn is not a positive integer; // n traverses int m = sn-n * (n-1)/2 from 1; // m is Sn-n (n-1) /2 while (m> = n) // exit the loop when m <n is Sn-n (n-1)/2 <n {if (m % n = 0) // if m can be divisible by n, the sum of n consecutive positive integer sequences is sn. {Int a1 = m/n; // Obtain a1 // Print the qualified continuous series for (int I = a1; I <a1 + n; I ++) {Console. write (string. format ("{0}", I);} Console. writeLine ();} n ++; // n plus 1 m = sn-n * (n-1)/2; // Next m} Console. writeLine ("cycles: {0}", n );}
This article is available at http://www.nowamagic.net/librarys/veda/detail/4262.