{"id":1295,"date":"2008-04-29T01:42:16","date_gmt":"2008-04-28T17:42:16","guid":{"rendered":"https:\/\/www.highflybird.com\/blog\/?p=132"},"modified":"2020-02-29T22:56:38","modified_gmt":"2020-02-29T14:56:38","slug":"pi%e7%9a%84%e9%ab%98%e7%b2%be%e5%ba%a6%e8%ae%a1%e7%ae%97","status":"publish","type":"post","link":"https:\/\/www.highflybird.com\/blog\/?p=1295","title":{"rendered":"PI\u7684\u9ad8\u7cbe\u5ea6\u8ba1\u7b97"},"content":{"rendered":"<p>\u672c\u7a0b\u5e8f\u91c7\u7528LISP\u8ba1\u7b97\u9ad8\u7cbe\u5ea6Pi\u7684\u6570\u503c\uff0c\u53ef\u8ba1\u7b97\u5230\u5c0f\u6570\u70b9\u540e16000\u4f4d\u3002<br \/>\n<!--more--><\/p>\n<pre lang=\"lisp\" line=\"1\" escaped=\"true\">;;;----------------------------------------------------;\n;;;highflybird    2008.4.20 Haikou                     ;\n;;;\u91c7\u7528LISP\u8ba1\u7b97\u9ad8\u7cbe\u5ea6Pi\u7684\u6570\u503c\uff0c\u53ef\u8ba1\u7b97\u5230\u5c0f\u6570\u70b9\u540e16000\u4f4d ;\n;;;\u6d4b\u8bd5\u8fd0\u884c\u90e8\u5206                                        ;\n;;;----------------------------------------------------;\n(defun c:test (\/ digits i n Result t1 t2 time str FILE KEY PATH)\n  (setq digits 10000)                                   ;\u6bcf\u6b21\u8f93\u51fa\u4f4d\u6570\n  (initget 7)                                           ;\u975e\u96f6\u975e\u8d1f\u975e\u7a7a\n  (setq n (getint \"nn\u8bf7\u8f93\u5165\u7cbe\u5ea6&lt;\u4e0d\u8d85\u8fc716000&gt;:\"))      ;\u8f93\u5165\u7cbe\u5ea6\n  (if (&gt; n 16000)                               \n    (alert \"\u8f93\u5165\u8d85\u8fc716000!\")                                            \n    (progn  \n      (setq t1     (getvar \"TDUSRTIMER\"))               ;\u5f00\u59cb\u8ba1\u65f6\n      (setq Result (CalPi digits n))                    ;\u8ba1\u7b97Pi\u503c\n      (setq t2     (getvar \"TDUSRTIMER\"))               ;\u8ba1\u65f6\u7ed3\u675f\n      (setq time   (* 86400 (- t2 t1)))\n      (princ \"n\u8ba1\u7b97Pi\u5171\u7528\u65f6(\u79d2):\")                           \n      (princ time)                                      ;\u548c\u6240\u8017\u65f6\u95f4\n      (initget 1 \"Yes No\")\n      (setq key (getkword \"n\u8981\u6253\u5370\u5230\u6587\u4ef6\u5417[\u662f(Yes)\/\u5426(No)]\uff1a\"))\n      (if (and (= key \"Yes\")\n               (setq path (getfiled \"\u8f93\u5165\u6587\u4ef6\u540d:\" \"c:\/\" \"txt\" 1))\n          )\n        (progn\n          (setq file (open path \"w\"))\n          (princ \"3n.141\" file)                        ;\u8fd9\u4e2a\u662f\u4e3a\u4e86\u6253\u5370\u9700\u8981\n          (setq i 2)\n          (foreach n (cdr Result)\n            (setq str (itoa n))\n            (while (&lt; (strlen str) 4)\n              (setq str (strcat \"0\" str))\n            )\n            (princ str file)\n            (if (= (rem i 25) 0)\n              (princ (strcat \"    --\" (itoa (* i 4)) \"n\") file)\n            )\n            (setq i (1+ i))\n          )\n          (close file)\n          (startapp \"Notepad\" path)\n        )\n      )\n    )\n  )    \n  (princ)                                               ;\u9000\u51fa\n)\n\n;;;----------------------------------------------------;\n;;;highflybird    2008.4.20 Haikou                     ;\n;;;\u91c7\u7528LISP\u8ba1\u7b97\u9ad8\u7cbe\u5ea6Pi\u7684\u6570\u503c\uff0c\u53ef\u8ba1\u7b97\u5230\u5c0f\u6570\u70b9\u540e16000\u4f4d ;\n;;;\u51fd\u6570\u6c42\u503c\u90e8\u5206                                        ;\n;;;----------------------------------------------------;\n(defun CalPi (digits n \/ b c d e f g h r s x)\n  (setq c (\/ (1+ n) (\/ (log 2) (log 10))))              ;\u9700\u8981\u8fed\u4ee3\u7684\u6b21\u6570\n  (setq c (fix c))                                      ;\u8f6c\u5316\u4e3a\u6574\u6570\n  (setq e 0 r nil)                                      ;\u5b58\u50a8\u7ed3\u679c\u7684\u5b57\u7b26\u4e32\u8d4b\u7a7a\u503c\n  (setq h (\/ digits 5))                                 ;\u4ece\u5c0f\u6570\u540e\u7b97\u8d77\n  (repeat c                                     \n    (setq f (cons h f))                                 ;\u521d\u59cb\u4f59\u6570\u4e3a10000 * 2 \/ 10\n  )\n  (repeat (1+ (\/ n 4))                                  ;\u91cd\u590d1+ 800\/4 = 201\u6b21\n    (setq d 0)                                          ;\u6bcf\u6b21\u672b\u4f4d\u5c0f\u6570\u4e3a0\n    (setq g (+ c c))                                    ;\u5206\u6bcd\u3002\u56e0\u4e3a\u6bcf\u6b21\u5faa\u73af\u90fd\u8f93\u51fa\u4e864\u4f4d\uff0c\u6240\u4ee5\u5728\u540e\u9762\u8fd0\u7b97\u65f6\u4e58\u4ee5\u4e86a\uff0c\u6240\u4ee5\u8fd9\u91cc\u5f97 -2\n    (setq b c)                                          ;\u5206\u5b50\n    (setq x nil)\n    (while (&gt; b 0)\n      ;;\u6839\u636e\u516c\u5f0f\uff0c\u4e58\u4ee5\u5206\u5b50\n      (setq d (* d b))                                  \n      (setq b (1- b))\n      (setq d (+ d (* (car f) digits)))                 ;\u56e0\u4e3a\u6bcf\u6b21\u5916\u5faa\u73af\u90fd\u8f93\u51fa\u4e864\u4f4d\n      ;;\u6839\u636e\u516c\u5f0f\uff0c\u9664\u4ee5\u5206\u6bcd\n      (setq f (cdr f))\n      (setq g (1- g))\n      (setq x (cons (rem d g) x))                       ;\u5e26\u5206\u6570\u7684 \u5206\u5b50\u90e8\u5206\n      (setq d (\/ d g))                                  ;\u5e26\u5206\u6570\u7684 \u6574\u6570\u90e8\u5206\n      (setq g (1- g))\n    )\n    (setq f (reverse x))\n    (repeat 13                                          \n      (setq f (cdr f))\n    )\n    (setq s (+ e (\/ d digits)))                         ;printf(\"%.4d\", e+d\/a);\n    (setq r (cons s r))                                 ;\u7b97\u51fa\u7684\u6bcf\u4e00\u9879\uff0c\u6ce8\u610f\u8868\u7684\u6bcf\u9879\u5982\u679c\u4e0d\u8db34\u4f4d\u8981\u52a0\u96f6\u8865\u5168\n    (setq e (rem d digits))                             ;e = d % a;\n    (setq c (- c 13))                                   ;\u7cbe\u5ea6\u56fa\u5b9a\u4e3a800\u4f4d,\u6bcf\u8f93\u51fa4\u4f4d\u540e,\u76f8\u5f53\u4e8e\u7cbe\u5ea6\u9700\u6c42\u964d\u4f4e\u4e864\u4f4d,\u6545\u6bcf\u6b21\u53ef\u5c11\u7b9713\u9879\n  )\n  (reverse r)                                           ;\u628a\u8868\u9879\u53cd\u8f6c\n)  \n \n\n;;;----------------------------------------------------;\n;;;highflybird    2008.4.20 Haikou                     ;\n;;;\u91c7\u7528LISP\u8ba1\u7b97\u9ad8\u7cbe\u5ea6e\u7684\u6570\u503c\uff0c\u53ef\u8ba1\u7b97\u5230\u5c0f\u6570\u70b9\u540e16000\u4f4d  ;\n;;;\u51fd\u6570\u6c42\u503c\u90e8\u5206                                        ;\n;;;----------------------------------------------------;\n(defun CalE (a c \/ b d e f p s x)\n  (setq b 0 e 0 P \"\")\n  (repeat (1+ c)\n    (setq f (cons a f))\n  )\n  (while (&gt; c 0)\n    (setq d 0)\n    (setq b c)\n    (setq x nil)\n    (while (&gt; b 0)    \n      (setq d (+ d (* (car f) a)))\n      (setq f (cdr f))\n      (setq x (cons (rem d b) x))\n      (setq d (\/ d b))\n      (setq b (1- b))\n    )\n   \n    (setq f (reverse x))\n    (repeat 14\n      (setq f (cdr f))\n    )\n    (setq c (- c 14))\n    (setq s (+ e (\/ d a)))\n    (setq s (itoa s))\n    (while (&lt; (strlen s) 4)\n      (setq s (strcat \"0\" s))\n    )\n    (setq P (strcat p s))\n    (setq e (rem d a))\n  )  \n  p\n)\n\n;|\u4ee5\u4e0b\u662fC\u7684\u6e90\u4ee3\u7801:                                      \n--------------------------------------------------------\nfor(;b-c;)                                              \nf[b++]=a;                                               \nfor(;d=0,g=c;c-=14,printf(\"%.4d \",e+d\/a),e=d%a)         \nfor(b=c;d+=f[b]*a,f[b]=d%b,d\/=b,--b;);                  \n--------------------------------------------------------\n|;;                                                     \n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u7a0b\u5e8f\u91c7\u7528LISP\u8ba1\u7b97\u9ad8\u7cbe\u5ea6Pi\u7684\u6570\u503c\uff0c\u53ef\u8ba1\u7b97\u5230\u5c0f\u6570\u70b9\u540e16000\u4f4d\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ngg_post_thumbnail":0,"footnotes":""},"categories":[9],"tags":[12],"class_list":["post-1295","post","type-post","status-publish","format-standard","hentry","category-programming","tag-pi-"],"_links":{"self":[{"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1295","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1295"}],"version-history":[{"count":0,"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1295\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1295"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1295"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.highflybird.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1295"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}