Add Eulers Totient

This commit is contained in:
Johannes Røsvik 2020-05-23 16:13:55 +02:00
parent bf8a6e23be
commit f0279f0a5d
No known key found for this signature in database
GPG Key ID: 8A47E30339E13FFD
1 changed files with 78 additions and 11 deletions

View File

@ -172,7 +172,9 @@
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Euler function φ and Z*n\n", "# Euler function φ and Z*n\n",
"From slides 2020-4135-l07 - Number Theory for Public Key Cryptography" "From slides 2020-4135-l07 - Number Theory for Public Key Cryptography\n",
"\n",
"> $\\phi(16) = \\phi(2^{4}) = 16*\\left(1-\\frac{1}{2}\\right)$"
] ]
}, },
{ {
@ -184,8 +186,8 @@
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"φ(81) = 54 since the 54 items in Z*81 are each relatively prime to 81.\n", "φ(16) = 8 since the 8 items in Z*16 are each relatively prime to 16.\n",
"Z*81 = [1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 26, 28, 29, 31, 32, 34, 35, 37, 38, 40, 41, 43, 44, 46, 47, 49, 50, 52, 53, 55, 56, 58, 59, 61, 62, 64, 65, 67, 68, 70, 71, 73, 74, 76, 77, 79, 80]\n" "Z*16 = [1, 3, 5, 7, 9, 11, 13, 15]\n"
] ]
} }
], ],
@ -204,13 +206,78 @@
" r.append(i)\n", " r.append(i)\n",
" return r\n", " return r\n",
"\n", "\n",
"n = 81\n", "n = 16\n",
"ar = z_star(n)\n", "ar = z_star(n)\n",
"e = len(ar)\n", "e = len(ar)\n",
"print(f\"φ({n}) = {e} since the {e} items in Z*{n} are each relatively prime to {n}.\")\n", "print(f\"φ({n}) = {e} since the {e} items in Z*{n} are each relatively prime to {n}.\")\n",
"print(f\"Z*{n} = {ar}\")" "print(f\"Z*{n} = {ar}\")"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Eulers Totient\n",
"- https://www.dcode.fr/euler-totient\n",
"- https://www.ibnus.io/eulers-totient-function-using-python/\n",
"\n",
"In general:\n",
"\n",
"> $\\varphi(n) = n \\prod_{p \\mid n} \\left( 1 - \\frac{1}{p} \\right)$\n",
"\n",
"Examples:\n",
"> $\\varphi(16) = \\varphi(2^{4}) = 16*\\left(1-\\frac{1}{2}\\right) = 8$\n",
"\n",
"> $\\varphi(1125) = \\varphi(33555) = 1125\\left(1-\\frac{1}{3}\\right)\\left(1-\\frac{1}{5}\\right) = 600$\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"𝜑(1125) = 600\n"
]
}
],
"source": [
"# Euler's Totient Function\n",
"# using Euler's product formula\n",
"def totient(n) :\n",
" result = n # Initialize result as n\n",
"\n",
" # Consider all prime factors\n",
" # of n and for every prime\n",
" # factor p, multiply result with (1 1/p)\n",
" p = 2\n",
" while(p * p <= n) :\n",
"\n",
" # Check if p is a prime factor.\n",
" if (n % p == 0) :\n",
"\n",
" # If yes, then update n and result\n",
" while (n % p == 0) :\n",
" n = n // p\n",
" result = result * (1.0 - (1.0 / (float) (p)))\n",
" p = p + 1\n",
"\n",
" # If n has a prime factor\n",
" # greater than sqrt(n)\n",
" # (There can be at-most one\n",
" # such prime factor)\n",
" if (n > 1) :\n",
" result = result * (1.0 - (1.0 / (float)(n)))\n",
"\n",
" return (int)(result)\n",
"\n",
"n = 1125\n",
"print(f\"𝜑({n}) = {totient(1125)}\")"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -221,7 +288,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -245,14 +312,14 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"1105 is composite\n" "1105 is probable prime\n"
] ]
} }
], ],
@ -299,7 +366,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -382,7 +449,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -442,7 +509,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -519,7 +586,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {