A pair of numbers has a unique LCM but a single number can be the LCM of more than one possible
pairs. For example `12` is the LCM of `(1, 12), (2, 12), (3,4)` etc. For a given positive integer N, the number of different integer pairs with LCM is equal to N can be called the LCM cardinality of that number N. In this kata your job is to find out the LCM cardinality of a number.

Source: codewars | Difficulty: EASY
Tags: Fundamentals

Starter code: def lcm_cardinality(n):
	

Solutions:
from itertools import combinations
from math import gcd

def lcm_cardinality(n):
	return 1 + sum((1 for (a, b) in combinations(divisors(n), 2) if lcm(a, b) == n))

def divisors(n):
	d = {1, n}
	for k in range(2, int(n ** 0.5) + 1):
		if n % k == 0:
			d.add(k)
			d.add(n // k)
	return sorted(d)

def lcm(a, b):
	return a * b // gcd(a, b)


from math import gcd
from itertools import combinations

def divisors(n):
	for i in range(1, int(n ** 0.5) + 1):
		if n % i == 0:
			yield i
			if i * i != n:
				yield (n // i)

def lcm_cardinality(n):
	return sum((a * b == gcd(a, b) * n for (a, b) in combinations(divisors(n), 2))) + 1


def lcm_cardinality(n):
	res = 1
	for i in [2] + list(range(3, int(n ** 0.5) + 1, 2)):
		count = 0
		while not n % i:
			(n, count) = (n // i, count + 1)
		res *= 2 * count + 1
	if n > 1:
		res *= 3
	return (res + 1) // 2


def lcm_cardinality(n):
	card = 1
	for d in range(2, int(n ** 0.5) + 1):
		if not n % d:
			mul = 0
			while not n % d:
				mul += 1
				n //= d
			card *= 2 * mul + 1
	if n > 1:
		card *= 3
	return (card + 1) // 2


def lcm_cardinality(n):
	ans = 1
	for i in range(2, int(n ** 0.5) + 1):
		if n % i == 0:
			t = 0
			while n % i == 0:
				n /= i
				t += 1
			ans *= t * 2 + 1
	if n > 1:
		ans *= 3
	return int((ans - 1) / 2 + 1)


from math import sqrt, gcd

def lcm_cardinality(n):
	li = sum([[i] if n // i == i else [i, n // i] for i in range(1, int(sqrt(n)) + 1) if n % i == 0], [])
	return len([[li[i], li[j]] for i in range(len(li)) for j in range(i, len(li)) if li[i] * li[j] // gcd(li[i], li[j]) == n])


from collections import Counter as C
p = [2]
for i in range(3, 15000, 2):
	if all((i % j for j in p)):
		p.append(i)

def factors(n):
	a = []
	for i in p:
		while n % i == 0:
			a.append(i)
			n //= i
	if n > 1:
		a.append(n)
	return a

def lcm_cardinality(n):
	if n == 1:
		return 1
	c = list(C(factors(n)).values())
	ans = 0
	for i in range(len(c)):
		m = c[i]
		for j in c[i + 1:]:
			m *= 2 * j + 1
		ans += m
	return ans + 1


from math import gcd, sqrt, ceil
from itertools import chain

def lcm(x, y):
	return x // gcd(x, y) * y

def lcm_cardinality(n):
	root = ceil(sqrt(n))
	divisor = chain.from_iterable(([i, n // i] for i in range(1, root) if n % i == 0))
	divisor = list(chain([root] if root * root == n else [], divisor))
	size = len(divisor)
	return sum((1 for i in range(size) for j in range(i, size) if lcm(divisor[i], divisor[j]) == n))


import math

def lcm(a, b):
	return a * b // math.gcd(a, b)

def divisors(n):
	(i, ans) = (1, [])
	while i ** 2 <= n:
		if not n % i:
			ans.append(i)
			if i ** 2 != n:
				ans.append(n // i)
		i += 1
	return ans

def lcm_cardinality(n):
	(ans, divs) = (0, divisors(n))
	for i in range(len(divs)):
		for j in range(i, len(divs)):
			if lcm(divs[i], divs[j]) == n:
				ans += 1
	return ans


def lcm_cardinality(n):
	cardinality = 1
	divisor = 2
	while n > 1:
		order = 0
		while n % divisor == 0:
			n //= divisor
			order += 1
		cardinality *= 2 * order + 1
		divisor += 1
	return (cardinality + 1) // 2