Python 輸出斐波納挈數列
#coding:utf8import time import os global_list = [] def test1(n): if n==1 or n==2: return 1 else: return test1(n-1) + test1(n-2) def test2(n): global global_list if n==1 or n==2: return 1 else: temp = test2(n-1) + test2(n-2) if isinstance(temp,int): global_list.append(temp) return temp def main(): global global_list input_int = raw_input("Entry your number:") length = int(input_int) #第一種方法 start = time.clock() for i in xrange(length): print test1(i+1 ), end = time.clock() print os.linesep + "處理耗時:%f s"%(end-start) #第二種方法 start = time.clock() test2(length) temp_set = set(global_list) global_list = [i for i in temp_set] global_list.sort(reverse=True) global_list.extend([1,1]) for i in global_list[::-1]: print i, end = time.clock() print os.linesep + "處理耗時:%f s"%(end-start) if __name__ == "__main__": main() </pre>
本文由用戶 cc68 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!