{"id":77514,"date":"2016-03-14T00:01:15","date_gmt":"2016-03-14T07:01:15","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/?p=77514"},"modified":"2019-02-18T09:10:53","modified_gmt":"2019-02-18T16:10:53","slug":"for-the-love-of-pi","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/for-the-love-of-pi\/","title":{"rendered":"For the love of Pi"},"content":{"rendered":"<p><strong>Summary<\/strong>: In honor of Pi Day today, we have a guest blog post written by PowerShell MVP, Doug Finke.<\/p>\n<p><figure id=\"attachment_77523\" aria-labelledby=\"figcaption_attachment_77523\" class=\"wp-caption alignnone\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/PI1small.png\"><img decoding=\"async\" class=\"wp-image-77523 size-full\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/PI1small.png\" alt=\"Image of apple pie with Pi character\" width=\"254\" height=\"197\" \/><\/a><figcaption id=\"figcaption_attachment_77523\" class=\"wp-caption-text\">Photo courtesy of Ed Wilson<\/figcaption><\/figure><\/p>\n<p>It&#8217;s that time of year again when lovers of math, geometry, Albert Einstein, food, and Windows PowerShell can stop and reflect on that transcendental irrational number, Pi. It&#8217;s the ratio of a circle&#8217;s circumference to its diameter or &#8220;Hey, can I get a slice of that Pi?&#8221;.<\/p>\n<h2>Approximate Pi<\/h2>\n<p>The best we can do is approximate Pi, 22\/7 (go ahead, type that into a PowerShell console). The first recorded algorithm for rigorously calculating the value of \u03c0 was a geometrical approach using polygons, which was devised around 250 BC by the Greek mathematician, Archimedes.<\/p>\n<p>Here we calculate \u03c0 to 8 decimal places in only 13 iterations.<\/p>\n<p style=\"padding-left: 30px\"># http:\/\/www.craig-wood.com\/nick\/articles\/pi-archimedes<\/p>\n<p style=\"padding-left: 30px\">function pi_archimedes($n) {\n# Calculate n iterations of Archimedes PI recurrence relation<\/p>\n<p style=\"padding-left: 30px\">$polygon_edge_length_squared = 2.0\n$polygon_sides = 4<\/p>\n<p style=\"padding-left: 30px\">if($n -gt 0) {\n0..($n-1) | % {\n$polygon_edge_length_squared = 2 &#8211; 2 * [math]::sqrt(1 &#8211; $polygon_edge_length_squared \/ 4)\n$polygon_sides *= 2\n}\n}<\/p>\n<p style=\"padding-left: 30px\">$polygon_sides * [math]::sqrt($polygon_edge_length_squared) \/ 2\n}<\/p>\n<p style=\"padding-left: 30px\">0..15 |% {\n$result=pi_archimedes $_\n[pscustomobject]@{\niteration = $_\nsides\u00a0\u00a0\u00a0\u00a0 = [math]::pow(2, ($_+2))\nresult\u00a0\u00a0\u00a0 = $result\nerror\u00a0\u00a0\u00a0\u00a0 = [convert]::ToDecimal(($result-[math]::PI).ToString(&#8220;N9&#8221;))\n}\n}<\/p>\n<p>After iteration 13, the estimate of \u03c0 starts getting worse. You can improve this by improving the precision of the calculation, which is left as an exercise to the reader.<\/p>\n<p>iteration \u00a0 \u00a0 \u00a0 sides \u00a0 \u00a0 \u00a0result \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0error\n&#8212;&#8212;&#8212; \u00a0 \u00a0 \u00a0 \u00a0 &#8212;&#8211; \u00a0 \u00a0 \u00a0 \u00a0&#8212;&#8212; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &#8212;&#8211;\n0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a04 \u00a0 \u00a0 \u00a0 2.82842712474619 \u00a0 \u00a0-0.313165529\n1 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 8 \u00a0 \u00a0 \u00a0 3.06146745892072 \u00a0 -0.080125195\n2 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a016 \u00a0 \u00a0 \u00a0 \u00a03.12144515225805 \u00a0 \u00a0-0.020147501\n3 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a032 \u00a0 \u00a0 \u00a0 3.13654849054594 \u00a0-0.005044163\n4 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a064 \u00a0 \u00a0 \u00a0 3.14033115695474 \u00a0 \u00a0-0.001261497\n5 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0128 \u00a0 \u00a0 \u00a0 3.14127725093276 \u00a0 -0.000315403\n6 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0256 \u00a0 \u00a0 \u00a0 3.14151380114415 \u00a0 \u00a0 -0.000078852\n7 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0512 \u00a0 \u00a0 \u00a0 3.14157294036788 \u00a0 -0.000019713\n8 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 1024 \u00a0 \u00a0 \u00a0 3.14158772527996 \u00a0 \u00a0-0.000004928\n9 \u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a02048 \u00a0 \u00a0 \u00a0 3.14159142150464 \u00a0 \u00a0-0.000001232\n10 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 4096 \u00a0 \u00a0 \u00a0 3.14159234561108 \u00a0 \u00a0-0.000000308\n11 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a08192 \u00a0 \u00a0 \u00a0 3.141592576545 \u00a0 \u00a0 \u00a0 \u00a0 -0.000000077\n12 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 16384 \u00a0 \u00a0 \u00a0 3.14159263346325 \u00a0 \u00a0-0.000000020\n13 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 32768 \u00a0 \u00a0 \u00a0 3.14159265480759 \u00a0 \u00a0-0.000000001\n14 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 65536 \u00a0 \u00a0 \u00a0 3.14159264532122 \u00a0 \u00a0-0.000000008\n15 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0131072 \u00a0 \u00a0 \u00a0 3.14159260737572 \u00a0 \u00a0-0.000000046<\/p>\n<h2>Food lovers<\/h2>\n<p>Let&#8217;s turn our attention to getting some <strong><em>Pi<\/em><\/strong>\u00a0recipes, like apple and cherry.<\/p>\n<p><figure id=\"attachment_77533\" aria-labelledby=\"figcaption_attachment_77533\" class=\"wp-caption alignnone\" ><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/P3comboSmall.png\"><img decoding=\"async\" class=\"wp-image-77533 size-medium\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/P3comboSmall-300x120.png\" alt=\"Image of apple and cherry pies\" width=\"300\" height=\"120\" \/><\/a><figcaption id=\"figcaption_attachment_77533\" class=\"wp-caption-text\">Photo courtesy of Ed Wilson<\/figcaption><\/figure><\/p>\n<p>Here&#8217;s a function,\u00a0<strong>Get-Pi<\/strong>, that uses the Microsoft Bing API.<\/p>\n<p><strong>Note:<\/strong> You need a <a href=\"https:\/\/datamarket.azure.com\/home\" target=\"_blank\">Bing API key<\/a> to make it work.<\/p>\n<p style=\"padding-left: 30px\">function Get-Pi{\n[CmdletBinding()]\nparam(\n[Parameter(ValueFromPipeline=$true)]\n$q\n)<\/p>\n<p style=\"padding-left: 30px\">Begin {\nAdd-Type -Assembly System.Web<\/p>\n<p style=\"padding-left: 30px\">$Key=$env:BingPicKey\nif(!$env:BingPicKey) {\nThrow &#8216;$env:BingPicKey needs to be set&#8217;\n}<\/p>\n<p style=\"padding-left: 30px\">$Base64KeyBytes = [Text.Encoding]::ASCII.GetBytes(&#8220;ignored:$Key&#8221;)\n$Base64Key = [Convert]::ToBase64String($Base64KeyBytes)\n}<\/p>\n<p style=\"padding-left: 30px\">Process {\n$query=[Web.HttpUtility]::UrlEncode($q + &#8216; pi&#8217;)<\/p>\n<p style=\"padding-left: 30px\">$url = &#8220;https:\/\/api.datamarket.azure.com\/Bing\/Search\/Web?`$format=json&amp;Query=&#8217;$query'&#8221;<\/p>\n<p style=\"padding-left: 30px\">$r=Invoke-RestMethod $url -Headers @{ &#8216;Authorization&#8217; = &#8220;Basic $Base64Key&#8221; }<\/p>\n<p style=\"padding-left: 30px\">$r.d.results| % {\n[PSCustomObject][Ordered]@{\nPiType = $q\nDescription=$_.description\nUrl = $_.Url\n}\n}\n}\n}<\/p>\n<p>In action<\/p>\n<p>Let&#8217;s get the <strong><em>Pi<\/em><\/strong> recipes for these classics.<\/p>\n<p style=\"padding-left: 30px\">echo cherry chicken apple | Get-Pi<\/p>\n<p class=\"SourceCode\">Here is a modified version of the output.<\/p>\n<p>PiType\u00a0 Description\n&#8212;&#8212;\u00a0 &#8212;&#8212;&#8212;&#8211;\ncherry\u00a0 Bake an all-American Cherry Pie recipe from Food Network using fresh\ncherry\u00a0 Directions. Mix ingredients for filling. Place in pastry-lined pie p\ncherry\u00a0 Looking for a fruit dessert? Then check out this delicious pie with\ncherry\u00a0 Looking for cherry pie recipes? Allrecipes has more than 60 trusted\ncherry\u00a0 Summary. UPDATE 13 April 2014 &#8230; It rips through the Cherry Pi&#8217;s bo\nchicken Get this all-star, easy-to-follow Chicken Pie recipe from Trisha Yea\nchicken Use a prepared double-crust pie pastry to help achieve this easy, be\nchicken Make your leftover chicken into the ultimate comfort food with a few\nchicken Get this all-star, easy-to-follow Chicken Pot Pie recipe from Ree Dr\nchicken I found this on another site. It is a fun and different way to serve\napple\u00a0\u00a0 A classic apple pie takes a shortcut with easy Pillsbury\u00ab unroll-fil\napple\u00a0\u00a0 I remember coming home sullen one day because we&#8217;d lost a softball g\napple\u00a0\u00a0 Winners of the Washington Apple Pi Photo Contest 2015 were announced\napple\u00a0\u00a0 This is the apple pie I\u2019ve been making for years and if using Pillsbu\napple\u00a0\u00a0 For a touch of homegrown comfort, bake Bobby Flay&#8217;s classic Apple Pi<\/p>\n<h2>Happy Pi Day<\/h2>\n<p>From math to code to search APIs, that&#8217;s a quick tour for today. I encourage you to tour the Internet and read up on this magical number, and as a bonus, here is <strong>Get-BingPics<\/strong> on <a href=\"https:\/\/gist.github.com\/dfinke\/4ab9927eab082a8ef4b1#file-get-bingpics-ps1\" target=\"_blank\">GitHub<\/a>. It uses the same approach that we\u2019ve seen, but the output is not text. Instead,\u00a0the output is an HTML document with each image URL embedded in it. Click the following image to open a new\u00a0window and see <strong>Get-BingPics<\/strong> in action.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/GetBingPics.gif\" target=\"_blank\"><img decoding=\"async\" class=\"alignnone wp-image-77543 size-medium\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/GetBingPics-300x167.gif\" alt=\"Click the image to see animation of Get-BingPics results.\" width=\"300\" height=\"167\" \/><\/a><\/p>\n<p>Thank you, Doug, for an excellent Pi Day blog post.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If\u00a0you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>,\u00a0or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guy Forum<\/a>.\u00a0Also check out my <a href=\"https:\/\/blogs.technet.microsoft.com\/msoms\/\" target=\"_blank\">Microsoft Operations Management Suite Blog<\/a>.\u00a0See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: In honor of Pi Day today, we have a guest blog post written by PowerShell MVP, Doug Finke. It&#8217;s that time of year again when lovers of math, geometry, Albert Einstein, food, and Windows PowerShell can stop and reflect on that transcendental irrational number, Pi. It&#8217;s the ratio of a circle&#8217;s circumference to its [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[568],"tags":[78,56,3,45],"class_list":["post-77514","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-hey-scripting-guy","tag-doug-finke","tag-guest-blogger","tag-scripting-guy","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: In honor of Pi Day today, we have a guest blog post written by PowerShell MVP, Doug Finke. It&#8217;s that time of year again when lovers of math, geometry, Albert Einstein, food, and Windows PowerShell can stop and reflect on that transcendental irrational number, Pi. It&#8217;s the ratio of a circle&#8217;s circumference to its [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77514","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=77514"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/77514\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=77514"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=77514"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=77514"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}