Given a natural number n, calculate sum of all its proper divisors. A proper divisor of a natural number is the divisor that is strictly less than the number.
Example 1:
Input: n = 10
Output: 8 
Explanation: Proper divisors 1 + 2 + 5 = 8. 
Example 2:
Input: n = 6
Output: 6
Explanation: Proper divisors 1 + 2 + 3 = 6. 
Your Task:  
You dont need to read input or print anything. Complete the function divSum() which takes n as input parameter and returns sum of proper divisors of n.
Expected Time Complexity: O(sqrt(n))
Expected Auxiliary Space: O(1)
Constraints:
2<= n <=10^{6}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3
class Solution:
    def divSum (self, n):
        # code here 

Solutions:
import math

class Solution:

	def divSum(self, n):
		s = 1
		a = int(math.sqrt(n))
		for i in range(2, a + 1):
			if n % i == 0:
				if i * i == n:
					s = s + i
				else:
					b = n // i
					s = s + i + b
		return s


class Solution:

	def divSum(self, n):
		if n <= 1:
			return 0
		div_sum = 1
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				div_sum += i
				if i != n // i:
					div_sum += n // i
		return div_sum


class Solution:

	def divSum(self, n):
		lim = int(n ** 0.5) + 1
		sum = 1
		for i in range(2, lim):
			if n % i == 0:
				if i == n // i:
					sum += i
				else:
					sum += i + n // i
		return sum


class Solution:

	def divSum(self, n):
		a = [1]
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				if i not in a:
					a.append(i)
				if n // i not in a:
					a.append(n // i)
		return sum(a)


class Solution:

	def divSum(self, n):
		sum1 = 1
		import math
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				if i * i == n:
					sum1 += i
				else:
					sum1 += i + n // i
		return sum1


class Solution:

	def divSum(self, n):
		result = 1
		m = int(pow(n, 0.5) + 1)
		for i in range(2, m):
			if n % i == 0:
				result += i
				result += int(n / i)
		if (m - 1) * (m - 1) == n:
			result -= m - 1
		return result


import math

class Solution:

	def divSum(self, n):
		i = 2
		sm = 1
		m = math.sqrt(n)
		while i <= m:
			if n % i == 0:
				if n // i != i:
					sm += i + n // i
				else:
					sm += i
			i += 1
		return sm


class Solution:

	def divSum(self, n):
		x = 0
		for i in range(1, int(n ** 0.5) + 1):
			if n % i == 0:
				x += i
				if n // i != i:
					x += n // i
		return x - n
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.divSum(n))


import math

class Solution:

	def divSum(self, n):
		i = 2
		result = 0
		while i <= math.sqrt(n):
			if n % i == 0:
				if i == n / i:
					result = result + i
				else:
					result = result + (i + n / i)
			i = i + 1
		return int(result + 1)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.divSum(n))


class Solution:

	def divSum(self, n):
		s = {1: 1}
		k = int(n ** 0.5)
		for i in range(2, k + 1):
			if n % i == 0:
				s[i] = 1
				s[n // i] = 1
		return sum(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		ob = Solution()
		print(ob.divSum(n))


import math

class Solution:

	def divSum(self, n):
		sum = 0
		x = int(math.sqrt(n))
		i = 1
		while i <= x:
			if n % i == 0:
				if n // i == i:
					sum += i
				elif n // i != n:
					sum += i
					sum += n // i
				else:
					sum += i
			i += 1
		return sum


class Solution:

	def divSum(self, n):
		p = 1
		w = int(pow(n, 0.5))
		for i in range(2, w + 1):
			if n % i == 0:
				if i * i != n:
					p = p + i + n // i
				else:
					p = p + i
		return p


import math

class Solution:

	def divSum(self, n):
		s = 1
		p = int(math.sqrt(n))
		for i in range(2, int(math.sqrt(n)) + 1):
			if n % i == 0:
				s = s + i + n // i
		if p ** 2 == n:
			s -= p
		return s


class Solution:

	def divSum(self, n):
		sum = 1
		if n == 1:
			return 0
		for x in range(2, int(pow(n, 0.5)) + 1):
			if n % x == 0:
				if x == n / x:
					sum += x
				else:
					sum += x + n / x
		return int(sum)


class Solution:

	def divSum(self, n):
		c = 0
		i = 2
		while i <= n ** 0.5:
			if n % i == 0:
				if n // i == i:
					c += i
				else:
					c += n // i + i
			i += 1
		return c + 1


import math

class Solution:

	def divSum(self, num):
		result = 0
		i = 2
		while i <= math.sqrt(num):
			if num % i == 0:
				if i == num / i:
					result = result + i
				else:
					result = result + (i + num / i)
			i = i + 1
		return round(result + 1)


class Solution:

	def divSum(self, n):
		sum = 0
		i = 2
		while i <= n ** (1 / 2):
			if n % i == 0:
				if i == n / i:
					sum = sum + i
				else:
					sum = sum + (i + n / i)
			i = i + 1
		return int(sum + 1)


import math

class Solution:

	def divSum(self, n):
		result = 0
		i = 2
		while i <= math.sqrt(n):
			if n % i == 0:
				if i == n / i:
					result = result + i
				else:
					result = result + (i + n // i)
			i = i + 1
		return int(result + 1)