Newest questions tagged pi number-theory - Code Golf Stack Exchange most recent 30 from codegolf.stackexchange.com 2020-01-27T13:46:27Z https://codegolf.stackexchange.com/feeds/tag?tagnames=pi+number-theory&sort=newest https://creativecommons.org/licenses/by-sa/4.0/rdf https://codegolf.stackexchange.com/q/67773 23 Co-primality and the number pi Luis Mendo https://codegolf.stackexchange.com/users/36398 2015-12-26T16:16:32Z 2017-04-01T13:38:54Z <h1>Introduction</h1> <p>Number theory is full of wonders, in the form of unexpected connections. Here's one of them.</p> <p>Two integers are <a href="https://en.wikipedia.org/wiki/Coprime_integers">co-prime</a> if they have no factors in common other than 1. Given a number <em>N</em>, consider all integers from 1 to <em>N</em>. Draw two such integers <em>at random</em> (all integers have the same probability of being selected at each draw; draws are independent and with replacement). Let <em>p</em> denote the probability that the two selected integers are co-prime. Then <em>p</em> tends to 6/<em>π</em><sup>2</sup> ≈ 0.6079... as <em>N</em> tends to infinity.</p> <h1>The challenge</h1> <p>The purpose of this challenge is to compute <em>p</em> as a function of <em>N</em>.</p> <p>As an example, consider <em>N</em> = 4. There are 16 possible pairs obtained from the integers 1,2,3,4. 11 of those pairs are co-prime, namely (1,1), (1,2), (1,3), (1,4), (2,1), (3,1), (4,1), (2,3), (3,2), (3,4), (4,3). Thus <em>p</em> is 11/16 = 0.6875 for <em>N</em> = 4.</p> <p>The <em>exact</em> value of <em>p</em> needs to be computed with at least <em>four</em> decimals. This implies that the computation has to be deterministic (as opposed to Monte Carlo). But it need not be a direct enumeration of all pairs as above; any method can be used.</p> <p>Function arguments or stdin/stdout may be used. If displaying the output, trailing zeros may be omitted. So for example <code>0.6300</code> can be displayed as <code>0.63</code>. It should be displayed as a decimal number, not as a fraction (displaying the string <code>63/100</code> is not allowed).</p> <p>Winning criterion is fewest bytes. There are no restrictions on the use of built-in functions.</p> <h1>Test cases</h1> <p>Input / output (only four decimals are mandatory, as indicated above):</p> <pre><code>1 / 1.000000000000000 2 / 0.750000000000000 4 / 0.687500000000000 10 / 0.630000000000000 100 / 0.608700000000000 1000 / 0.608383000000000 </code></pre>