All problems

Problem 546

Count Coprime Pairs

Medium · 20 pts · Math, Number Theory, Sieve

Problem

Count the pairs (a, b) with 1 <= a < b <= N such that gcd(a, b) = 1. For a fixed b, the number of a < b that are coprime to b is Euler's totient φ(b) (for b >= 2). So the answer is φ(2) + φ(3) + ... + φ(N). Compute all totients at once with a sieve: start with φ(i) = i, and for every prime p multiply φ of each multiple of p by (1 - 1/p). The numbers in this problem can be larger than a 32-bit int can hold (about 2.1 × 10^9), so use a 64-bit integer type: long long in C++, long in Java (Python needs nothing special).

Input

A single integer N.

Output

Print the number of coprime pairs.

Constraints

1 <= N <= 1000000

Example input

5

Example output

9

Explanation

For N = 5 the coprime pairs are (1,2), (1,3), (1,4), (1,5), (2,3), (2,5), (3,4), (3,5) and (4,5). Only (2,4) shares a factor, so the answer is 9.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Pythagorean Triplets Count Next: Steps to Reduce a Binary Number to One