5 examples of 'python fibonacci generator' in Python

Every line of 'python fibonacci generator' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
6def py_fibonacci(n):
7 """https://stackoverflow.com/a/52133289/2536357"""
8 i = 0
9 nextterm = 1
10 present = 1
11 previous = 0
12
13 while i < n:
14 nextterm = present + previous
15 present = previous
16 previous = nextterm
17 i = i + 1
18
19 return nextterm
37@tasklets.tasklet
38def fibonacci(n):
39 """A recursive Fibonacci to exercise task switching."""
40 if n <= 1:
41 raise tasklets.Return(n)
42 a = yield fibonacci(n - 1)
43 b = yield fibonacci(n - 2)
44 raise tasklets.Return(a + b)
14def fibonacci(x: int):
15 if x < 0:
16 return -1
17 if x == 0:
18 return 0
19 cache = [0] * (x + 1)
20 for i in range(1, len(cache)):
21 cache[i] = -1
22 cache[1] = 1
23 return fibonacci_recur(x, cache)
6def test_fib(self, func):
7 result = []
8 expected = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
9 for i in range(len(expected)):
10 result.append(func(i))
11 assert_equal(result, expected)
12 print('Success: test_fib')
210def _create_fibonacci_points(n):
211 """
212 Get an array of approximately equidistant points on a sphere surface
213 using a golden section spiral.
214 """
215 phi = (3 - np.sqrt(5)) * np.pi * np.arange(n)
216 z = np.linspace(1 - 1.0/n, 1.0/n - 1, n)
217 radius = np.sqrt(1 - z*z)
218 coords = np.zeros((n, 3))
219 coords[:,0] = radius * np.cos(phi)
220 coords[:,1] = radius * np.sin(phi)
221 coords[:,2] = z
222 return coords

Related snippets