Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.
Code Golf

Return to Answer

added 8 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

Idea

Clearly, the number of pairs (j, k) such that j ≤ 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) withsuch that j ≤ k, then compute 2m - 1.

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

Code

RÆṪSḤ’÷2 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.
 ÷2 Divide the result by n2.

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

Idea

Clearly the number of pairs (j, k) such that j ≤ 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) with j ≤ k, then compute 2m - 1.

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

Code

RÆṪSḤ’÷2 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.
 ÷2 Divide the result by n2.

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

Idea

Clearly, the number of pairs (j, k) such that j ≤ 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 ≤ k, then compute 2m - 1.

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

Code

RÆṪSḤ’÷2 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.
 ÷2 Divide the result by n2.
added 350 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

How it worksIdea

Clearly the number of pairs (j, k) such that j ≤ 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) with j ≤ k, then compute 2m - 1.

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

Code

RÆṪSḤ’÷2 Input: n
R Yield [1, ..., n].
 ÆṪ Apply Euler's totient function to each k in [1, ..., n].
 This yields the number of coprimes to k, less than or equal to k.
 S Compute the sum of all results.
 This yields the number of coprime pairs (j, k) such that j ≤ k.
 Ḥ Multiply the result by 2.
 This accounts for all (k, j) pairs, too.
 ’ Subtract 1 to account for counting (1, 1) twice.
 ÷2 Divide the result by n2.

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

How it works

RÆṪSḤ’÷2 Input: n
R Yield [1, ..., n].
 ÆṪ Apply Euler's totient function to each k in [1, ..., n].
 This yields the number of coprimes to k, less than or equal to k.
 S Compute the sum of all results.
 This yields the number of coprime pairs (j, k) such that j ≤ k.
 Ḥ Multiply the result by 2.
 This accounts for all (k, j) pairs, too.
 ’ Subtract 1 to account for counting (1, 1) twice.
 ÷2 Divide the result by n2.

Jelly, (削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

Idea

Clearly the number of pairs (j, k) such that j ≤ 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) with j ≤ k, then compute 2m - 1.

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

Code

RÆṪSḤ’÷2 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.
 ÷2 Divide the result by n2.
deleted 135 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830

JellyJelly,(削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Rest to be updated. Try it online!


Jelly , 12 bytes

Rb‘xgR=1SS÷2

Try it online!

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

0000000: 52 62 b6 78 67 52 3d91 31b0 53 53aa b7 9a 8a RbR.xgR=1SS.S....

How it works

Rb‘xgR=1SS÷2RÆṪSḤ’÷2  Input: n
R Yield [1, ..., n].
 b‘ÆṪ Apply Euler's Converttotient eachfunction to baseeach n+1.k Yieldsin [[1][1, ..., [n]]n].
 x This Repeatyields eachthe nnumber times.of Yieldscoprimes [[1,to ...k, 1],less ...,than [n,or ...,equal n]]to k.
 gRS TakeCompute GCDthe withsum [1,of ...,all n]results.
 This yields allthe n2number GCDsof ascoprime apairs 10x10(j, arrayk) such that j ≤ k.
  =1 CheckMultiply forthe equalityresult withby 12.
 S This Computeaccounts thefor sumall of(k, allj) columnspairs, too.
  S Subtract 1 Computeto theaccount sumfor ofcounting all(1, sums1) twice.
 ÷2 ÷2 Divide the result by n2.

Jelly, 8 bytes

RÆṪSḤ’÷2

Rest to be updated. Try it online!


Jelly , 12 bytes

Rb‘xgR=1SS÷2

Try it online!

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

0000000: 52 62 b6 78 67 52 3d 31 53 53 9a 8a Rb.xgR=1SS..

How it works

Rb‘xgR=1SS÷2 Input: n
R Yield [1, ..., n].
 b‘ Convert each to base n+1. Yields [[1], ..., [n]].
 x Repeat each n times. Yields [[1, ..., 1], ..., [n, ..., n]].
 gR Take GCD with [1, ..., n].
 This yields all n2 GCDs as a 10x10 array.
 =1 Check for equality with 1.
 S Compute the sum of all columns.
 S Compute the sum of all sums.
 ÷2 Divide the result by n2.

Jelly,(削除) 12 (削除ここまで) 8 bytes

RÆṪSḤ’÷2

Try it online!

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

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

How it works

RÆṪSḤ’÷2  Input: n
R Yield [1, ..., n].
 ÆṪ Apply Euler's totient function to each k in [1, ..., n].
 This yields the number of coprimes to k, less than or equal to k.
 S Compute the sum of all results.
 This yields the number of coprime pairs (j, k) such that j ≤ k.
  Multiply the result by 2.
 This accounts for all (k, j) pairs, too.
  Subtract 1 to account for counting (1, 1) twice.
 ÷2 Divide the result by n2.
added 170 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading
deleted 14 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading
added 547 characters in body
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading
Source Link
Dennis
  • 211.7k
  • 41
  • 380
  • 830
Loading

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