Générateur de Suites Numériques
Générez des suites arithmétiques, géométriques, de Fibonacci et de nombres premiers.
Questions Fréquentes
Implémentation du Code
def arithmetic_nth(a, d, n):
"""n-th term of arithmetic sequence (1-indexed)."""
return a + (n - 1) * d
def arithmetic_sum(a, d, n):
"""Sum of first n terms of arithmetic sequence."""
return n * (2 * a + (n - 1) * d) / 2
def geometric_nth(a, r, n):
"""n-th term of geometric sequence (1-indexed)."""
return a * (r ** (n - 1))
def geometric_sum(a, r, n):
"""Sum of first n terms of geometric sequence."""
if r == 1:
return a * n
return a * (1 - r ** n) / (1 - r)
def fibonacci(n):
"""Generate first n Fibonacci numbers."""
seq = [0, 1]
for _ in range(n - 2):
seq.append(seq[-1] + seq[-2])
return seq[:n]
def detect_sequence(seq):
"""Auto-detect if sequence is arithmetic, geometric, or other."""
diffs = [seq[i+1] - seq[i] for i in range(len(seq) - 1)]
if len(set(diffs)) == 1:
return "arithmetic", diffs[0]
ratios = [seq[i+1] / seq[i] for i in range(len(seq) - 1) if seq[i] != 0]
if len(set(round(r, 9) for r in ratios)) == 1:
return "geometric", ratios[0]
return "other", None
# Examples
print(arithmetic_nth(2, 3, 5)) # 14 (2,5,8,11,14)
print(arithmetic_sum(1, 1, 100)) # 5050 (Gauss sum)
print(geometric_sum(1, 0.5, float("inf"))) # use formula: 1/(1-0.5)=2
print(geometric_sum(1, 2, 10)) # 1023
print(fibonacci(10)) # [0,1,1,2,3,5,8,13,21,34]
print(detect_sequence([3,6,12,24])) # ('geometric', 2.0)Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.