本帖最后由 元芳你怎么看 于 2016-4-8 16:08 编辑
- """
- 无忧公主的编程挑战004:
- 斐波那契数列第 12 项 144,是第一个包含 3 个数字的斐波那契数。我的问题是:第一个包含 1000 个数字的斐波那契数,是第几项呢?
- """
- def Ghost_Fibonacci(x):
- i = 1
- while True:
- f = Ghost_Fibonacci_Iteration(i)
- if f<10**(x-1):
- i = i + 1
- else:
- break
- print("The first value of Fibonacci sequence which has 1000 figures is NO.%d" % i)
-
- def Ghost_Fibonacci_Recursion(n):
- if n<=0:
- print("Please enter an positive integer!")
- if n == 1 or n == 2:
- return(1)
- else:
- fn = Ghost_Fibonacci_Recursion(n-1) + Ghost_Fibonacci_Recursion(n-2)
- return(fn)
-
- def Ghost_Fibonacci_Iteration(n):
- if n<=0:
- print("Please enter an positive integer!")
- elif n == 1 or n ==2:
- return(1)
- else:
- a,b,c = 1,1,1
- for i in range(n-2):
- c = a + b
- a = b
- b = c
- return(c)
复制代码
|