Skip to content

Instantly share code, notes, and snippets.

@unaoya
Created July 21, 2018 07:27
Show Gist options
  • Save unaoya/880730000fb7967a1b27a799562c00ea to your computer and use it in GitHub Desktop.
Save unaoya/880730000fb7967a1b27a799562c00ea to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"平方剰余記号"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def Leg(n, p):\n",
" if pow(n, (p-1)//2, p) == 1:\n",
" return 1\n",
" elif pow(n, (p-1)//2, p) == (p - 1):\n",
" return -1\n",
" else:\n",
" return 0"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"$\\sin(\\dfrac{k\\pi}{p})$を計算"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"import math\n",
"def sin(k,p):\n",
" return math.sin(math.pi*k/p)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6180339887498948"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 5\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6180339887498949"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-1+math.sqrt(5))/2"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.3027756377319947"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 13\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.30277563773199456"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-3+math.sqrt(13))/2"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.12310562561766056"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 17\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Pell方程式の解を総当たりで求める\n",
"$$\n",
"x^2-py^2=\\pm4\n",
"$$"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 1 -4\n",
"3 1 4\n",
"4 2 -4\n",
"7 3 4\n"
]
}
],
"source": [
"p=5\n",
"for x in range(1,10):\n",
" for y in range(1,10):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3 1 -4\n",
"11 3 4\n",
"36 10 -4\n"
]
}
],
"source": [
"p=13\n",
"n=50\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"8 2 -4\n",
"66 16 4\n"
]
}
],
"source": [
"p=17\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.12310562561766059"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-8+2*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.19258240356725195"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 29\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5 1 -4\n",
"27 5 4\n"
]
}
],
"source": [
"p=29\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.19258240356725187"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-5+1*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0827625302982197"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 37\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"12 2 -4\n"
]
}
],
"source": [
"p=37\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-12+2*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = 41\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p=41\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-64+10*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = 53\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p=53\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-7+1*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = 61\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p=61\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-39+5*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.00029239763581977255"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"p = 229\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15 1 -4\n"
]
}
],
"source": [
"p=229\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.06637297521077823"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"(-15+1*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.0002923976358197762"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"((-15+1*math.sqrt(p))/2)**3"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p = 257\n",
"prod = 1\n",
"for k in range(1, p//2 + 1):\n",
" prod = prod * (sin(k, p) ** Leg(k, p))\n",
"prod"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"p=257\n",
"n=100\n",
"for x in range(1,n):\n",
" for y in range(1,n):\n",
" if x**2 - p*y**2 == 4:\n",
" print(x,y,4)\n",
" elif x**2 - p*y**2 == -4:\n",
" print(x,y,-4)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"(-32+2*math.sqrt(p))/2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.6.5"
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment