Skip to content

Commit 239b5cc

Browse files
committed
fix fib
1 parent 82ab8ce commit 239b5cc

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

‎commands.py‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,13 @@
99
from base64 import *
1010

1111
phi = (1+5**.5)/2
12-
Fib = lambda n:int(phi**n/5**.5+.5)
12+
def Fib(n):
13+
if n<2:
14+
return n
15+
a,b=1,1
16+
while n>2:
17+
a,b,n=b,a+b,n-1
18+
return b
1319

1420
primes = [2,3]
1521

@@ -24,10 +30,7 @@ def __call__(self,*args):
2430

2531
class Math(object):
2632
def __getattr__(self, fn):
27-
if fn in ['pi','e']:
28-
return getattr(rmath,fn)
29-
else:
30-
return MathSelector(fn)
33+
return MathSelector(fn) if isinstance(getattr(cmath,fn),function) else getattr(rmath,fn)
3134

3235
math = Math()
3336

@@ -92,13 +95,10 @@ def nth_prime(n):
9295
return primes[n]
9396

9497
def Fib_index(n):
95-
a,b=5*n**2+4,5*n**2-4
96-
if int(a**.5)==a:
97-
return int(math.log((n*5**.5+a**.5)/2)/math.log(phi))
98-
elif int(b**.5)==b:
99-
return int(math.log((n*5**.5+b**.5)/2)/math.log(phi))
100-
else:
101-
return -1
98+
i=0
99+
while Fib(i)<n:
100+
i+=1
101+
return i if Fib(i) == n else -1
102102

103103
def div_fn(srs):
104104
a=srs.pop()

0 commit comments

Comments
 (0)