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