Skip to content

Commit 0426602

Browse files
Add fibonacci.ipynb
Add Fibonacci program into Python with Jupyter notebook.
1 parent bca9010 commit 0426602

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
{
2+
"metadata": {
3+
"kernelspec": {
4+
"name": "xpython",
5+
"display_name": "Python 3.13 (XPython)",
6+
"language": "python"
7+
},
8+
"language_info": {
9+
"file_extension": ".py",
10+
"mimetype": "text/x-python",
11+
"name": "python",
12+
"version": "3.13.1"
13+
}
14+
},
15+
"nbformat_minor": 5,
16+
"nbformat": 4,
17+
"cells": [
18+
{
19+
"id": "cce9d36f-b5b9-4910-a2c4-522d1dbd9aa3",
20+
"cell_type": "code",
21+
"source": "def fibonacci_iterative(n):\n \"\"\"\n Generates the Fibonacci sequence up to the n-th term iteratively.\n Author: IamBisrutPyne\n Time Complexity: O(n)\n Space Complexity: O(1) (excluding the list of results)\n \"\"\"\n if n < 0:\n raise ValueError(\"Input must be a non-negative integer.\")\n if n == 0:\n return []\n if n == 1:\n return [0]\n\n # Initialize sequence with the first two terms: F(0)=0, F(1)=1\n sequence = [0, 1]\n \n a, b = 0, 1\n for _ in range(2, n):\n next_val = a + b\n sequence.append(next_val)\n \n # Shift values\n a = b\n b = next_val\n \n return sequence\n\nn_terms = 20\nresult = fibonacci_iterative(n_terms)\nprint(f\"Fibonacci sequence up to {n_terms} terms: {result}\")",
22+
"metadata": {
23+
"trusted": true
24+
},
25+
"outputs": [
26+
{
27+
"name": "stdout",
28+
"output_type": "stream",
29+
"text": "Fibonacci sequence up to 10 terms: [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]\n"
30+
}
31+
],
32+
"execution_count": 1
33+
},
34+
{
35+
"id": "f1aa0b69-6048-441f-a93b-f7896398e4a8",
36+
"cell_type": "code",
37+
"source": "",
38+
"metadata": {
39+
"trusted": true
40+
},
41+
"outputs": [],
42+
"execution_count": null
43+
}
44+
]
45+
}

0 commit comments

Comments
 (0)