Revision 3e3ce425-cd27-4b83-9afd-be571f1a1263 - Code Golf Stack Exchange

# [Jelly][1], <s>12</s> 8 bytes

 RÆṪSḤ’÷²

[Try it online!][2]

The following binary code works with [this version of the Jelly interpreter][3].

 0000000: 52 91 b0 53 aa b7 9a 8a R..S....

### Idea

Clearly, the number of pairs **(j, k)** such that **j &leq; k** and **j** and **k** are co-prime equals the number of pairs **(k, j)** that satisfy the same conditions. Also, if **j = k**, **j = 1 = k**.

Thus, to count the number of co-prime pairs with coordinates between **1** and **n**, it suffices to calculate the amount **m** of pairs **(j, k)** such that **j &leq; k**, then compute **2m - 1**.

Finally, since Euler's totient function **&phi;(k)** yields the number integers between between **1** and **k** that are co-prime to **k**, we can calculate **m** as **&phi;(1) + &hellip; + &phi;(n)**.

### Code

 RÆṪSḤ’÷² Input: n

 R Yield [1, ..., n].
 ÆṪ Apply Euler's totient function to each k in [1, ..., n].
 S Compute the sum of all results.
 Ḥ Multiply the result by 2.
 ’ Subtract 1.
 ÷² Divide the result by n².

[1]: http://github.com/DennisMitchell/jelly
[2]: http://jelly.tryitonline.net/#code=UsOG4bmqU-G4pOKAmcO3wrI&input=&args=MTAwMA
[3]: https://github.com/DennisMitchell/jelly/tree/0ea6f804d01b9da15070c77e8e7ba3ffa0f9d114

AltStyle によって変換されたページ (->オリジナル) /