Search code examples
algorithmnumber-theorylcm

From 1 to N how many pairs have LCM = N?


Suppose N = 8. There are 4 pairs (1,8),(2,8),(4,8),(8,8) whose LCM is 8. If N = 6. Then there are 5 pairs (1,6),(2,6),(2,3),(3,6),(6,6) whose LCM is 6. Now I want to know how to find the number of pairs quickly?


Solution

  • The question "Pairs of numbers with at given LCM" at math.stackexchange.com gives the formula

    ((2e1+1)(2e2+1)...(2ek+1)+1)/2
            where e1, e2, ... is the exponents for the unique prime factors of n
    

    for this number.

    i.e.
    8 = 2^3 has ((2*3+1)+1)/2 = 4 such pairs,
    6 = 2^1 * 3^1 has ((2*1+1)(2*1+1)+1)/2 = 5 such pairs, and
    60 = 2^2 * 3^1 * 5^1 has ((2*2+1)(2*1+1)(2*1+1)+1)/2 = 23 such pairs.