Add files via upload

This commit is contained in:
Peter Norvig
2026-05-29 13:59:54 -07:00
committed by GitHub
parent 7ac2359e88
commit d67caa2820

View File

@@ -53,7 +53,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 1,
"id": "249c7f13-cdf9-41a2-b0e7-8a3dec15f74b", "id": "249c7f13-cdf9-41a2-b0e7-8a3dec15f74b",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -81,7 +81,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 2,
"id": "734de0e3-aafc-438a-8332-09741eef39aa", "id": "734de0e3-aafc-438a-8332-09741eef39aa",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -91,7 +91,7 @@
"6857" "6857"
] ]
}, },
"execution_count": 11, "execution_count": 2,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -105,29 +105,36 @@
"id": "8bf8dd51-8e06-4572-85fa-fe42875e0305", "id": "8bf8dd51-8e06-4572-85fa-fe42875e0305",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Example Factor Tree\n", "We can represent the execution of the program as a series of equations, one per line. Using `F` as an abbreviation for `largest_prime_factor`, here is hjow we find the largest prime factor of 360:\n",
"\n", "\n",
"[Here](https://www.cuemath.com/numbers/factor-tree/) is the \"factor tree\" of *n* = 36:\n", " F(360) = max(2, F(180))\n",
"\n", " F(180) = max(2, F(90))\n",
"<img src=\"factor36.png\" width=300>\n", " F(90) = max(2, F(45))\n",
"\n", " F(45) = max(3, F(15))\n",
"\n", " F(15) = max(3, F(5))\n",
"We could also represent this as a series of equations, one per line:\n", " F(5) = 5\n",
"\n", " F(36) = max(2, max(2, max(2, 3, 3, 5))) = 5"
" 36 = 2 × 18\n", ]
" 18 = 2 × 9\n", },
" 9 = 3 × 3\n", {
" 3 = 3\n", "cell_type": "code",
" 36 = 2 × 2 × 3 × 3\n", "execution_count": 3,
"\n", "id": "a4aaf952-bf6a-4dec-a950-5c573e1fbef0",
"\n", "metadata": {},
"Or as equations for how `largest_prime_factor(36)` is broken down, with `lpf` as an abbreviation for `largest_prime_factor`:\n", "outputs": [
"\n", {
" lpf(36) = max(2, lpf(18))\n", "data": {
" lpf(18) = max(2, lpf(9))\n", "text/plain": [
" lpf(9) = max(3, lpf(3))\n", "360"
" lpf(3) = 3\n", ]
" lpf(36) = max(2, max(2, max(3, 3))) = 3" },
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"2 * 2 * 2 * 3 * 3 * 5"
] ]
}, },
{ {
@@ -142,34 +149,38 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 4,
"id": "9bedb1c9-4138-4250-90e6-77b10ba01586", "id": "9bedb1c9-4138-4250-90e6-77b10ba01586",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'all tests pass'" "[11, 'out of', 11, 'tests pass']"
] ]
}, },
"execution_count": 12, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
], ],
"source": [ "source": [
"def tests():\n", "def tests():\n",
" assert largest_prime_factor(1) == 1 # by convention, 1\n", " \"\"\"Test largest_prime_factor.\"\"\"\n",
" assert largest_prime_factor(2) == 2 # even prime\n", " cases = {1: 1, # by convention, 1\n",
" assert largest_prime_factor(3) == 3 # odd prime\n", " 2: 2, # even prime\n",
" assert largest_prime_factor(6) == 3 # composite\n", " 3: 3, # odd prime\n",
" assert largest_prime_factor(8) == 2 # power of 2\n", " 6: 3, # composite\n",
" assert largest_prime_factor(36) == 3 # example from the diagram\n", " 32: 2, # power of 2\n",
" assert largest_prime_factor(49) == 7 # square of a prime\n", " 49: 7, # square of a prime\n",
" assert largest_prime_factor(97) == 97 # bigger prime\n", " 97: 97, # bigger prime\n",
" assert largest_prime_factor(97 ** 9) == 97 # even bigger prime\n", " 99991: 99991, # even bigger prime\n",
" assert largest_prime_factor(600851475143) == 6857 # really big number\n", " 97**9: 97, # even bigger power of a prime\n",
" return 'all tests pass'\n", " 360: 5, # test case for equations above\n",
" 600851475143: 6857 # Project Euler #3\n",
" }\n",
" correct = sum(largest_prime_factor(n) == cases[n] for n in cases)\n",
" return [correct, 'out of', len(cases), 'tests pass']\n",
"\n", "\n",
"tests()" "tests()"
] ]
@@ -186,7 +197,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 5,
"id": "0e4a1dc6-5969-49fd-9d65-11d9801cbea2", "id": "0e4a1dc6-5969-49fd-9d65-11d9801cbea2",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -194,8 +205,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: user 633 μs, sys: 71 μs, total: 704 μs\n", "CPU times: user 181 μs, sys: 0 ns, total: 181 μs\n",
"Wall time: 706 μs\n" "Wall time: 183 μs\n"
] ]
}, },
{ {
@@ -204,7 +215,7 @@
"6857" "6857"
] ]
}, },
"execution_count": 13, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -223,7 +234,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 6,
"id": "7323d528-96d7-4c05-a05d-125e99605443", "id": "7323d528-96d7-4c05-a05d-125e99605443",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -231,8 +242,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: user 1.77 s, sys: 21.7 ms, total: 1.79 s\n", "CPU times: user 1.9 s, sys: 18.4 ms, total: 1.92 s\n",
"Wall time: 1.79 s\n" "Wall time: 1.92 s\n"
] ]
}, },
{ {
@@ -241,7 +252,7 @@
"99999989" "99999989"
] ]
}, },
"execution_count": 14, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -265,7 +276,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 7,
"id": "b90b5407-4666-4925-99d2-6a3a6b192fac", "id": "b90b5407-4666-4925-99d2-6a3a6b192fac",
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
@@ -293,17 +304,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 8,
"id": "4026dc87-a0aa-4c75-b92a-96ec24cda1b7", "id": "4026dc87-a0aa-4c75-b92a-96ec24cda1b7",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'all tests pass'" "[11, 'out of', 11, 'tests pass']"
] ]
}, },
"execution_count": 16, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -322,7 +333,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 9,
"id": "d3d0c13e-5c01-4112-b372-60f7eb302d25", "id": "d3d0c13e-5c01-4112-b372-60f7eb302d25",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -330,8 +341,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: user 209 μs, sys: 0 ns, total: 209 μs\n", "CPU times: user 196 μs, sys: 0 ns, total: 196 μs\n",
"Wall time: 210 μs\n" "Wall time: 196 μs\n"
] ]
}, },
{ {
@@ -340,7 +351,7 @@
"99999989" "99999989"
] ]
}, },
"execution_count": 17, "execution_count": 9,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -361,7 +372,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 18, "execution_count": 10,
"id": "9b6030ef-626a-4195-aedb-ef2edac65da4", "id": "9b6030ef-626a-4195-aedb-ef2edac65da4",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
@@ -369,8 +380,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"CPU times: user 2.15 s, sys: 22 ms, total: 2.17 s\n", "CPU times: user 1.96 s, sys: 19.3 ms, total: 1.97 s\n",
"Wall time: 2.17 s\n" "Wall time: 1.97 s\n"
] ]
}, },
{ {
@@ -379,7 +390,7 @@
"9927935178558959" "9927935178558959"
] ]
}, },
"execution_count": 18, "execution_count": 10,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -403,17 +414,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 19, "execution_count": 11,
"id": "0308612c-6860-49b0-bcd6-856fa08133b1", "id": "0308612c-6860-49b0-bcd6-856fa08133b1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'all tests pass'" "[11, 'out of', 11, 'tests pass']"
] ]
}, },
"execution_count": 19, "execution_count": 11,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -444,17 +455,17 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 12,
"id": "b8907a8f-872f-4531-825c-fae9c70211c1", "id": "b8907a8f-872f-4531-825c-fae9c70211c1",
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"data": { "data": {
"text/plain": [ "text/plain": [
"'all tests pass'" "[11, 'out of', 11, 'tests pass']"
] ]
}, },
"execution_count": 20, "execution_count": 12,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@@ -476,14 +487,6 @@
" \n", " \n",
"tests()" "tests()"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"id": "5a3de62a-5e7a-4458-b754-6b2ab50e5fb9",
"metadata": {},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {