The first article in this series:
Http://www.cnblogs.com/dhf327/p/4773672.html
100 lamp problem, the last time we were basically solved, regardless of whether the algorithm is optimized enough, at least we have been in an acceptable time to get the answer.
But in the end, I left a question for you.
One, the last question
The previous article gives a method for Python to find all the factors of a positive integer, which is working normally and does not have any problem with the speed of all factors of a single positive integer.
But combined with the last topic, the heart of the students suddenly see the problem, 1 to 10 of the 12, assuming that each number of all factors need 1 milliseconds, the total cycle of how long? (Considering that the later figures are large, the early numbers are smaller, this 1 milliseconds is still a relatively optimistic time)
Python can quickly calculate:
1 Print (1000000000000/1000/60/60/24/365)
The result is >31.7 this number of units should be years , my God, not to tease me to play. This time Ren who also can not accept, this QQ group we do not add?
Obviously, even if we use parallel algorithms, we still don't get the right results in an acceptable time. Maybe it can be treated with a cluster, and it's like a big puffing out mosquito.
Algorithm optimization is required at this time.
Second, we talk about algorithm optimization
When the normal way can not be solved, we need a new idea to solve this problem, because this is basically a purely mathematical problem, and my digital Foundation, then please out of the Almighty search engine bar, found two related theorems can be used.
Approximate number theorems, approximate numbers and theorems. Here is the approximate factor. The explanation of Baidu Encyclopedia is as follows:
For a greater than 1 positive integer n can be decomposed factorization: N=p1^a1*p2^a2*p3^a3*...*pk^ak,
Then the approximate number theorem of n is known to have (a?+1) (a?+1) (a?+1) ... (ak+1) One,
then N (a?+1) (a?+1) (a?+1) ... (ak+1) A positive approximate and a
F (n) = (p1^0+p1^1+p1^2+...p1^a1) * (P2^0+P2^1+P2^2+...P2^A2) *...* (Pk^0+pk^1+pk^2+...pk^ak)
such as 10=2^1*5^1; Then the number of factors of 10 is (+) * (+) = 4, Factor and is (2^0+2^1) + (5^0+5^1) =18
The contents of the above theorem are not mentioned, the general P1<p2<p3 ...
Prove that you can find the information on their own, in short, this content we can use.
If we use this theorem, it seems to solve our problem, let's try.
Three, decomposition factorization
Each number to decompose factorization, that is, the factor of prime numbers, forget the definition of prime numbers of students please own Baidu,
Assuming we have sequential sequence of prime numbers starting from 2, we can quickly calculate the result of a number decomposition of prime numbers.
m=[2,3,5,7,11,13,17,19,...]
Assuming that the decomposition of n factorization, then it is starting from 2, if divisible, then divide the value after dividing by 2, if there is no time, except one of the following value 3, and so on, until the decomposition is complete.
This is known as a short-circuit algorithm.
Let's compare the algorithm of all the factors in the previous article, we can really find all the factors of a positive integer, and now using this formula, we can do not have to find all the factors, we can directly to the sum of all the factors, seems to be a bit faster.
In fact, the algorithm in the previous article does not depend on anything else, only the computational quantity comparison, although only from 2 to N square root, but this one of each number to participate in the operation, the calculation is very large.
At this point the new short-circuit algorithm looks faster, but it is important that he needs a sequence of prime numbers!
The problem seems to be getting complicated.
Not to be continued)
Thought of by 100 lamps (ii)