Amicable numbers are two different numbers so related that the sum of the proper divisors of each is equal to the other number. (A proper divisor of a number is a positive factor of that number other than the number itself. Given two Numbers A and B, find whether they are Amicable Numbers or not. Print 1 if they are Amicable else 0.
 
Example 1:
Input:
A = 220 , B = 284
Output:
1
Explanation:
Proper divisors of 220 are 1, 2,
4, 5, 10, 11, 20, 22, 44, 55 and 110.
Sum of these is 284. Proper divisors
of 284 are 1, 2, 4, 71 and 142
with sum 220.
Example 2:
Input:
A = 4 , B = 8
Output:
0
Explanation:
Proper divisors of 4 are 1 and 2.
Sum of these is 3!=8. Proper divisors
of 8 are 1, 2 and 4 with sum 7!=4 and
thus the Otput 0.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function isAmicable() which takes 2 Integes A and B as input and returns the answer.
 
Expected Time Complexity: O(sqrt(A,B))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= A,B <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Mathematics

Starter code: #User function Template for python3



class Solution:

    def isAmicable(self, A , B):

        # code here 

Solutions:
class Solution:

	def isAmicable(self, A, B):
		c = 0
		d = 0
		for i in range(1, A):
			if A % i == 0:
				c += i
		for i in range(1, B):
			if B % i == 0:
				d += i
		if c == B and d == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		i = 1
		c = 0
		while i < A:
			if A % i == 0:
				c += i
			i = i + 1
		t = 0
		if c == B:
			t = 1
		else:
			return 0
		i = 1
		c = 0
		while i < B:
			if B % i == 0:
				c += i
			i = i + 1
		f = 0
		if c == A:
			f = 1
		else:
			return 0
		return 1


class Solution:

	def isAmicable(self, A, B):
		s = 0
		p = 0
		for i in range(1, A):
			if A % i == 0:
				s += i
		for i in range(1, B):
			if B % i == 0:
				p += i
		if s == B and p == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		x = 0
		y = 0
		for i in range(1, A):
			if A % i == 0:
				x += i
		for i in range(1, B):
			if B % i == 0:
				y += i
		if A == y and B == x:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		a = 0
		b = 0
		for i in range(1, A):
			if A % i == 0:
				a = a + i
		for i in range(1, B):
			if B % i == 0:
				b = b + i
		if A == b and B == a:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		s1 = 0
		s2 = 0
		for i in range(1, A):
			if A % i == 0:
				s1 += i
		for j in range(1, B):
			if B % j == 0:
				s2 += j
		if s1 == B and s2 == A:
			return 1
		else:
			return 0


import math

class Solution:

	def isAmicable(self, A, B):
		if A == 1 or B == 1:
			return 0
		(s1, s2) = (1, 1)
		(l1, l2) = (int(math.sqrt(A)) + 1, int(math.sqrt(B)) + 1)
		for i in range(2, l1):
			if A % i == 0:
				s1 += i + A // i
		for i in range(2, l2):
			if B % i == 0:
				s2 += i + B // i
		if s1 == B and s2 == A:
			return 1
		return 0


def sum_number(a):
	lst = []
	for i in range(1, a):
		if a % i == 0:
			lst.append(i)
	return sum(lst)

class Solution:

	def isAmicable(self, A, B):
		a = sum_number(A)
		b = sum_number(B)
		if A == b and B == a:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		sumOfA = 0
		sumOfB = 0
		for i in range(1, int(A / 2) + 1):
			if A % i == 0:
				sumOfA += i
		for j in range(1, int(B / 2) + 1):
			if B % j == 0:
				sumOfB += j
		if sumOfA == B and sumOfB == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		if A > B:
			(A, B) = (B, A)
		(K, L) = ([], [])
		for i in range(1, B):
			if A % i == 0 and i < A:
				K.append(i)
			if B % i == 0:
				L.append(i)
		return 1 if sum(K) == B and sum(L) == A else 0


import math

class Solution:

	def isAmicable(self, A, B):
		s = 0
		for i in range(1, int(math.sqrt(A))):
			if A % i == 0:
				s += i
				if i > 1:
					s += A // i
		s1 = 0
		for i in range(1, int(math.sqrt(B))):
			if B % i == 0:
				s1 += i
				if i > 1:
					s1 += B // i
		if s1 == A and B == s:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		sum1 = 0
		sum2 = 0
		for i in range(1, A):
			if A % i == 0:
				sum1 += i
		for i in range(1, B):
			if B % i == 0:
				sum2 += i
		if sum1 == B and sum2 == A:
			return 1
		else:
			return 0


import math

class Solution:

	def isAmicable(self, A, B):
		ADivisors = []
		for i in range(1, A):
			if A % i == 0:
				ADivisors.append(i)
		BDivisors = []
		for j in range(1, B):
			if B % j == 0:
				BDivisors.append(j)
		if sum(ADivisors) == B and sum(BDivisors) == A:
			return 1
		else:
			return 0


import math

class Solution:

	def isAmicable(self, A, B):

		def divisorSum(n):
			result = 1
			i = 2
			while i < int(math.sqrt(n)):
				if n % i == 0:
					result = result + (i + int(n / i))
				i += 1
			if i * i == n:
				result += i
			return result
		if A == B:
			return 0
		if divisorSum(A) != B:
			return 0
		return 1 if divisorSum(B) == A else 0


class Solution:

	def isAmicable(self, A, B):
		if A == 1 or B == 1:
			return 0
		s = {1: 1}
		k = int(A ** 0.5)
		for i in range(2, k + 1):
			if A % i == 0:
				s[i] = 1
				s[A // i] = 1
		k = int(B ** 0.5)
		m = {1: 1}
		for i in range(2, k + 1):
			if B % i == 0:
				m[i] = 1
				m[B // i] = 1
		if sum(s) == B and sum(m) == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		s = 0
		p = 0
		for i in range(1, A):
			if A % i == 0:
				s += i
		for i in range(1, B):
			if B % i == 0:
				p += i
		if s == B and p == A:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(A, B) = map(int, input().split())
		ob = Solution()
		print(ob.isAmicable(A, B))


class Solution:

	def isAmicable(self, A, B):
		x = sum([i for i in range(1, int(A / 2) + 1) if A % i == 0])
		y = sum([i for i in range(1, int(B / 2) + 1) if B % i == 0])
		if x == B and y == A:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(A, B) = map(int, input().split())
		ob = Solution()
		print(ob.isAmicable(A, B))


class Solution:

	def isAmicable(self, A, B):
		sum = 0
		sum1 = 0
		for i in range(1, A):
			if A % i == 0:
				sum = sum + i
		for j in range(1, B):
			if B % j == 0:
				sum1 = sum1 + j
		if sum == B and sum1 == A:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(A, B) = map(int, input().split())
		ob = Solution()
		print(ob.isAmicable(A, B))


class Solution:

	def isAmicable(self, A, B):
		s1 = 0
		s2 = 0
		for i in range(1, A):
			if A % i == 0:
				s1 += i
		for j in range(1, B):
			if B % j == 0:
				s2 += j
		if s1 == B and s2 == A:
			return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		(A, B) = map(int, input().split())
		ob = Solution()
		print(ob.isAmicable(A, B))


class Solution:

	def isAmicable(self, A, B):
		rA = max(A, B)
		rB = min(A, B)
		sumA = rA > 1
		sumB = rB > 1
		div = 2
		while div * div <= rA:
			if rA % div == 0:
				of = rA // div
				sumA = sumA + div + of
			if div * div <= rB:
				if rB % div == 0:
					of = rB / div
					sumB = sumB + div + of
			div = div + 1
		if sumA == rB and sumB == rA:
			return 1
		return 0


import math

class Solution:

	def isAmicable(self, A, B):
		if A == 1 and B == 1:
			return 0
		s1 = 1
		i = 2
		while i <= math.sqrt(A):
			if A % i == 0:
				if A / i == i:
					s1 += i
				else:
					s1 += i
					s1 += int(A / i)
			i += 1
		s2 = 1
		i = 2
		while i <= math.sqrt(B):
			if B % i == 0:
				if B / i == i:
					s2 += i
				else:
					s2 += i
					s2 += int(B / i)
			i += 1
		return 1 if s1 == B and s2 == A else 0


def getSumOfDivisors(n):
	sm = 0
	for i in range(1, n, 1):
		if n % i == 0:
			sm += i
	return sm

class Solution:

	def isAmicable(self, a, b):
		if a == getSumOfDivisors(b) and b == getSumOfDivisors(a):
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		if A == 1 and B == 1:
			return 0
		a = 0
		b = 0
		import math
		a1 = int(math.sqrt(A)) + 1
		a2 = int(math.sqrt(B)) + 1
		for i in range(2, a1):
			if A % i == 0:
				a = a + i + A // i
		for i in range(2, a2):
			if B % i == 0:
				b = b + i + B // i
		if a + 1 == B and b + 1 == A:
			return 1
		return 0


import math as m

class Solution:

	def isAmicable(self, a, b):
		if a == 1 or b == 1:
			return 0
		ans1 = self.func(a)
		ans2 = self.func(b)
		if ans1 == b and ans2 == a:
			return 1
		else:
			return 0

	def func(self, x):
		ans = 0
		for i in range(2, int(m.sqrt(x)) + 1):
			if x % i == 0:
				ans += i
				if i != x // i:
					ans += x // i
		return ans + 1


class Solution:

	def isAmicable(self, A, B):
		li = []
		l = []
		for i in range(1, A):
			if A % i == 0:
				li.append(i)
		for j in range(1, B):
			if B % j == 0:
				l.append(j)
		if sum(li) == B and sum(l) == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		a = []
		b = []
		for i in range(1, A):
			if A % i == 0:
				a.append(i)
		for j in range(1, B):
			if B % j == 0:
				b.append(j)
		s1 = sum(a)
		s2 = sum(b)
		if s1 == B and s2 == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		l1 = []
		l2 = []
		for i in range(1, int(A / 2 + 1)):
			if A % i == 0:
				l1.append(i)
		for i in range(1, int(B / 2 + 1)):
			if B % i == 0:
				l2.append(i)
		sum1 = sum(l1)
		sum2 = sum(l2)
		if sum1 == B and sum2 == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, a, b):
		count1 = 0
		count2 = 0
		for i in range(1, a):
			if a % i == 0 and i != a:
				count1 += i
		for i in range(1, b):
			if b % i == 0 and i != b:
				count2 += i
		if count1 == b and count2 == a:
			return 1
		else:
			return 0


from math import sqrt

class Solution:

	def sumofdiv(self, n):
		k = int(sqrt(n))
		i = 2
		su = 0
		while i <= k:
			if n % i == 0:
				if i == int(n / i):
					su += i
				else:
					su += i
					su += int(n / i)
			i += 1
		return su + 1

	def isAmicable(self, A, B):
		if A == B:
			return 0
		if self.sumofdiv(A) == B and A == self.sumofdiv(B):
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		res1 = 0
		res2 = 0
		for i in range(1, A):
			if A % i == 0:
				res1 = res1 + i
		if res1 != B:
			return 0
		for i in range(1, B):
			if B % i == 0:
				res2 = res2 + i
		if res2 != A:
			return 0
		return 1


class Solution:

	def isAmicable(self, A, B):
		SA = 0
		SB = 0
		for i in range(1, A):
			if A % i == 0:
				SA += i
		for j in range(1, B):
			if B % j == 0:
				SB += j
		if A == SB and B == SA:
			return 1
		else:
			return 0


import math

class Solution:

	def isAmicable(self, A, B):
		if A == B:
			return 0
		x = int(math.sqrt(A))
		y = int(math.sqrt(B))
		i = 2
		c = 1
		d = 1
		while i * i < A:
			if int(A % i) == 0:
				c += i
				c += A // i
			i += 1
		if i * i == A:
			c += i
		i = 2
		while i * i < B:
			if B % i == 0:
				d += i
				d += B // i
			i += 1
		if i * i == B:
			d += i
		if c == B and d == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		l1 = []
		l2 = []
		i = 1
		while i * i <= A:
			if A % i == 0:
				l1.append(i)
				l1.append(int(A / i))
			i += 1
		i = 1
		while i * i <= B:
			if B % i == 0:
				l2.append(i)
				l2.append(int(B / i))
			i += 1
		sum1 = 0
		sum2 = 0
		for i in l1:
			sum1 += i
		for i in l2:
			sum2 += i
		sum1 -= A
		sum2 -= B
		if A == 1 and B == 1:
			return 0
		if sum1 == B and sum2 == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		(d1, d2) = ([], [])
		for i in range(1, A // 2 + 1):
			if A % i == 0:
				d1.append(i)
		for i in range(1, B // 2 + 1):
			if B % i == 0:
				d2.append(i)
		if sum(d1) == B and sum(d2) == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		sum1 = 0
		sum2 = B
		for j in range(1, A):
			if A % j == 0:
				sum1 = sum1 + j
		if sum1 == sum2:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		s = 0
		t = 0
		for i in range(1, A + 1):
			if A % i == 0:
				s += i
		b = s - A
		for j in range(1, B + 1):
			if B % j == 0:
				t += j
		c = t - B
		return 1 if b == B and c == A else 0


class Solution:

	def isAmicable(self, A, B):
		s1 = 1
		s2 = 1
		if A == 1 or B == 1:
			return 0
		for i in range(2, int(A ** 0.5) + 1):
			if A % i == 0:
				s1 += i
				s1 += A // i
		for i in range(2, int(B ** 0.5) + 1):
			if B % i == 0:
				s2 += i
				s2 += B // i
		if s1 == B and s2 == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		(sum1, sum2) = (0, 0)
		(i, j) = (1, 1)
		while i < A:
			if A % i == 0:
				sum1 += i
			i += 1
		while j < B:
			if B % j == 0:
				sum2 += j
			j += 1
		if sum1 == B and sum2 == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		count = 0
		for i in range(1, A // 2 + 1):
			if A % i == 0:
				count += i
		count1 = 0
		for i in range(1, B // 2 + 1):
			if B % i == 0:
				count1 += i
		if count == B and count1 == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, x, y):
		tmp = y
		a = 0
		b = 0
		for i in range(1, tmp):
			if tmp % i == 0:
				a += i
		tmp = x
		for i in range(1, tmp):
			if tmp % i == 0:
				b += i
		if a == x and b == y:
			return 1
		return 0


def divisor(a):
	n = 0
	for i in range(int(a / 2)):
		if a % (i + 1) == 0:
			n = n + i + 1
	return n

class Solution:

	def isAmicable(self, A, B):
		c = divisor(A)
		d = divisor(B)
		if c == B and d == A:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		Ad = []
		Bd = []
		(c, d) = (0, 0)
		for i in range(1, int(A / 2 + 1)):
			if A % i == 0:
				Ad.append(i)
		for i in range(1, int(B / 2 + 1)):
			if B % i == 0:
				Bd.append(i)
		for i in Ad:
			c += i
		for i in Bd:
			d += i
		if (c == B) & (d == A):
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		(a, b) = ([], [])
		temp = 1
		while temp <= A // 2:
			if A % temp == 0:
				a.append(temp)
			temp += 1
		temp = 1
		while temp <= B // 2:
			if B % temp == 0:
				b.append(temp)
			temp += 1
		if sum(a) == B and sum(b) == A:
			return 1
		return 0


class Solution:

	def isAmicable(self, A, B):
		(c1, c2) = (0, 0)
		if A == 1 and B == 1:
			return 0
		else:
			for i in range(1, A + 1):
				if A % i == 0:
					c1 += i
			for j in range(1, B + 1):
				if B % j == 0:
					c2 += j
			if c1 == c2:
				return 1
			return 0


import math

class Solution:

	def isAmicable(self, a, b):
		da = []
		db = []
		sa = int(math.sqrt(a))
		sb = int(math.sqrt(b))
		for i in range(1, sa + 1):
			if a % i == 0:
				if i == a // i:
					da.append(i)
				else:
					da.append(i)
					da.append(a // i)
		for i in range(1, sb + 1):
			if b % i == 0:
				if i == b // i:
					db.append(i)
				else:
					db.append(i)
					db.append(b // i)
		if sum(da) - a == b and sum(db) - b == a:
			return 1
		else:
			return 0


class Solution:

	def isAmicable(self, A, B):
		a = []
		b = []
		for i in range(1, A // 2 + 1):
			if A % i == 0:
				a.append(i)
		if sum(a) != B:
			return 0
		for i in range(1, B // 2 + 1):
			if B % i == 0:
				b.append(i)
		if sum(b) != A:
			return 0
		return 1