Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

doc for get_primes() #41

Merged
merged 1 commit into from
Jan 23, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 109 additions & 0 deletions docs/get_primes_doc.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial: get_primes()\n",
"\n",
"The \"get_primes()\" retrieves a list of all prime numbers under a given number. This list of prime numbers can aid you in solving Project Euler problems, creating hashing methods, as well as many other number theory and cryptography applications."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1: Project Euler's prime numbers sum.\n",
"Project Euler's 10th problem states: \"Find the sum of all the primes below two million\". Using our \"get_primes()\" function trivializes this solution, as we can simply use Python's built-in \"sum()\" function with our function while passing 2,000,000 as the argument. Our function will retrieve the list of all primes under 2,000,000 and \"sum()\" will calculate thier sum to reach the answer."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"142913828922"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from num_theory.get_primes import get_primes \n",
"\n",
"sum(get_primes(2000000))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2: Hashing\n",
"Let's say you want to create a simple hash function that hashes a given number into a number within the range of 0 and 255. You can use the modulo \"%\" operation alongside a sum of our \"get_primes()\" function's output to create a simple algorithm that achieves this. please note that this hashing function is too primitive for most practical uses and is only used here to provide an easy to understand example."
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"201\n",
"58\n",
"100\n"
]
}
],
"source": [
"def simple_hash(num: int) -> int:\n",
" return sum(get_primes(num)) % 256\n",
"\n",
"print(simple_hash(2025))\n",
"print(simple_hash(1999))\n",
"print(simple_hash(23))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional notes\n",
"The get_primes() function uses a custom Sieve of Eratosthenes algorithm to retrieve the list of prime numbers. It does this by initializing a list of all number under n, then it iterates through all of them from the bottom to the top and assigns their multiples as non-primes. Once done, it leaves you with a list containing only prime numbers under n. This algorithm has a time complexity of O(n log(log(n))) and a space complexity of O(n)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "524",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.13.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Loading