Linq Trivia - Calculating PI with a single line of code

I recently had an interview where I was given the task of calculating the mathematical constant PI. I was going to do it in a single line of code (to show off my Linq prowess), but thought the interviewer would have considered me a bit of a geek, so I fleshed it out to his expectations.

But heres the line anyway

var pi = Enumerable.Range(0, 100000000).Aggregate(0d, (tot, next) => tot += Math.Pow(-1d, next)/(2*next + 1)*4);

Not bad !!

Dean

Pingbacks and trackbacks (2)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About the author

I am a senior .NET contract (freelance) software developer specialising in WPF and WinRT application development with C#, F#, C++. I mainly work in the Investment Bnking industry building performant and robust user interfaces for front office and middle office systems

RecentComments

Comment RSS

Month List