# For the love of Pi

Dr Scripto

Summary: In honor of Pi Day today, we have a guest blog post written by PowerShell MVP, Doug Finke.

It’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’s the ratio of a circle’s circumference to its diameter or “Hey, can I get a slice of that Pi?”.

## Approximate Pi

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 Ï€ was a geometrical approach using polygons, which was devised around 250 BC by the Greek mathematician, Archimedes.

Here we calculate Ï€ to 8 decimal places in only 13 iterations.

# http://www.craig-wood.com/nick/articles/pi-archimedes

function pi_archimedes(\$n) { # Calculate n iterations of Archimedes PI recurrence relation

\$polygon_edge_length_squared = 2.0 \$polygon_sides = 4

if(\$n -gt 0) { 0..(\$n-1) | % { \$polygon_edge_length_squared = 2 – 2 * [math]::sqrt(1 – \$polygon_edge_length_squared / 4) \$polygon_sides *= 2 } }

\$polygon_sides * [math]::sqrt(\$polygon_edge_length_squared) / 2 }

0..15 |% { \$result=pi_archimedes \$_ [pscustomobject]@{ iteration = \$_ sidesÂ Â Â Â  = [math]::pow(2, (\$_+2)) resultÂ Â Â  = \$result errorÂ Â Â Â  = [convert]::ToDecimal((\$result-[math]::PI).ToString(“N9”)) } }

After iteration 13, the estimate of Ï€ starts getting worse. You can improve this by improving the precision of the calculation, which is left as an exercise to the reader.

iteration Â  Â  Â  sides Â  Â  Â result Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â error ——— Â  Â  Â  Â  —– Â  Â  Â  Â —— Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  —– 0 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 4 Â  Â  Â  2.82842712474619 Â  Â -0.313165529 1 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  8 Â  Â  Â  3.06146745892072 Â  -0.080125195 2 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 16 Â  Â  Â  Â 3.12144515225805 Â  Â -0.020147501 3 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 32 Â  Â  Â  3.13654849054594 Â -0.005044163 4 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 64 Â  Â  Â  3.14033115695474 Â  Â -0.001261497 5 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 128 Â  Â  Â  3.14127725093276 Â  -0.000315403 6 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 256 Â  Â  Â  3.14151380114415 Â  Â  -0.000078852 7 Â  Â  Â  Â  Â  Â  Â  Â  Â  Â 512 Â  Â  Â  3.14157294036788 Â  -0.000019713 8 Â  Â  Â  Â  Â  Â  Â  Â  1024 Â  Â  Â  3.14158772527996 Â  Â -0.000004928 9 Â Â  Â  Â  Â  Â  Â  Â  Â 2048 Â  Â  Â  3.14159142150464 Â  Â -0.000001232 10 Â  Â  Â  Â  Â  Â  Â  4096 Â  Â  Â  3.14159234561108 Â  Â -0.000000308 11 Â  Â  Â  Â  Â  Â  Â  Â 8192 Â  Â  Â  3.141592576545 Â  Â  Â  Â  -0.000000077 12 Â  Â  Â  Â  Â  Â  16384 Â  Â  Â  3.14159263346325 Â  Â -0.000000020 13 Â  Â  Â  Â  Â  Â  32768 Â  Â  Â  3.14159265480759 Â  Â -0.000000001 14 Â  Â  Â  Â  Â  Â  65536 Â  Â  Â  3.14159264532122 Â  Â -0.000000008 15 Â  Â  Â  Â  Â  Â 131072 Â  Â  Â  3.14159260737572 Â  Â -0.000000046

## Food lovers

Let’s turn our attention to getting some PiÂ recipes, like apple and cherry.

Here’s a function,Â Get-Pi, that uses the Microsoft Bing API.

Note: You need a Bing API key to make it work.

function Get-Pi{ [CmdletBinding()] param( [Parameter(ValueFromPipeline=\$true)] \$q )

\$Key=\$env:BingPicKey if(!\$env:BingPicKey) { Throw ‘\$env:BingPicKey needs to be set’ }

\$Base64KeyBytes = [Text.Encoding]::ASCII.GetBytes(“ignored:\$Key”) \$Base64Key = [Convert]::ToBase64String(\$Base64KeyBytes) }

Process { \$query=[Web.HttpUtility]::UrlEncode(\$q + ‘ pi’)

\$url = “https://api.datamarket.azure.com/Bing/Search/Web?`\$format=json&Query=’\$query'”

\$r=Invoke-RestMethod \$url -Headers @{ ‘Authorization’ = “Basic \$Base64Key” }

\$r.d.results| % { [PSCustomObject][Ordered]@{ PiType = \$q Description=\$_.description Url = \$_.Url } } } }

In action

Let’s get the Pi recipes for these classics.

echo cherry chicken apple | Get-Pi

Here is a modified version of the output.

PiTypeÂ  Description ——Â  ———– cherryÂ  Bake an all-American Cherry Pie recipe from Food Network using fresh cherryÂ  Directions. Mix ingredients for filling. Place in pastry-lined pie p cherryÂ  Looking for a fruit dessert? Then check out this delicious pie with cherryÂ  Looking for cherry pie recipes? Allrecipes has more than 60 trusted cherryÂ  Summary. UPDATE 13 April 2014 … It rips through the Cherry Pi’s bo chicken Get this all-star, easy-to-follow Chicken Pie recipe from Trisha Yea chicken Use a prepared double-crust pie pastry to help achieve this easy, be chicken Make your leftover chicken into the ultimate comfort food with a few chicken Get this all-star, easy-to-follow Chicken Pot Pie recipe from Ree Dr chicken I found this on another site. It is a fun and different way to serve appleÂ Â  A classic apple pie takes a shortcut with easy PillsburyÂ« unroll-fil appleÂ Â  I remember coming home sullen one day because we’d lost a softball g appleÂ Â  Winners of the Washington Apple Pi Photo Contest 2015 were announced appleÂ Â  This is the apple pie Iâ€™ve been making for years and if using Pillsbu appleÂ Â  For a touch of homegrown comfort, bake Bobby Flay’s classic Apple Pi

## Happy Pi Day

From math to code to search APIs, that’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 Get-BingPics on GitHub. It uses the same approach that weâ€™ve seen, but the output is not text. Instead,Â the output is an HTML document with each image URL embedded in it. Click the following image to open a newÂ window and see Get-BingPics in action.

Thank you, Doug, for an excellent Pi Day blog post.

I invite you to follow me on Twitter and Facebook. IfÂ you have any questions, send email to me at scripter@microsoft.com,Â or post your questions on the Official Scripting Guy Forum.Â Also check out my Microsoft Operations Management Suite Blog.Â See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy