Farey Sequence

The Farey sequence of order n is the sequence of completely reduced fractions between 0 and 1 which, when in lowest terms, have denominators less than or equal to n, arranged in ascending order. Farey sequence for different values of n are shown in the figure on the left below: {} F 1 = 01 , 1 1 {} F4 = 01,14,13,12,23,34,1 {} F7 = 01,17,16,15,14,27,31,25,73,12,47,35,23,57,43,45,56,67,1 Figure 1: Figure 2: Five desired pairs in F4 It is very well known that if m1 and m2 and are two consecutive fractions of a Farey Sequence n1 n2 then m2n1 − m1n2 = 1. But many fractions which are not consecutive also show this property. For example, in F7, 52 and 12 also show this property although they are not consecutive fractions in F7. Given the value of n, your job is to find number of pair of non-consecutive fractions mi and mj , such thatmjni−minj =1. Input ni nj Input file contains at most 20000 lines of input. Each line contains a positive integer which denotes the value of n (0 < n < 1000001). Input is terminated by a line containing a single zero. This line should not be processed. Output For each line of input produce one line of output. This line contains number of pair of non-consecutive fractions mi and mj,(j−i>1)inFareySeriesFn,suchthatmjni−minj =1. ni nj Sample Input 1 4 0 Sample Output 0 5