Generate Fibonacci sequences and work with Fibonacci numbers, golden ratios, and related mathematical concepts.
The Fibonacci sequence starts with 0, 1, and each subsequent number is the sum of the two preceding ones.
First 10 Fibonacci numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34
def fibonacci(n):
"""Generate first n Fibonacci numbers."""
if n <= 0:
return []
elif n == 1:
return [0]
seq = [0, 1]
for i in range(2, n):
seq.append(seq[-1] + seq[-2])
return seq
def nth_fibonacci(n):
"""Get the nth Fibonacci number (0-indexed)."""
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
import math
def is_fibonacci(x):
"""Check if x is a Fibonacci number."""
def is_perfect_square(n):
s = int(math.sqrt(n))
return s * s == n
return is_perfect_square(5 * x * x + 4) or is_perfect_square(5 * x * x - 4)
As n increases, the ratio F(n+1)/F(n) approaches the golden ratio φ ≈ 1.618033988749895
Similar to Fibonacci but starts with 2, 1
Each number is sum of previous three
Sum of first n Fibonacci numbers = F(n+2) - 1
共 1 个版本