-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathsequence.py
48 lines (39 loc) · 1.14 KB
/
sequence.py
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
# This is made by MRayan Asim
# packages needed:
# pip install matplotlib.pyplot
import matplotlib.pyplot as plt
def fibonacci_sequence(n):
sequence = []
if n <= 0:
return sequence
elif n == 1:
sequence = [0]
elif n == 2:
sequence = [0, 1]
else:
sequence = [0, 1]
while len(sequence) < n:
next_number = sequence[-1] + sequence[-2]
sequence.append(next_number)
return sequence
print(
"Hello! This program of Fibonacci series is made by MRayan Asim. Hope you will like this! 😊"
)
# Get user input
num_terms = int(input("Enter the number of terms in the Fibonacci sequence: "))
# Generate the Fibonacci sequence
fib_sequence = fibonacci_sequence(num_terms)
# Display the formula
print("Formula: F(n) = F(n-1) + F(n-2)")
# Display the result with commas
fib_str = ", ".join(str(num) for num in fib_sequence)
print("Fibonacci sequence:")
print(fib_str)
# Create the graph
plt.plot(range(1, num_terms + 1), fib_sequence, marker="o")
plt.xlabel("Number of Terms")
plt.ylabel("Fibonacci Term Value")
plt.title("Fibonacci Sequence")
plt.grid(True)
time.sleep(3)
plt.show()