{"id":6162,"date":"2015-04-26T17:25:24","date_gmt":"2015-04-26T09:25:24","guid":{"rendered":"http:\/\/jpuyy.com\/?p=6162"},"modified":"2022-01-13T11:04:36","modified_gmt":"2022-01-13T03:04:36","slug":"calculate-pi","status":"publish","type":"post","link":"https:\/\/jpuyy.com\/?p=6162","title":{"rendered":"pi\u8ba1\u7b97"},"content":{"rendered":"<p>\u8ba1\u7b97pi<\/p>\n<p>c\u8bed\u8a00\u7248<\/p>\n<pre>#include &lt;stdio.h&gt;\r\n\r\nint main() {\r\n    int r[2800 + 1];\r\n    int i, k;\r\n    int b, d;\r\n    int c = 0;\r\n\r\n    for (i = 0; i &lt; 2800; i++) {\r\n        r[i] = 2000;\r\n    }\r\n\r\n    for (k = 2800; k &gt; 0; k -= 14) {\r\n        d = 0;\r\n\r\n        i = k;\r\n        for (;;) {\r\n            d += r[i] * 10000;\r\n            b = 2 * i - 1;\r\n\r\n            r[i] = d % b;\r\n            d \/= b;\r\n            i--;\r\n            if (i == 0) break;\r\n            d *= i;\r\n        }\r\n        printf(\"%.4d\", c + d \/ 10000);\r\n        c = d % 10000;\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>gcc pi.c -o pi<\/p>\n<p>\u7528 shell \u7684 bc \u547d\u4ee4\u8ba1\u7b97\u5f88\u65b9\u4fbf<\/p>\n<pre>   EXAMPLES\r\n       In \/bin\/sh,  the following will assign the value of \"pi\" to the shell variable pi.\r\n               pi=$(echo \"scale=10; 4*a(1)\" | bc -l)\r\n<\/pre>\n<p>\u6d4b\u8bd5\u8bb0\u5f55<\/p>\n<pre>\r\nbash\r\ntime pi=$(echo \"scale=10000; 4*a(1)\" | bc -l)\r\n\r\n# a is arctan, and this give pi to 10000 digits.\r\n\r\n<\/pre>\n<p>2022-01-13 MacBook Pro (14-inch, 2021) Apple M1 Max<br \/>\nreal\t0m50.483s<br \/>\nuser\t0m49.896s<br \/>\nsys\t0m0.179s<\/p>\n<p>2020-10-27 MacBook Pro (16-inch, 2019) 2.6 GHz 6-Core Intel Core i7<br \/>\nreal\t1m17.887s<br \/>\nuser\t1m17.080s<br \/>\nsys\t0m0.184s<\/p>\n<p>2020-11-21 Mac mini (2018) 3.2 GHz 6-Core Intel Core i7<br \/>\nreal\t1m8.796s<br \/>\nuser\t1m8.647s<br \/>\nsys\t0m0.066s<\/p>\n<p>2020-11-21 windows (WSL) centos7 AMD \u9510\u9f995 3600X \u5904\u7406\u5668 (r5)7nm 6\u683812\u7ebf\u7a0b 3.8GHz 95W AM4\u63a5\u53e3 \u76d2\u88c5CPU<\/p>\n<p>yum install bc<br \/>\nreal    1m16.686s<br \/>\nuser    1m16.609s<br \/>\nsys     0m0.047s<\/p>\n<p>2019? MacBook Pro (15-inch, 2017) \u8017\u65f6 real\t1m56.849s<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8ba1\u7b97pi c\u8bed\u8a00\u7248 #include &lt;stdio.h&gt; int main() { int r[2800 + 1]; int i, k; int b, d; int c = 0; for (i = 0; i &lt; 2800; i++) { r[i] = 2000; } for (k = 2800; k &gt; 0; k -= 14) { d = 0; i = k; for (;;) { d [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[],"class_list":["post-6162","post","type-post","status-publish","format-standard","hentry","category-life"],"_links":{"self":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6162","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6162"}],"version-history":[{"count":17,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6162\/revisions"}],"predecessor-version":[{"id":9448,"href":"https:\/\/jpuyy.com\/index.php?rest_route=\/wp\/v2\/posts\/6162\/revisions\/9448"}],"wp:attachment":[{"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6162"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6162"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jpuyy.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6162"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}