Happy Pi Approximation Day!

Thread Starter

xox

Joined Sep 8, 2017
495
Here's one of my favorite ways to do it. The probability that two randomly selected integers are coprime is 6/π^2.

Code:
#include <stdio.h>
#include <math.h>

#ifndef __unix__
#warning /dev/urandom may not be available on this system!
#endif

typedef unsigned long ulong;

ulong sample()
{
 static FILE* stream = NULL;
 if(!stream)
  stream = fopen("/dev/urandom", "rb");
 ulong result;
 fread(&result, 1, sizeof(result), stream);
 return result;
}

ulong gcd(ulong n1,ulong n2)
{
 if(!n2)
  return n1;
 return gcd(n2,n1 % n2);
}

int main(void)
{
 ulong coprime = 0, total = 0;
 for(;;)
 {
  if(gcd(sample(),sample()) == 1)
   ++coprime;
  double estimate = 1 / sqrt(coprime/(6 * (double)++total));
  printf(" %.10g\n", estimate);
 }
}
 

ZCochran98

Joined Jul 24, 2018
88
I find it interesting that that probability is exactly equal to \( \left[\sum_{n=1}^{\infty}\frac{1}{n^2}\right]^{-1} \), or \(\zeta(2)^{-1}\). Pretty neat relationship, I must say (an explanation of why the two are related can be found here, for the curious).
 

ZCochran98

Joined Jul 24, 2018
88
\( \pi \) is "infinite," to use your term. More accurately/precisely, \( \pi \) is a transcendental irrational number. It is irrational because it cannot be represented by a ratio of two integers, and it is transcendental because it cannot be represented as the root of a real polynomial with rational coefficients.

I'm not entirely sure what you mean by "in a state of self-approximation in perpetuity." \( \pi \) can be approximated or even stated exactly via other means. An approximation of an irrational number (transcendental or otherwise) or repeating rational number is just a way to get a "close-enough" value to use it for computational purposes. For instance, a common way to approximate \( \pi \) is with the ratio 22/7, which only gets 3 digits of \( \pi \), or you can outright say \( \pi \) is roughly 3.14159265, just like an approximation of another transcendental number, Euler's number e, is 2.718281828, and irrational numbers like the square root of 2 (1.414213562) or the golden ratio (1.618033989).

There are also exact representations of them, and usually quite a few. For instance, there are dozens of ways to represent \( \pi \) exactly, with one of my favorites being Chudnovsky's formula, which generates around 14 digits of \( \pi \) per term:
\[ \frac{1}{\pi} = \frac{12}{640320^{3/2}}\sum_{k=0}^\infty\frac{\left(6k\right)!\left(13591409+545140134k\right)}{\left(3k\right)!\left(k!\right)^3\left(-640320\right)^{3k}} \]
 

ZCochran98

Joined Jul 24, 2018
88
I never said any such thing.
I was referring to a post that has since been deleted, apparently. When I posted my statement, there was another post by another user. That post has been deleted since I replied, from the looks of it. Apologies for the confusion!
 

ZCochran98

Joined Jul 24, 2018
88
I was referring to a post that has since been deleted, apparently. When I posted my statement, there was another post by another user. That post has been deleted since I replied, from the looks of it. Apologies for the confusion!
To clarify somewhat, the question I was answering was something along the lines of "is pi infinite or is it in a state of self-approximation in perpetuity?" Because it was deleted, that may not be word-for-word, obviously.
 
\( \pi \) is "infinite," to use your term. More accurately/precisely, \( \pi \) is a transcendental irrational number. It is irrational because it cannot be represented by a ratio of two integers, and it is transcendental because it cannot be represented as the root of a real polynomial with rational coefficients.

I'm not entirely sure what you mean by "in a state of self-approximation in perpetuity." \( \pi \) can be approximated or even stated exactly via other means. An approximation of an irrational number (transcendental or otherwise) or repeating rational number is just a way to get a "close-enough" value to use it for computational purposes. For instance, a common way to approximate \( \pi \) is with the ratio 22/7, which only gets 3 digits of \( \pi \), or you can outright say \( \pi \) is roughly 3.14159265, just like an approximation of another transcendental number, Euler's number e, is 2.718281828, and irrational numbers like the square root of 2 (1.414213562) or the golden ratio (1.618033989).

There are also exact representations of them, and usually quite a few. For instance, there are dozens of ways to represent \( \pi \) exactly, with one of my favorites being Chudnovsky's formula, which generates around 14 digits of \( \pi \) per term:
\[ \frac{1}{\pi} = \frac{12}{640320^{3/2}}\sum_{k=0}^\infty\frac{\left(6k\right)!\left(13591409+545140134k\right)}{\left(3k\right)!\left(k!\right)^3\left(-640320\right)^{3k}} \]

My apologies, I did in fact delete the post — I considered after the fact that I didn't want to detract from the original poster's emphasis on "happy pi day."

I am in agreement with your response, but there are those who claim "pi" is finite, and that the decimal expansion has no bearing on the actual underlying value of it, and that it is just 1 point on the real number line. (As an aside, I feel this is incongruous, and one of the reasons I believe the real number line cannot be logically reckoned as "a single line," but more as two lines super-positioned, since most numbers in it have 2 informational dimensions (the integer + the fractional addendum). The mantissa of a rational or irrational needs to be represented by more than one point. What I meant by "self-approximation in perpetuity": pi's irrationality/transcendent non-integer component is an a state of approaching 0 as a fractional "addendum" to 3... i.e., some might only need 3 and 14159/100,000's, whereas NASA might need 3 + 1,415,926,535/10,000,000,000's — but there's an unresolved state of approximating that fractional element.

How many digits of the mantissa would be required in base-10 to actually claim "pi is in use?"
 
Last edited:

ZCochran98

Joined Jul 24, 2018
88
I considered after the fact that I didn't want to detract from the original poster's emphasis on "happy pi day."
Your reason makes sense why your original message was deleted. As such, this will be the last thing I post to this thread (for the same reason you gave), just to answer your question.

It really depends on how much precision you needed, I suppose. When doing extraordinarily-precise measurements to determine physical constants, for instance, with extreme detail, you may need 10 digits or more. If you're doing an approximation of something on a "back-of-the-napkin" calculation, it may even be sufficient to say "pi is about 3." There's not really a mantissa requirement to specify "pi is in use," except for the context of the situation, as pi is representative of an individual, exact number - it is a singular piece of information.

Food for thought: Whole numbers plus mantissa elements are, really, only of use for computational purposes for application, and are simply for representational purposes to represent an individual quantity. The use of a whole number plus mantissa is also dependent on scale, as on one scale, your whole number could become a mantissa, and on another scale, it could become an entirely whole number (for instance, in physics we frequently scale everything relative to planck's constant or the speed of light, effectively "setting" both to 1, to simplify calculations, and the concept of angular frequency is to scale up normal frequency in terms of 2*pi -> 2*pi*rotations per time unit = radians traversed per time unit).

I'm not going to address the real-numbers-as-a-plane bit, as there's another entire thread devoted to that you started a few days ago. However, just as a suggestion, I think you should really talk to a mathematician on that subject, if you're super-invested into the idea, as they would be able to give better insight as to the validity of that idea (whether valid or invalid), discuss the ramifications of such an idea (especially in mathematics and operations on real numbers), and make an argument either way in regards to it far better than I could.

And to keep it on topic: another fun (but very slow) way to approximate pi is Buffon's Needle experiment - a kind of Monte Carlo method.
 
Top