Skip to content

Commit

Permalink
added an example of modular exponentiation
Browse files Browse the repository at this point in the history
  • Loading branch information
jet08013 committed May 30, 2018
1 parent 688d0c5 commit 8ff3f88
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 20 deletions.
82 changes: 82 additions & 0 deletions ctnt2018/ModularExponentiation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def mexp(a,x,N):\n",
" m,s=1,a\n",
" while x>0:\n",
" print('{0:5d} {1:5d} {2:10d}'m,s,x)\n",
" if x % 2 ==1:\n",
" m=((m*s) % N)\n",
" s=((s*s) % N)\n",
" x=x//2\n",
" return m"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 5 123\n",
"5 25 61\n",
"125 625 30\n",
"125 21028 15\n",
"41321 15573 7\n",
"23556 62697 3\n",
"104119 3316 1\n"
]
},
{
"data": {
"text/plain": [
"55006"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mexp(5,123,123199)"
]
},
{
"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": 2
}
20 changes: 0 additions & 20 deletions ctnt2018/PminusOneMethod.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -127,26 +127,6 @@
"show_factors(640)"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1869036133"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mexp(5,factorial(10),2**32+1))-1 % (2**32+1)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 8ff3f88

Please sign in to comment.