feat(pre-algebra): refactor to get_multiples_in_range & add to rust lib

This commit is contained in:
rzmk 2023-09-21 00:33:49 -04:00
parent 8ed5716a83
commit a8261965a6
No known key found for this signature in database
2 changed files with 66 additions and 7 deletions

View file

@ -173,11 +173,11 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"**Write a program that finds all multiples of a given number $n$ up to a given limit $l$.**\n",
"**Write a program that finds all multiples of a positive integer `n` in the range `[n, end]`.**\n",
"\n",
"- Assume that $n$ and $l$ are positive integers greater than or equal to 1 ($n, l \\in \\mathbb{Z}^+$).\n",
"- Assume that `n` and `end` are positive integers greater than or equal to 1.\n",
"\n",
"For example for $n = 3$ and $l = 20$, the output of `get_multiples(3, 20)` may be:\n",
"For example for `n = 3` and `end = 20`, the output of `get_multiples_in_range(3, 20)` may be:\n",
"\n",
"```\n",
"{3, 6, 9, 12, 15, 18}\n",
@ -190,15 +190,15 @@
"metadata": {},
"outputs": [],
"source": [
"def get_multiples(n: int, l: int) -> set:\n",
"def get_multiples_in_range(n: int, end: int) -> set:\n",
" multiples = set()\n",
" for num in range(n, l + 1):\n",
" for num in range(n, end + 1):\n",
" if num % n == 0:\n",
" multiples.add(num)\n",
" return multiples\n",
"\n",
"assert get_multiples(3, 20) == {3, 6, 9, 12, 15, 18}\n",
"assert get_multiples(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}"
"assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",
"assert get_multiples_in_range(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}"
]
}
],