In the case of a situation, recursive operations are required, but recursion is very large and there are more than 10,000 occurrences. Do not say 10,000 times recursion, the original test code is Java, not installed JDK and compiled environment, or with Python bar
First look at the original Java code:
public class Upcount { private long calc (int depth) { if (depth = = 0) return 1; Long cc = calc (depth-1); return cc + (depth% 7) + (((((cc ^ depth)% 4) = = 0)? 1:0); } public static void Main (string[] args) { Upcount UC = new Upcount (); System.out.println (Uc.calc (11589));} }
Java has not played much, but these lines of code to look at or not pressure, the Gordian knot to the Python code
Def calc (depth): if depth = = 0: return 1 cc = Long (calc (depth-1)) Xor_mod = (cc ^ depth)%4 if Xor_ MoD = = 0: return cc+ (depth%7) +1 else: return cc+ (depth%7) Number = Long (Calc (11589)) Print number
Code stuck, F5, wrong.
This version of the code originally did not add long, because the previous string of more than 10-bit integers can be used directly, so it is doubtful whether it is related to long
Of course, it doesn't really matter if it's long enough for a long, Python-supported integer length, as the following code is written:
Cimal = 7original = 28679718602997181072337614380936720482949array = "result=" "while original!=0: remainder = orig Inal% cimal array + = str (remainder) original/= cimallength = Len (array) for I in Xrange (0,length): result + = Array[length-1-i]print result
The above code will be a long string of decimal numbers into 7 binary notation, you can also switch to any of the binary, in order to do is 8 and 16, an Oct (), Hex () is done, with the Division method to solve it
Therefore, it can be seen that the error is not the size of the number, after all, 11589 for the current computer is only a side dish, 2^16 and 65536?
Actually came here to find, did not say the real reason for the previous recursive error, haggard
The reason for the recursive error is that the Python default recursion limit is only about 1000 times, but here to run 10000+, brush half a day: Runtimeerror:maximum recursion depth exceeded
So hurriedly checked, found that you can set the limit of recursion, see the maximum number of recursion in Python, as an extension can also view official documents
In general, in order to prevent the benefits and crashes, the Python language default to the number of restrictions, then I changed the limit is not OK?
Import Sys
# set the Maximun depth as 20000
Sys.setrecursionlimit (20000)
Insert the above code, decisively change 20000, this is not the limit should be no problem, but the result is big surprise, nothing has been lost, puzzled
Did not continue to investigate, asked the next small partner Littlehann, discussed under, did not dig down on this problem. Rather, it refers to the efficiency of recursion in practical applications, and it is true that in addition to textbooks, recursion is seldom seen
The original purpose is only to evaluate, do not want to delve into it, or instead of the iteration, although not very impressive, but a for statement can be done
The code is as follows:
Def calc (depth): tmp = 0 result = 1 for i in Xrange (0,depth+1): cc = result if (cc ^ i)%4 = = 0: TM p = 1 else: tmp = 0 result = result + (i)%7 + tmp return resultfinal = Calc (11589) Print final
Just a few lines of code, all of a sudden. Thinking of the last interview, the TX interviewer asked me the algorithm, then referred to the use of recursive implementation of an operation, and then think is not can also use iteration?
Time has been long, and the topic is not very clear, but today's lesson is: Most (code write less, by the sense of the estimate), the efficiency of recursion is relatively low, this can be determined that the class
Time has also been mentioned. The efficiency of using iterations is significantly higher than recursion (the specific concept of iteration is not very clear), at least with loops, hundreds of thousands of times I'm sure it's okay, but even if I change the recursion limit, I'm going to hit a strike.
Finally, paste a python long VS C long long link, interested can go to see