Skip to main content

Let's calculate pi using SQL

It's Pi Day! Let's calculate pi using ClickHouse SQL

It's Pi Day! Let's calculate pi using SQL

Happy Pi Day! We thought it would be fun to calculate pi using SQL queries in ClickHouse. Here is what we came up with so far...

  1. This one uses the ClickHouse numbers_mt table function to return 1B rows and only takes 40ms to compute the calculation:
  1. The following example also processes 1B numbers, just not as quickly:
  1. This one is obviously our favorite in ClickHouse (and the most accurate!):
  1. Someone knew their trigonometry with this one:
  1. Here is a handy API that lets you specify the number of digits you want:
  1. This one is clever - it uses ClickHouse distance functions:
  1. If you're a physicist, you will be content with this one:
  1. Another indirect mehthod (this one came from Alexey Milovidov) that is accurate to 7 decimal places - and it's quick:
Note

If you have any more, we'd love for you to contribute. Thanks!

· 3 min read