Given X and Y as the GCD and LCM of two numbers A and B. Find all possible pairs of (A,B).
Note: (a, b) and (b, a) are considered as two different pairs.
Example 1:
Input: x = 2, y = 12
Output: 4
Explaination: The pairs are (2, 12), 
(4, 6), (6, 4) and (12, 2).
Example 2:
Input: x = 3, y = 6
Output: 2
Explaination: The pairs are (3, 6) and 
(6, 3).
Example 3:
Input: x = 6, y = 4
Output: 0
Explaination: There is no such pair such 
that gcd is 6 and lcm is 4.
Your Task:
You do not need to read input or print anything. Your task is to complete the function pairCount() which takes x and y as input parameters and returns the number of possibe pairs.
Expected Time Complexity: O(sqrt(N)*LogN)    [N is maximum of x and y]
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ x, y ≤ 10^{4}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3



class Solution:

    def pairCount(self, x, y):

        # code here

Solutions:
class Solution:

	def pairCount(self, x, y):
		if x > y:
			return 0
		else:

			def gcd(m, n):
				u = max(m, n)
				v = min(m, n)
				r = u % v
				while r > 0:
					u = v
					v = r
					r = u % v
				return v
			n = y
			p = x * y
			arr = []
			pr = []
			for i in range(1, y + 1):
				if y % i == 0:
					arr.append(i)
			for i in range(len(arr)):
				a = x * arr[i]
				b = n * x // a
				if gcd(a, b) == x:
					pr.append([a, b])
			return len(pr)


class Solution:

	def pairCount(self, g, l):

		def gcd(a, b):
			if a == 0:
				return b
			return gcd(b % a, a)
		count = 0
		p = g * l
		for a in range(g, l + 1):
			if p % a == 0 and gcd(a, p // a) == g:
				count += 1
		return count


class Solution:

	def gcd(self, a, b):
		if b == 0:
			return a
		return self.gcd(b, a % b)

	def pairCount(self, x, y):
		if x == y:
			return 1
		g = x * y
		c = 0
		for i in range(x, y + 1):
			if i % x == 0 and y % i == 0:
				j = g // i
				if j % x == 0 and i != j and (self.gcd(i, j) == x):
					c += 1
		return c


import math

class Solution:

	def calculate_lcm(self, x, y):
		if x > y:
			greater = x
		else:
			greater = y
		while True:
			if greater % x == 0 and greater % y == 0:
				lcm = greater
				break
			greater += 1
		return lcm

	def pairCount(self, x, y):
		count = 0
		for i in range(x, y + 1):
			for j in range(x, y + 1):
				if math.gcd(i, j) == x and i * j / math.gcd(i, j) == y:
					count += 1
		return count


class Solution:

	def pairCount(self, x, y):
		n = y / x
		ans = 2
		if n % 1 != 0:
			ans = 0
		else:
			for i in range(1, int(n ** 0.5) + 1):
				if n % i == 0 and n / i % i != 0:
					ans += 2
		if n == 1:
			ans = 1
		return ans


from math import gcd

class Solution:

	def pairCount(self, x, y):
		count = 0
		lit = []
		n = x * y
		i = 1
		while x * i <= y:
			(n2, n1) = (x * i, y // i)
			if n % n2 == 0 and n % n1 == 0 and (gcd(n1, n2) == x):
				lit.append(n2)
				lit.append(n1)
			i += 1
		return len(lit) // 2


import math

class Solution:

	def pairCount(self, x, y):
		count = 0
		p = x * y
		for a in range(1, y + 1):
			if not p % a and math.gcd(a, p // a) == x:
				count += 1
		return count

	def gcd(a, b):
		if a == 0:
			return b
		else:
			return math.gcd(b % a, a)