Skip to content

Commit

Permalink
doc for get_primes()
Browse files Browse the repository at this point in the history
  • Loading branch information
Thamerd committed Jan 22, 2025
1 parent 7a9e7b7 commit 75d0f1b
Showing 1 changed file with 109 additions and 0 deletions.
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
}

0 comments on commit 75d0f1b

Please sign in to comment.