The Fibonacci numbers are the numbers in the following integer sequence (Fn):
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
such as:F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying
F(n) * F(n+1) = prod.
Your function productFib takes an integer (prod) and returns an array:
[F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)
depending on the language if F(n) * F(n+1) = prod.
If you don't find two consecutive F(m) verifying F(m) * F(m+1) = prodyou will return
[F(m), F(m+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, ...
such as:F(n) = F(n-1) + F(n-2) with F(0) = 0 and F(1) = 1.
Given a number, say prod (for product), we search two Fibonacci numbers F(n) and F(n+1) verifying
F(n) * F(n+1) = prod.
Your function productFib takes an integer (prod) and returns an array:
[F(n), F(n+1), true] or {F(n), F(n+1), 1} or (F(n), F(n+1), True)
depending on the language if F(n) * F(n+1) = prod.
If you don't find two consecutive F(m) verifying F(m) * F(m+1) = prodyou will return
[F(m), F(m+1), false] or {F(n), F(n+1), 0} or (F(n), F(n+1), False)
F(m) being the smallest one such as F(m) * F(m+1) > prod.
注意两点:
1.while循环没范围,用条件划范围,range总需要先给出个范围,这就是用起来要注意的区别和优化
2.对错的返回,完全可以不用去if定义,直接列出一个运算的式子,满足就自然对,不满足自然错,这是if的优化
【fib数列到200个,数位可达到20+位以上以上以上】
我的解法:(如此智障的解法昂)
def productFib(prod): a,b=1,1 l=[0,1,1]#数列初始的几个数 ll=[] for i in range(200): b,a=a,a+b #累加的一个变换 l.append(a) for j in range (len(l)-2):#防止出范围 if prod==l[j]*l[j+1]: ll.append(l[j]) ll.append(l[j+1]) ll.append(True) elif l[j]*l[j+1]<prod<l[j+1]*l[j+2]: ll.append(l[j+1]) ll.append(l[j+2]) ll.append(False) return ll
大神解法:
def productFib(prod): a, b = 0, 1 while prod > a * b: a, b = b, a + b return [a, b, prod == a * b]【打脸不??!!跟大神的解法一比,我的逻辑思维幼稚的一笔啊~~~整那么多五五六六七七八八有啥用!!】