{"id":8871,"date":"2024-08-16T14:36:28","date_gmt":"2024-08-16T13:36:28","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=8871"},"modified":"2024-09-02T15:51:23","modified_gmt":"2024-09-02T14:51:23","slug":"calculate-pi","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/calculate-pi\/","title":{"rendered":"Calculate PI"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large is-resized\"><img loading=\"lazy\" decoding=\"async\" width=\"996\" height=\"1024\" src=\"https:\/\/solidt.eu\/site\/wp-content\/uploads\/2024\/08\/image-8-996x1024.png\" alt=\"\" class=\"wp-image-8872\" style=\"width:840px;height:auto\" srcset=\"https:\/\/solidt.eu\/site\/wp-content\/uploads\/2024\/08\/image-8-996x1024.png 996w, https:\/\/solidt.eu\/site\/wp-content\/uploads\/2024\/08\/image-8-292x300.png 292w, https:\/\/solidt.eu\/site\/wp-content\/uploads\/2024\/08\/image-8-768x790.png 768w, https:\/\/solidt.eu\/site\/wp-content\/uploads\/2024\/08\/image-8.png 1071w\" sizes=\"auto, (max-width: 996px) 100vw, 996px\" \/><\/figure>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">function calculatePi(precision) {\n    let total = (precision + 1) * (precision + 1); \/\/ Inclusief alle punten\n    let outside = 0;\n    let xP, yP;\n\n    \/\/ Loop alleen over de helft van de waarden van x (symmetrie gebruiken)\n    for (let x = precision; x >= 0; x--) {\n        xP = x \/ precision;\n\n        for (let y = precision; y >= 0; y--) {\n            if (x >= y) break;  \/\/ Itereer alleen over de onderste helft van x\n            yP = y \/ precision;\n\n            if (xP * xP + yP * yP > 1.0) {\n                outside++;\n            } else {\n                break; \/\/ Zodra een punt binnen de cirkel is gevonden, kunnen we stoppen\n            }\n        }\n    }\n\n    \/\/ Correcte telling van het aantal binnenliggende punten\n    let inside = total - (outside * 2);\n\n    \/\/ Bereken Pi\n    return (inside \/ total) * 4; \/\/ De ratio van binnen tot totale punten, vermenigvuldigd met 4\n}\n\n\nconsole.log('calcated  = ', calculatePi(300000));\nconsole.log('PI        = ', Math.PI);\nconsole.log('355 \/ 113 = ', 355 \/ 113);\nconsole.log('1-9       = ', 3 + 4 \/ 28 - 1 \/ (790 + 5 \/ 6))<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"Gauss-Legendre\">Gauss-Legendre algoritm<\/h2>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"javascript\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">(() => {\n\t\/\/ Gauss-Legendre-algoritm\n    function calculatePi(iterations) {\n        let a = 1.0;\n        let b = 1.0 \/ Math.sqrt(2);\n        let t = 1.0 \/ 4.0;\n        let p = 1.0;\n\n        for (let i = 0; i &lt; iterations; i++) {\n            const a_next = (a + b) \/ 2.0;\n            const b_next = Math.sqrt(a * b);\n            t -= p * Math.pow(a - a_next, 2);\n            p *= 2.0;\n            a = a_next;\n            b = b_next;\n        }\n\n        const pi = Math.pow(a + b, 2) \/ (4 * t);\n        return pi;\n    }\n\n    const iterations = 5; \/\/ Slechts een paar iteraties nodig\n    const piEstimate = calculatePi(iterations);\n    console.log(`Geschatte waarde van Pi met ${iterations}`);\n\tconsole.log(`Waarde: ${piEstimate}`);\n\tconsole.log(`PI:     ${Math.PI}`);\n})();<\/pre><\/div>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"lua\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">-- Gauss-Legendre-algoritme\nfunction calculatePi(iterations)\n    local a = 1.0\n    local b = 1.0 \/ math.sqrt(2)\n    local t = 1.0 \/ 4.0\n    local p = 1.0\n\n    for i = 1, iterations do\n        local a_next = (a + b) \/ 2.0\n        local b_next = math.sqrt(a * b)\n        t = t - p * math.pow(a - a_next, 2)\n        p = p * 2.0\n        a = a_next\n        b = b_next\n    end\n\n    local pi = math.pow(a + b, 2) \/ (4.0 * t)\n    return pi\nend\n\nlocal iterations = 5 -- Slechts een paar iteraties nodig\nlocal piEstimate = calculatePi(iterations)\nprint(string.format(\"Geschatte waarde van Pi met %d iteraties: %.15f\", iterations, piEstimate))\nprint(string.format(\"Werkelijke waarde van Pi: %.15f\", math.pi))<\/pre><\/div>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">#r \"nuget: NBitcoin, 7.0.37\"\nusing System;\nusing NBitcoin;\n\nclass Program\n{\n    static void Main()\n    {\n        int precision = 100; \/\/ Aantal decimalen van \u03c0\n        int iterations = 10; \/\/ Aantal iteraties van het Gauss-Legendre algoritme\n\n        BigDecimal a = new BigDecimal(1, precision);\n        BigDecimal b = BigDecimal.One \/ BigDecimal.Sqrt(new BigDecimal(2, precision), precision);\n        BigDecimal t = new BigDecimal(0.25m, precision);\n        BigDecimal p = BigDecimal.One;\n\n        for (int i = 0; i &lt; iterations; i++)\n        {\n            BigDecimal aNext = (a + b) \/ 2;\n            b = BigDecimal.Sqrt(a * b, precision);\n            t -= p * BigDecimal.Pow(a - aNext, 2);\n            a = aNext;\n            p *= 2;\n        }\n\n        BigDecimal pi = (a + b) * (a + b) \/ (t * 4);\n\n        Console.WriteLine($\"\u03c0 \u2248 {pi.ToString().Substring(0, precision + 2)}\"); \/\/ Precision + 2 to include \"3.\"\n    }\n}\n<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Gauss-Legendre algoritm<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8871","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=8871"}],"version-history":[{"count":9,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8871\/revisions"}],"predecessor-version":[{"id":8901,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/8871\/revisions\/8901"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=8871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=8871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=8871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}