def fib if or or return return fib fib def getFibNumber fibNum if fibN

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def fib(n):
if n == 1 or n ==2 or n <= 0:
return 1.
return (fib(n - 1) + fib(n - 2))
def getFibNumber(fibNum):
if (fibNum < 1):
return 2
if (fibNum < 2):
return 3
x2, x3, n = 1, 2, 4
fibCur = x2+x3
while fibNum > fibCur:
fibCur = fibCur + x3
buf = x3
x3 = x2+x3
x2 = x3
n += 1
return n
def fibonachi(a, b, eps, delta, f):
fN = (b - a)/delta
N = getFibNumber(fN)
print N
x1 = a + (b - a)*(fib(N - 2.)/fib(N))
x2 = a + (b - a)*(fib(N - 1.)/fib(N))
y1 = f(x1)
y2 = f(x2)
k = 1;
while N != 0:
N -= 1;
x1Buf, x2Buf, aBuf, bBuf = x1, x2, a, b
if y1 > y2:
a = x1
x1 = x2
x2 = b - (aBuf + x1Buf)*(fib(N - 3.)/fib(N))
y1 = y2
y2 = f(x2Buf)
else:
b = x2
x2 = x1
x2 = a + (bBuf - x2Buf)*(fib(N - 2.)/fib(N))
y2 = y1
y1 = f(x1Buf)
k += 1
return x1