Skip to content

Instantly share code, notes, and snippets.

@foxqstm
Created September 19, 2021 16:16
Show Gist options
  • Save foxqstm/f7173b688c20037adb9ba7c934383849 to your computer and use it in GitHub Desktop.
Save foxqstm/f7173b688c20037adb9ba7c934383849 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 236,
"id": "2021d64b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 47, 79, 127, 191, 193, 197, 199, 359, 383, 431, 433, 439, 443, 467, 479, 487, 491, 499, 4099, 4111, 4133, 4157, 4159, 4219, 4223, 4271, 4273, 4337, 4339, 4343, 4919, 4967, 4969, 4973, 18797, 18901, 19093, 19141, 19153, 19441, 19457, 19463, 19469, 19471, 19727, 19729, 19777, 19781, 19783, 20071, 20359, 20551, 20567, 20569, 20593, 20599, 21271, 73111, 73123, 73127, 73133, 73141, 73237, 73309, 73693, 73709, 73961, 73963, 74003, 74147, 74149, 74197, 74203, 74779, 74923, 75043, 76003, 76099, 76103, 76157, 76159, 76831, 77551, 77563, 77569, 77573, 77897, 77899, 77963, 77971, 77983, 78751, 78847, 79279, 79283, 79307, 79309, 79333, 79337]\n",
"Find next prime: 38.18 %\n",
"Find true prime: 40.0 %\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