Skip to content

Commit 893edfd

Browse files
committed
Cleaned up code via cursor.
1 parent bfebc36 commit 893edfd

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

code/09-agentic-ai/math-research/math_research.ipynb

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
},
1313
{
1414
"cell_type": "code",
15-
"execution_count": 1,
15+
"execution_count": 4,
1616
"id": "6040bded7e982e0c",
1717
"metadata": {
1818
"ExecuteTime": {
@@ -29,6 +29,38 @@
2929
"import mathf"
3030
]
3131
},
32+
{
33+
"cell_type": "raw",
34+
"metadata": {
35+
"vscode": {
36+
"languageId": "raw"
37+
}
38+
},
39+
"source": [
40+
"## Understanding the Mathematical Problem\n",
41+
"\n",
42+
"### Fibonacci Numbers\n",
43+
"The Fibonacci sequence is one of the most famous sequences in mathematics, where each number is the sum of the two preceding ones:\n",
44+
"**1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...**\n",
45+
"\n",
46+
"### The Research Question\n",
47+
"We're investigating an interesting mathematical relationship: **Which Fibonacci numbers are multiples of prime numbers?**\n",
48+
"\n",
49+
"Specifically, we want to explore:\n",
50+
"- Given a prime number (like 17), which Fibonacci numbers are divisible by it?\n",
51+
"- How large do these numbers get?\n",
52+
"- Is there a pattern in their occurrence?\n",
53+
"\n",
54+
"### Why This Matters\n",
55+
"This type of research helps us understand:\n",
56+
"- **Number theory patterns**: How different mathematical sequences interact\n",
57+
"- **Divisibility properties**: When and why certain relationships occur\n",
58+
"- **Computational mathematics**: How to efficiently find these relationships\n",
59+
"\n",
60+
"### What We'll Discover\n",
61+
"In the analysis below, we'll find the first 10 Fibonacci numbers that are multiples of 17, revealing how quickly these numbers grow and providing insight into the fascinating intersection of prime numbers and the Fibonacci sequence.\n"
62+
]
63+
},
3264
{
3365
"cell_type": "markdown",
3466
"id": "9209940ceb5e46c0",
@@ -41,7 +73,7 @@
4173
},
4274
{
4375
"cell_type": "code",
44-
"execution_count": 2,
76+
"execution_count": null,
4577
"id": "c8beeefbe98fd1b8",
4678
"metadata": {
4779
"ExecuteTime": {
@@ -51,15 +83,13 @@
5183
},
5284
"outputs": [],
5385
"source": [
86+
"from itertools import islice\n",
87+
"\n",
5488
"prime = 17\n",
5589
"all_fibs = mathf.fibonacci()\n",
56-
"based_fibs = mathf.multiples_of(all_fibs, prime)\n",
90+
"fib_multiples_of_prime = mathf.multiples_of(all_fibs, prime)\n",
5791
"\n",
58-
"first_10 = []\n",
59-
"for idx, fib in enumerate(based_fibs):\n",
60-
" first_10.append(fib)\n",
61-
" if idx > 10:\n",
62-
" break"
92+
"first_10 = list(islice(fib_multiples_of_prime, 10))"
6393
]
6494
},
6595
{
@@ -72,7 +102,7 @@
72102
},
73103
{
74104
"cell_type": "code",
75-
"execution_count": 3,
105+
"execution_count": 8,
76106
"id": "980daf7b8a4f2a61",
77107
"metadata": {
78108
"ExecuteTime": {
@@ -99,7 +129,12 @@
99129
"37,889,062,373,143,906\n",
100130
"2,880,067,194,370,816,120\n",
101131
"218,922,995,834,555,169,026\n",
102-
"16,641,027,750,620,563,662,096\n"
132+
"16,641,027,750,620,563,662,096\n",
133+
"1,264,937,032,042,997,393,488,322\n",
134+
"96,151,855,463,018,422,468,774,568\n",
135+
"7,308,805,952,221,443,105,020,355,490\n",
136+
"555,565,404,224,292,694,404,015,791,808\n",
137+
"42,230,279,526,998,466,217,810,220,532,898\n"
103138
]
104139
}
105140
],
@@ -120,7 +155,7 @@
120155
],
121156
"metadata": {
122157
"kernelspec": {
123-
"display_name": "Python 3 (ipykernel)",
158+
"display_name": "venv",
124159
"language": "python",
125160
"name": "python3"
126161
},
Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,43 @@
1-
import typing
1+
from collections.abc import Generator, Iterable
22

33

4-
def fibonacci() -> typing.Generator[int, None, None]:
4+
def fibonacci() -> Generator[int, None, None]:
5+
"""Generate an infinite sequence of Fibonacci numbers.
6+
7+
Yields:
8+
int: The next Fibonacci number in the sequence (1, 1, 2, 3, 5, 8, ...)
9+
10+
Example:
11+
>>> fib = fibonacci()
12+
>>> [next(fib) for _ in range(5)]
13+
[1, 1, 2, 3, 5]
14+
"""
515
current, nxt = 0, 1
616
while True:
717
current, nxt = nxt, nxt + current
818
yield current
919

1020

11-
def multiples_of(collection: typing.Iterable[int], number: int) -> typing.Generator[int, None, None]:
21+
def multiples_of(collection: Iterable[int], number: int) -> Generator[int, None, None]:
22+
"""Filter a collection to yield only multiples of a given number.
23+
24+
Args:
25+
collection: An iterable of integers to filter
26+
number: The number to find multiples of (must be positive)
27+
28+
Yields:
29+
int: Numbers from the collection that are multiples of the given number
30+
31+
Raises:
32+
ValueError: If number is not positive
33+
34+
Example:
35+
>>> list(multiples_of([1, 2, 3, 4, 5, 6], 2))
36+
[2, 4, 6]
37+
"""
38+
if number <= 0:
39+
raise ValueError('Number must be positive')
40+
1241
for n in collection:
1342
if n % number == 0:
1443
yield n

0 commit comments

Comments
 (0)