Skip to content

Commit

Permalink
added some easy examples for is_prime
Browse files Browse the repository at this point in the history
  • Loading branch information
vrudhgarg committed Jan 26, 2025
1 parent f5ab9dd commit b2f63f4
Showing 1 changed file with 55 additions and 28 deletions.
83 changes: 55 additions & 28 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 1,
"metadata": {},
"outputs": [
{
Expand All @@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -107,7 +107,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -144,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -189,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -270,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -300,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -329,7 +329,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -415,7 +415,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -456,7 +456,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 10,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -529,59 +529,86 @@
"```\n",
"\n",
"---\n",
"### Application 1: Prime Pattern Finder\n",
"### Application 1: Cryptography & Security PIN Validation\n",
"\n",
"By filtering prime numbers that satisfy the pattern $p = 6k \\pm 1$, this demonstrates how mathematical properties can be utilized to further refine prime numbers."
"In cryptography, prime numbers are widely used for secure encryption. A system might validate that a chosen PIN is prime before accepting it as secure.\n"
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Special primes (6k ± 1) between 2 and 200: [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]\n"
"PIN 53 is accepted as it's a prime number—secure and unique!\n"
]
}
],
"source": [
"from num_theory.is_prime import is_prime\n",
"pin = 53\n",
"if is_prime(pin):\n",
" print(f\"PIN {pin} is accepted as it's a prime number—secure and unique!\")\n",
"else:\n",
" print(f\"PIN {pin} is rejected. Please choose a prime number.\")\n",
"# Output: PIN 53 is accepted as it's a prime number—secure and unique!\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application 2: Unique Classroom Groups\n",
"\n",
"# Find primes matching the pattern\n",
"def find_special_primes(start, end):\n",
" primes = [n for n in range(start, end + 1) if is_prime(n)]\n",
" special_primes = [p for p in primes if (p - 1) % 6 == 0 or (p + 1) % 6 == 0]\n",
" return special_primes\n",
"\n",
"# Find special primes in the range\n",
"special_primes = find_special_primes(2, 200)\n",
"print(f\"Special primes (6k ± 1) between 2 and 200: {special_primes}\")"
"A teacher wants to divide students into groups of a size that is prime, ensuring groups are small and effective."
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Group size 7 works! It's prime, so students will interact uniquely.\n"
]
}
],
"source": [
"group_size = 7\n",
"if is_prime(group_size):\n",
" print(f\"Group size {group_size} works! It's prime, so students will interact uniquely.\")\n",
"else:\n",
" print(f\"Group size {group_size} isn't prime. Let's try a different size.\")\n",
"# Output: Group size 7 works! It's prime, so students will interact uniquely."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application 2: Generate RSA Keys\n",
"### Application 3: Generate RSA Keys\n",
"\n",
"In encryption applications, generating an RSA key pair (public and private keys) requires two large prime numbers and modulus calculations. This code uses `is_prime` to verify the generated prime numbers and constructs the RSA public and private keys, showcasing a practical application in a real-world encryption system."
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RSA Keys:\n",
"Public Key: (52931, 126169)\n",
"Private Key: (44011, 126169)\n"
"Public Key: (14315, 62429)\n",
"Private Key: (40511, 62429)\n"
]
}
],
Expand Down Expand Up @@ -653,7 +680,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "524",
"display_name": "test_env",
"language": "python",
"name": "python3"
},
Expand Down

0 comments on commit b2f63f4

Please sign in to comment.