More latex please

This commit is contained in:
Johannes Røsvik 2020-05-23 18:06:44 +02:00
parent f404704d7d
commit da33a814e6
No known key found for this signature in database
GPG Key ID: 8A47E30339E13FFD
1 changed files with 69 additions and 69 deletions

View File

@ -10,7 +10,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 14,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -19,7 +19,7 @@
"5" "5"
] ]
}, },
"execution_count": 1, "execution_count": 14,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
@ -46,7 +46,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 17,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -72,7 +72,7 @@
" x = y1 - (b//a) * x1 \n", " x = y1 - (b//a) * x1 \n",
" y = x1 \n", " y = x1 \n",
"\n", "\n",
" return gcd,x,y \n", " return gcd, x, y \n",
"\n", "\n",
"a, b = 35,10\n", "a, b = 35,10\n",
"g, x, y = gcdExtended(a, b)\n", "g, x, y = gcdExtended(a, b)\n",
@ -92,7 +92,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 16,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -130,7 +130,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -167,6 +167,57 @@
"print(crt(n, a))" "print(crt(n, a))"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integer factorisation\n",
"Given an integer, find its prime factors"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The factors of 641361 are [3, 7, 7, 4363], verified\n",
"Manual test: True\n"
]
}
],
"source": [
"# Brute force solution\n",
"def prime_factors(n):\n",
" i = 2\n",
" factors = []\n",
" while i * i <= n:\n",
" if n % i:\n",
" i += 1\n",
" else:\n",
" n //= i\n",
" factors.append(i)\n",
" if n > 1:\n",
" factors.append(n)\n",
" return factors\n",
"\n",
"# Check if the numbers in array a is the factors of n\n",
"def factor_test(a, n):\n",
" k = 1\n",
" for i in factors:\n",
" k=k*i\n",
" return k==n\n",
"\n",
"n = 641361\n",
"factors = prime_factors(n)\n",
"print(f\"The factors of {n} are {factors}, {'verified' if factor_test(factors, n) else 'ERROR'}\")\n",
"\n",
"print(\"Manual test:\", factor_test([3, 7, 7, 4363], 641361))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -179,7 +230,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -233,7 +284,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -286,16 +337,16 @@
"- https://crypto.stanford.edu/pbc/notes/numbertheory/gen.html\n", "- https://crypto.stanford.edu/pbc/notes/numbertheory/gen.html\n",
"- Lecture 2, page 19\n", "- Lecture 2, page 19\n",
"\n", "\n",
"A generator of $Z^p$ is an element of order $p 1$\n", "A generator of $Z_p^$ is an element of order $p 1$\n",
"\n", "\n",
"To find a generator of Zp we can choose a value g and test it as follows:\n", "To find a generator of $Z_p^$ we can choose a value g and test it as follows:\n",
"1. compute all the distinct prime factors of p 1 and call them f1,f2,...,fr\n", "1. compute all the distinct prime factors of $p 1$ and call them $f_1, f_2, ..., f_r$\n",
"2. then g is a generator as long as $g^{\\frac{p1}{fi}} \\neq 1 \\mod(p)$ for $i = 1,2,,...,r$" "2. then $g$ is a generator as long as $g^{\\frac{p1}{f_i}} \\neq 1 \\mod(p)$ for $i = 1,2,,...,r$"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 8,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -331,7 +382,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 9,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -355,7 +406,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 8, "execution_count": 10,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -409,7 +460,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 11,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -482,57 +533,6 @@
" print(f\"{value} is {miller_rabin_prime(value)}.\")\n" " print(f\"{value} is {miller_rabin_prime(value)}.\")\n"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Integer factorisation\n",
"Given an integer, find its prime factors"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"The factors of 641361 are [3, 7, 7, 4363], verified\n",
"Manual test: True\n"
]
}
],
"source": [
"# Brute force solution\n",
"def prime_factors(n):\n",
" i = 2\n",
" factors = []\n",
" while i * i <= n:\n",
" if n % i:\n",
" i += 1\n",
" else:\n",
" n //= i\n",
" factors.append(i)\n",
" if n > 1:\n",
" factors.append(n)\n",
" return factors\n",
"\n",
"# Check if the numbers in array a is the factors of n\n",
"def factor_test(a, n):\n",
" k = 1\n",
" for i in factors:\n",
" k=k*i\n",
" return k==n\n",
"\n",
"n = 641361\n",
"factors = prime_factors(n)\n",
"print(f\"The factors of {n} are {factors}, {'verified' if factor_test(factors, n) else 'ERROR'}\")\n",
"\n",
"print(\"Manual test:\", factor_test([3, 7, 7, 4363], 641361))"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@ -552,7 +552,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 12,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
@ -629,7 +629,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 13,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {