How to use python to Recursive output Yang Hui Triangle
Xiaobian to share with you how to use python recursive output Yang Hui triangle, I believe most people still do not know how, so share this article for everyone's reference, I hope you have a lot of harvest after reading this article, let's go to understand it together!
recursive
Yang Hui triangle characteristic:
【1,1】=【0,1】+【1,0】
【1,2,1】=【0,1,1】+【1,1,0】
【1,3,3,1】=【0,1,2,1】+【1,2,1,0】
【1,4,6,4,1】=【0,1,3,3,1】+【1,3,3,1,0】
Line n equals line n-1, complement 0 from beginning to end respectively, and then add them bitwise.
def triangle_4(n):""" :param n: Number of Yang Hui triangle rows to be generated:return: "" triangle = [1] #Initialize Yang Hui triangle if n == 0: return triangle return [x+y for x, y in zip ([0] + triangle_4(n - 1), triangle_4(n - 1) + [0])]for i in range(10): print (''.join(str(triangle_4(i))).center(100)) The above is "Python how to use recursive output Yang Hui triangle" All the contents of this article, thank you for reading! I believe that everyone has a certain understanding, hope to share the content to help everyone, if you still want to learn more knowledge, welcome to pay attention to the industry information channel!