EKG Sequence

Time limit1sMemory limit128 MB

Summary
Build the EKG sequence up to position 1000000 and report the 1-based position where each queried integer n first appears.
Level

Medium6 of 10

Topics
Number theory, Brute force, Implementation, Math
Solved
No attempts yet

Problem

The EKG sequence is a sequence of positive integers built as follows. The first two terms are 1 and 2. Each later term is the smallest positive integer that has not been used yet and that shares a common factor greater than 1 with the immediately preceding term (that is, it is not coprime with it). So the third term is 4 (the smallest even number not yet used); the next is 6, and the next is 3. The first few terms of the sequence are:

1, 2, 4, 6, 3, 9, 12, 8, 10, 5, 15, 18, 14, 7, 21, 24, 16, 20, 22, 11, 33, 27

The sequence is named after an EKG (electrocardiogram) because of its erratic up-and-down fluctuations. It has a few interesting but non-trivial properties: every positive integer eventually appears in the sequence, and all primes appear in increasing order. Your task is to find the position of a given integer in the sequence.

Input

The input consists of several test cases. Each case is a line containing a single integer nn (1≤n≤3000001 \le n \le 300000). A line containing 0 follows the last test case. The prefix of the EKG sequence that contains every integer up to 300,000 does not contain any integer greater than 1,000,000.

Output

For each test case, print one line in the following format:

The number n appears in location p.

where nn is the given number and pp is the position at which it appears in the EKG sequence. It is guaranteed that pp will be no larger than 1,000,000.

Examples4

  1. Example 1

    Input
    12
    21
    2
    33
    100000
    299977
    0
    
    Expected output
    The number 12 appears in location 7.
    The number 21 appears in location 15.
    The number 2 appears in location 2.
    The number 33 appears in location 21.
    The number 100000 appears in location 97110.
    The number 299977 appears in location 584871.
    
  2. Example 2

    Input
    1
    0
    
    Expected output
    The number 1 appears in location 1.
    
  3. Example 3

    Input
    2
    3
    5
    7
    11
    13
    17
    19
    0
    
    Expected output
    The number 2 appears in location 2.
    The number 3 appears in location 5.
    The number 5 appears in location 10.
    The number 7 appears in location 14.
    The number 11 appears in location 20.
    The number 13 appears in location 28.
    The number 17 appears in location 33.
    The number 19 appears in location 37.
    
  4. Example 4

    Input
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    0
    
    Expected output
    The number 1 appears in location 1.
    The number 2 appears in location 2.
    The number 3 appears in location 5.
    The number 4 appears in location 3.
    The number 5 appears in location 10.
    The number 6 appears in location 4.
    The number 7 appears in location 14.
    The number 8 appears in location 8.
    The number 9 appears in location 6.
    The number 10 appears in location 9.