aboutsummaryrefslogtreecommitdiff
path: root/Homework
diff options
context:
space:
mode:
Diffstat (limited to 'Homework')
-rw-r--r--Homework/Homework1.pdfbin0 -> 120157 bytes
-rw-r--r--Homework/Homework2.pdfbin0 -> 107428 bytes
-rw-r--r--Homework/Homework3.ipynb179
-rw-r--r--Homework/Homework3.pdfbin0 -> 146471 bytes
-rw-r--r--Homework/Homework4.ipynb172
-rw-r--r--Homework/Homework4.pdfbin0 -> 116266 bytes
6 files changed, 351 insertions, 0 deletions
diff --git a/Homework/Homework1.pdf b/Homework/Homework1.pdf
new file mode 100644
index 0000000..9a8228a
--- /dev/null
+++ b/Homework/Homework1.pdf
Binary files differ
diff --git a/Homework/Homework2.pdf b/Homework/Homework2.pdf
new file mode 100644
index 0000000..1164333
--- /dev/null
+++ b/Homework/Homework2.pdf
Binary files differ
diff --git a/Homework/Homework3.ipynb b/Homework/Homework3.ipynb
new file mode 100644
index 0000000..13bad44
--- /dev/null
+++ b/Homework/Homework3.ipynb
@@ -0,0 +1,179 @@
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "*For this exercise you should have received this text in .ipynb format. Complete the exercises by modifying this file, and submit the modified version*"
8 ]
9 },
10 {
11 "cell_type": "markdown",
12 "metadata": {},
13 "source": [
14 "**Exercise 1**\n",
15 "\n",
16 "Use SageMath to solve the following problems:\n",
17 "\n",
18 "(a) Find the roots of the following polynomial over $\\mathbb Q$:\n",
19 "\\begin{align*}\n",
20 " p = 4 x^{7} + 4 x^{6} + 3 x^{5} - 13 x^{4} - 13 x^{3} - 9 x^{2} + 3 x + 3 \\in \\mathbb Q[x]\n",
21 "\\end{align*}\n",
22 "\n",
23 "(b) Find the roots of the same polynomial $p$ over $\\mathbb R$ and over $\\mathbb C$.\n",
24 "\n",
25 "(c) Find the determinant, the trace and the characteristic polynomial of the following matrix:\n",
26 "\\begin{align*}\n",
27 "A=\\left(\\begin{array}{rrrr}\n",
28 "-1 & 1 & -1 & 0 \\\\\n",
29 "1 & \\frac{1}{2} & 1 & 0 \\\\\n",
30 "\\frac{1}{2} & -\\frac{1}{2} & -2 & 1 \\\\\n",
31 "0 & 0 & 1 & 1\n",
32 "\\end{array}\\right)\n",
33 "\\end{align*}\n",
34 "\n",
35 "(d) Find a solution to the linear system $A\\mathbf x =\\mathbf v$, where $A$ is the matrix above and $\\mathbf v=(1, 2, 3, 4)$.\n",
36 "\n",
37 "Write your code in the cell below."
38 ]
39 },
40 {
41 "cell_type": "code",
42 "execution_count": null,
43 "metadata": {},
44 "outputs": [],
45 "source": []
46 },
47 {
48 "cell_type": "markdown",
49 "metadata": {},
50 "source": [
51 "**Exercise 2**\n",
52 "\n",
53 "After exchanging messages with the RSA protocol seen in class, Alice and Bob decide to meet and play their favorite game: flip a coin. They like this game very much because it does not take long to set it up and they have exactly the same chances of winning.\n",
54 "\n",
55 "Unfortunately, due to the COVID-19 pandemic they cannot meet in person, and despite being good friends they don't trust each other enough to play this game via Webex call. Luckily, Alice is an expert in cryptography and she knows how to play this game using the Chinese remainder theorem.\n",
56 "\n",
57 "The game plays out as follows:\n",
58 "\n",
59 "(A1) Alice picks two large prime numbers $p$ and $q$, she computes $n=pq$ and sends $n$ to Bob, keeping $p$ and $q$ secret.\n",
60 "\n",
61 "(B1) Bob picks a random number $a$ with $1<a<n$ and $\\gcd(a,n)=1$, computes $b=a^2\\mod n$ and sends $b$ to Alice, keeping $a$ secret.\n",
62 "\n",
63 "(A2) Alice computes two numbers $x$ and $y$ such that $x^2\\equiv b\\pmod p$ and $y^2\\equiv b\\pmod q$ and she uses the Chinese remainder theorem to compute a number $z$ such that $z\\equiv x\\pmod p$ and $z\\equiv y\\pmod q$, so that $z^2\\equiv b\\pmod n$. Then she sends $z$ to Bob.\n",
64 "\n",
65 "Since $n$ is the product of two primes, there are $4$ possible square roots of $b$ modulo $n$, corresponding to the solutions of the four systems of congruences (one for each possible combination of $\\pm$)\n",
66 "\\begin{align*}\\begin{cases}\n",
67 "z\\equiv \\pm x\\pmod p\\\\\n",
68 "z \\equiv \\pm y\\pmod q\n",
69 "\\end{cases}\\end{align*}\n",
70 "\n",
71 "One of those solutions is $a$ and another is $-a$, and Bob knows them. Alice is picking one of the $4$ possible roots at random (she chooses between $x$ and $-x$ and between $y$ and $-y$), so she has $50\\%$ chance of picking one that Bob already knows. This corresponds to Alice flipping a coin, and she wins if she picks $\\pm a$:\n",
72 "\n",
73 "(B2) If $z\\equiv\\pm a\\pmod n$, Bob declares to have lost. Otherwise, Bob claims to have won, and as proof he produces one prime factor of $n$ by computing $g=\\gcd(n,a+z)$. *(One can prove that in this situation $g$ is always one of the two prime factors of $n$.)*\n",
74 "\n",
75 "Since factoring a number without extra information is very hard, Alice will be convinced that she must have given Bob one of the square roots that he did not know, so she admits the loss.\n",
76 "\n",
77 "Now to the actual exercise:\n",
78 "\n",
79 "(a) Write the code for the functions A1, B1 and B2 as indicated in the cell below. The function A2 is already written.\n",
80 "\n",
81 "(b) Modify the functions B1, A2 and B2 to check that the opponent is not cheating. More precisely:\n",
82 "* In B1, Bob should check that $n$ is not a prime power. *(This is the only way Alice can try to cheat: if she sends Bob a number $n$ that is the product of more than two primes, than she has less than $50\\%$ chance of winning!)*\n",
83 "* In A2, Alice should check that $b$ is a square modulo $n$.\n",
84 "* In B2, Bob should check that $z^2\\equiv a^2\\pmod n$.\n",
85 "\n",
86 "In case cheating is detected, a message should be printed saying that the person is cheating."
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": 1,
92 "metadata": {
93 "collapsed": true
94 },
95 "outputs": [
96 {
97 "ename": "IndentationError",
98 "evalue": "expected an indented block (<ipython-input-1-60f510bf175f>, line 7)",
99 "output_type": "error",
100 "traceback": [
101 "\u001b[1;36m File \u001b[1;32m\"<ipython-input-1-60f510bf175f>\"\u001b[1;36m, line \u001b[1;32m7\u001b[0m\n\u001b[1;33m def B1(n):\u001b[0m\n\u001b[1;37m ^\u001b[0m\n\u001b[1;31mIndentationError\u001b[0m\u001b[1;31m:\u001b[0m expected an indented block\n"
102 ]
103 }
104 ],
105 "source": [
106 "# Alice needs this to compute the square roots\n",
107 "from sage.rings.finite_rings.integer_mod import square_root_mod_prime\n",
108 "\n",
109 "def A1():\n",
110 " # This function must return two distinct primes and their product.\n",
111 " \n",
112 "def B1(n):\n",
113 " # This function must return a random integer a\n",
114 " # with 1<a<n and gcd(a,n)=1.\n",
115 "\n",
116 "def A2(b, p, q):\n",
117 " x = ZZ(square_root_mod_prime(Integers(p)(b), p))\n",
118 " y = ZZ(square_root_mod_prime(Integers(q)(b), q))\n",
119 " return crt(x, y, p, q)\n",
120 "\n",
121 "def B2(a, z, n):\n",
122 " # This function must print out one of two messages:\n",
123 " # \"Bob has lost\" if z is congruent to a or -a modulo n.\n",
124 " # \"Bob has won, proof: \" followed by a prime factor of n otherwise.\n",
125 " # In this case the prime must be calculated as explained above.\n",
126 "\n",
127 "\n",
128 "# This is how the game plays out:\n",
129 "p, q, n = A1()\n",
130 "print(\"Alice picked n =\", n)\n",
131 "print(\"[[ Alice's secret:\", p, q, \"]]\")\n",
132 "a = B1(n)\n",
133 "b = a^2 % n\n",
134 "print(\"Bob picked b =\", b)\n",
135 "print(\"[[ Bob's secret:\", a, \"]]\")\n",
136 "z = A2(b, p, q)\n",
137 "print(\"Alice picked z =\", z)\n",
138 "B2(a, z, n)"
139 ]
140 },
141 {
142 "cell_type": "markdown",
143 "metadata": {},
144 "source": [
145 "**Grading**\n",
146 "\n",
147 "This homework assignment is worth $20\\%$ of your final grade. Exercise 1 is worth 4 points (one for each part) and Exercise 2 is worth 12 points (8 points for part (a) and 4 points for part (b)), for a total of **16 points**."
148 ]
149 },
150 {
151 "cell_type": "code",
152 "execution_count": null,
153 "metadata": {},
154 "outputs": [],
155 "source": []
156 }
157 ],
158 "metadata": {
159 "kernelspec": {
160 "display_name": "SageMath 9.2",
161 "language": "sage",
162 "name": "sagemath"
163 },
164 "language_info": {
165 "codemirror_mode": {
166 "name": "ipython",
167 "version": 3
168 },
169 "file_extension": ".py",
170 "mimetype": "text/x-python",
171 "name": "python",
172 "nbconvert_exporter": "python",
173 "pygments_lexer": "ipython3",
174 "version": "3.8.5"
175 }
176 },
177 "nbformat": 4,
178 "nbformat_minor": 4
179}
diff --git a/Homework/Homework3.pdf b/Homework/Homework3.pdf
new file mode 100644
index 0000000..13d55cf
--- /dev/null
+++ b/Homework/Homework3.pdf
Binary files differ
diff --git a/Homework/Homework4.ipynb b/Homework/Homework4.ipynb
new file mode 100644
index 0000000..99481b9
--- /dev/null
+++ b/Homework/Homework4.ipynb
@@ -0,0 +1,172 @@
1{
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {},
6 "source": [
7 "*For this exercise you should have received this text in .ipynb format. Complete the exercises by modifying this file, and submit the modified version*\n",
8 "\n",
9 "**Deadline:** Sunday, June 6."
10 ]
11 },
12 {
13 "cell_type": "markdown",
14 "metadata": {},
15 "source": [
16 "**Exercise 1 (6 points)**\n",
17 "\n",
18 "Use Sage to find the intersection points *in the real plane* (that is, only those points such that *both* coordinates are real numbers) of the following pairs of geometric objects:\n",
19 "\n",
20 "* The circle of equation $x^2 + y^2 = 4$ and the ellipse of equation $\\left(\\frac x2\\right)^2 + (2y)^2 = 4$.\n",
21 "* The circle of equation $x^2 + y^2 = 4$ and the ellipse of equation $\\left(\\frac x2-2\\right)^2 + (2y)^2 = 4$.\n",
22 "* The curve of equation $y^2 = x^3 -x +1$ and the horizontal line $y=10$.\n",
23 "* The $x$-axis and the graph of the function $f(x)=\\log(x) - e^{-x}$. *Hint: $f(x)$ has only one real zero.*"
24 ]
25 },
26 {
27 "cell_type": "code",
28 "execution_count": null,
29 "metadata": {},
30 "outputs": [],
31 "source": []
32 },
33 {
34 "cell_type": "markdown",
35 "metadata": {},
36 "source": [
37 "**Exercise 2 (6 points)**\n",
38 "\n",
39 "(a) Use Sage to compute\n",
40 "* the derivative\n",
41 "* a primite (i.e. integral)\n",
42 "* the power series expansion around $0$ up to order $4$\n",
43 "\n",
44 "of the following functions:\n",
45 "* $f(x) = e^x$\n",
46 "* $f(x) = \\sin(x)$\n",
47 "* $f(x) = \\cos(x)$\n",
48 "* $f(x) = \\tan(x)$\n",
49 "* $f(x) = \\log(1+x)$\n",
50 "* $f(x) = \\sqrt[3]{1+x}$\n",
51 "\n",
52 "(b) Use Sage to get the Latex code that represents the objects you computed above.\n",
53 "\n",
54 "(c) Arrange the results of the previous points in a table in Latex. The table should have 4 columns (function, derivative, integral, series) and one row for each of the functions above. *Note: when including Latex in a Markdown cell in Jupyter you will not receive any warning if you make mistakes; instead the Latex will simply not be rendered and it will appear as plain text. If you have troubles making this work you can send me a separate .tex (and .pdf) file.*"
55 ]
56 },
57 {
58 "cell_type": "code",
59 "execution_count": null,
60 "metadata": {},
61 "outputs": [],
62 "source": [
63 "# Compute derivatives etc..."
64 ]
65 },
66 {
67 "cell_type": "code",
68 "execution_count": null,
69 "metadata": {},
70 "outputs": [],
71 "source": [
72 "# Compute Latex code"
73 ]
74 },
75 {
76 "cell_type": "markdown",
77 "metadata": {},
78 "source": [
79 "(Write your table here)"
80 ]
81 },
82 {
83 "cell_type": "markdown",
84 "metadata": {},
85 "source": [
86 "**Exercise 3 (4 points)**\n",
87 "\n",
88 "The equation\n",
89 "\\begin{align*}\n",
90 "y^2+x^{16}=1\n",
91 "\\end{align*}\n",
92 "determines a closed curve in $\\mathbb R^2$ that looks like a rounded square. Determine the area of that shape, giving both an exact value (which might depend on some functions that Sage knows, but you don't) and an approximate value."
93 ]
94 },
95 {
96 "cell_type": "code",
97 "execution_count": null,
98 "metadata": {},
99 "outputs": [],
100 "source": []
101 },
102 {
103 "cell_type": "markdown",
104 "metadata": {},
105 "source": [
106 "**Exercise 4 (12 points)**\n",
107 "\n",
108 "A team of biologists is monitoring the population of river shrimps in the Alzette. At first they thought that the size $P(t)$ of their population on day $t$ would satisfy the differential equation $P'(t)=P(t)/10$. However this does not work well with the data they have collected, so they now believe that the population of shrimps follows the formula $P'(t)=P(t)/10-b$ for some value of $b$ between 1 and 100. They need your help here.\n",
109 "\n",
110 "(a) Using Sage, find a solution for the differential equation with initial conditions\n",
111 "\\begin{align*}\n",
112 "\\begin{cases}\n",
113 "P'(t)&=\\frac{P(t)}{10}-b\\\\\n",
114 "P(1)&=1000\n",
115 "\\end{cases}\n",
116 "\\end{align*}\n",
117 "where $b$ is a generic constant.\n",
118 "\n",
119 "(b) The list `data` in the cell below contains the actual number of shrimps that was measured every day from day $1$ (the $0$ at the beginning is meaningless, but it will help to keep it there). Plot in one single picture, possibly using different colors for each:\n",
120 "* The data as a bar chart.\n",
121 "* A curve that interpolates the data, using one of the methods shown in class.\n",
122 "* The solution of the differential equation for $b=0$.\n",
123 "* The solution of the differential equation for a value of $b$ of your choice ($1\\leq b\\leq 100$) that fits the data better than $b=0$. *(For this last point there is no right or wrong choice, just pick one that looks good)*"
124 ]
125 },
126 {
127 "cell_type": "code",
128 "execution_count": null,
129 "metadata": {},
130 "outputs": [],
131 "source": [
132 "data = [0, 1000, 1123, 1223, 1190, 1432, 1553, 1709, 1826, 1980, 2146, 2172, 2383, 2588, 2822, 3401, 3330, 4157, 3994, 4995, 5392, 5910, 6468, 7128, 7325, 7984, 9634, 10473, 11761, 12777]\n"
133 ]
134 },
135 {
136 "cell_type": "markdown",
137 "metadata": {},
138 "source": [
139 "**Grading**\n",
140 "\n",
141 "This homework assignment is worth $28$ ($24+4$) points, distributed as described above.\n",
142 "\n",
143 "Your final grade for the course will be the total of points you obtained (notice that the maximum is $20+20+16+28=84$) divided by $4$, rounded to the nearest integer. More precisely\n",
144 "\n",
145 "\\begin{align*}\n",
146 "\\operatorname{grade} = \\operatorname{min}\\left(20, \\left\\lfloor \\frac{\\operatorname{total}}{4} + 0.5\\right\\rfloor\\right)\n",
147 "\\end{align*}"
148 ]
149 }
150 ],
151 "metadata": {
152 "kernelspec": {
153 "display_name": "SageMath 9.0",
154 "language": "sage",
155 "name": "sagemath"
156 },
157 "language_info": {
158 "codemirror_mode": {
159 "name": "ipython",
160 "version": 3
161 },
162 "file_extension": ".py",
163 "mimetype": "text/x-python",
164 "name": "python",
165 "nbconvert_exporter": "python",
166 "pygments_lexer": "ipython3",
167 "version": "3.8.5"
168 }
169 },
170 "nbformat": 4,
171 "nbformat_minor": 4
172}
diff --git a/Homework/Homework4.pdf b/Homework/Homework4.pdf
new file mode 100644
index 0000000..ff8ecd2
--- /dev/null
+++ b/Homework/Homework4.pdf
Binary files differ

Generated with cgit - Back to sebastiano.tronto.net