我使用 Python/Numpy 中的函数来解决组合博弈论中的问题。
\nimport numpy as np\nfrom time import time\n\ndef problem(c):\n start = time()\n N = np.array([0, 0])\n U = np.arange(c)\n \n for _ in U:\n bits = np.bitwise_xor(N[:-1], N[-2::-1])\n N = np.append(N, np.setdiff1d(U, bits).min())\n\n return len(*np.where(N==0)), time()-start \n\nproblem(10000)\n
Run Code Online (Sandbox Code Playgroud)\n然后我用 Julia 编写它,因为我认为由于 Julia 使用即时编译,它会更快。
\nfunction problem(c)\n N = [0]\n U = Vector(0:c)\n \n for _ in U\n elems = N[1:length(N)-1]\n bits = elems .\xe2\x8a\xbb reverse(elems)\n push!(N, minimum(setdiff(U, bits))) \n end\n \n return sum(N .== 0)\nend\n\n@time problem(10000)\n …
Run Code Online (Sandbox Code Playgroud)