# 详解python中的迭代与递归方法

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没怎么玩过，但是这几行代码看过来还是没压力的，快刀斩乱麻改为对于python代码

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

cimal = 7
original = 28679718602997181072337614380936720482949
array = “”
result= “”
while original !=0:
remainder = original % cimal
array += str(remainder)
original /= cimal
length = len(array)
for i in xrange(0,length):
result += array[length-1-i]
print result

import sys

# set the maximun depth as 20000

sys.setrecursionlimit(20000)

def calc(depth):
tmp = 0
result = 1
for i in xrange(0,depth+1):
cc = result
if (cc ^ i)%4 == 0:
tmp = 1
else:
tmp = 0
result = result + (i)%7 + tmp
return result
final = calc(11589)
print final

Posted in 未分类