mirror of
https://github.com/rzmk/ladderz.git
synced 2025-12-19 06:59:25 +00:00
feat(pre-algebra): add is_factor and is_multiple
This commit is contained in:
parent
a380407f21
commit
186f76d4f7
3 changed files with 205 additions and 19 deletions
|
|
@ -24,7 +24,7 @@
|
|||
"\n",
|
||||
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/understanding-factor-pairs).\n",
|
||||
"\n",
|
||||
"**Write a program that finds all the factor pairs for a given number $n$.**\n",
|
||||
"**Write a program that finds all the factor pairs for a number $n$.**\n",
|
||||
"\n",
|
||||
"- Do not repeat any pairs (e.g., consider `(2, 8)` and `(8, 2)` as the same).\n",
|
||||
"- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n",
|
||||
|
|
@ -38,7 +38,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 25,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
|
@ -72,7 +72,9 @@
|
|||
"\n",
|
||||
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-of-a-number).\n",
|
||||
"\n",
|
||||
"**Write a program that finds all the factors of a given number $n$.** Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n",
|
||||
"**Write a program that finds all the factors of a number $n$.**\n",
|
||||
"\n",
|
||||
"- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n",
|
||||
"\n",
|
||||
"For example for $n = 16$, the output of `get_factors(16)` may be:\n",
|
||||
"\n",
|
||||
|
|
@ -83,7 +85,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
|
|
@ -101,6 +103,93 @@
|
|||
"assert get_factors(16) == {1, 2, 4, 8, 16}\n",
|
||||
"assert get_factors(120) == {1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Finding factors and multiples\n",
|
||||
"\n",
|
||||
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-and-multiples)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that checks if a number $x$ is a factor of $y$.**\n",
|
||||
"\n",
|
||||
"- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n",
|
||||
"\n",
|
||||
"For example, for $x = 2$ and $y = 10$, the output of `is_factor(2, 10)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"True\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_factor(x: int, y: int) -> bool:\n",
|
||||
" return y % x == 0\n",
|
||||
"\n",
|
||||
"assert is_factor(2, 10) == True\n",
|
||||
"assert is_factor(3, 10) == False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that checks if a number $x$ is a multiple of $y$.**\n",
|
||||
"\n",
|
||||
"- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n",
|
||||
"\n",
|
||||
"For example, for $x = 20$ and $y = 3$, the output of `is_multiple(20, 3)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"True\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_multiple(x: int, y: int) -> bool:\n",
|
||||
" return x % y == 0\n",
|
||||
"\n",
|
||||
"assert is_multiple(10, 2) == True\n",
|
||||
"assert is_multiple(10, 3) == False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that finds all multiples of a given number $n$ up to a given limit $l$.**\n",
|
||||
"\n",
|
||||
"- Assume that $n$ and $l$ are positive integers greater than or equal to 1 ($n, l \\in \\mathbb{Z}^+$).\n",
|
||||
"\n",
|
||||
"For example for $n = 3$ and $l = 20$, the output of `get_multiples(3, 20)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"{3, 6, 9, 12, 15, 18}\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue