Skip to content

Instantly share code, notes, and snippets.

@foxqstm
Created September 19, 2021 16:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foxqstm/5c4b2d564f917487dbb11a65163ed002 to your computer and use it in GitHub Desktop.
Save foxqstm/5c4b2d564f917487dbb11a65163ed002 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 238,
"id": "da2da064",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 43, 47, 59, 61, 67, 71, 73, 79, 83, 107, 109, 113, 1433, 2297, 2309, 2311, 2339, 2341, 2917, 4933, 4937, 4943, 4951, 4957, 4973, 5069, 5077, 5081, 5417, 5419, 5431, 5503, 5513, 5897, 5903, 5923, 5927, 5987, 6203, 7643, 7649, 7841, 7853, 7901, 7933, 7937, 7949, 7951, 7963, 9403, 9419, 9421, 9781, 10069, 10453, 10463, 10499, 10501, 10513, 31249, 31253, 31259, 31267, 31271, 31783, 32359, 32363, 32369, 32371, 32377, 32381, 32387, 32443, 32491, 32497, 32689, 32693, 32833, 33073, 33169, 33233, 33401, 33403, 33427, 296083, 297523, 297527, 297623, 297659, 297707, 297709, 297733, 297757, 297761, 297763, 297779, 297811, 297907, 297943]\n",
"Find next prime: 50.91 %\n",
"Find true prime: 52.73 %\n"
]
}
],
"source": [
"import random\n",
"import sympy\n",
"\n",
"def predict(p,plist):\n",
" n_iter=30\n",
" n_t=30\n",
" qlist=[]\n",
" q=p+2\n",
" \n",
" BOOL=True\n",
" for rr in range(1,n_iter):\n",
" if not BOOL:\n",
" break\n",
" rlist=[]\n",
" for pp in plist:\n",
" rlist.append(pp)\n",
" n_test=min(len(rlist),n_t)\n",
" rclist=random.sample(rlist,n_test)\n",
" \n",
" for r in rclist: \n",
" if q%r==0:\n",
" BOOL=False\n",
" break\n",
" if BOOL:\n",
" qlist.append(q)\n",
" \n",
"# return qlist\n",
"\n",
" w=[]\n",
" for pp in plist:\n",
" w.append(1/pp**3)\n",
" for r in range(1,n_iter):\n",
" qclist=random.choices(plist,k=r,weights=w)\n",
"\n",
" prod=1\n",
" for qp in qclist:\n",
" prod*=qp\n",
" q=p+2*prod\n",
"\n",
" rlist=[]\n",
" for pp in plist:\n",
" rlist.append(pp)\n",
" \n",
" for qp in set(qclist):\n",
" rlist.remove(qp)\n",
" \n",
" BOOL=True\n",
" for rr in range(1,n_iter):\n",
" if not BOOL:\n",
" break\n",
" n_test=min(len(rlist),n_t)\n",
" rclist=random.sample(rlist,n_test)\n",
" for r in rlist:\n",
" if q%r==0:\n",
" BOOL=False\n",
" break\n",
" if BOOL:\n",
" qlist.append(q)\n",
"\n",
" return qlist\n",
"\n",
"plist=[2,3,5,7,11,13,17,19,23,29]\n",
"Nmax=100\n",
"for n in range(Nmax):\n",
" qlist=predict(plist[-1],plist)\n",
" qlist.sort()\n",
" if qlist==[]:\n",
" break\n",
" plist.append(qlist[0])\n",
" plist.sort()\n",
"print(plist)\n",
"\n",
"pmax=max(plist)\n",
"count=0\n",
"for n in range(len(plist)-3):\n",
" p_T=list(sympy.primerange(plist[n]+1, min(3*plist[n],pmax)))\n",
" if p_T[0]==plist[n+1]:\n",
" count+=1\n",
" \n",
"print('Find next prime:',round(count/len(plist)*100,2),'%')\n",
"\n",
"p_T=list(sympy.primerange(plist[n]+1, pmax))\n",
"for p in plist:\n",
" if p in p_T:\n",
" count+=1\n",
" \n",
"print('Find true prime:',round(count/len(plist)*100,2),'%')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.11"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment