Given a number N, find the sum of all its factors.
Example 1:
Input: N = 30
Output: 72
Explanation: Factors sum 1 + 2 + 3 + 5 
+ 6 + 10 + 15 + 30 = 72
Example 2:
Input: N = 15
Output: 24
Explanation: Factors sum 1 + 3 + 5 
+ 15 = 24
Your Task:  
You don't need to read input or print anything. Your task is to complete the function factorSum() which takes N as input and returns the sum.
Expected Time Complexity: O(sqrt N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{9}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Starter code: #User function Template for python3



class Solution:

    def factorSum(self, N):

        # code here 

Solutions:
class Solution:

	def givePN(N):
		primenum = [True] * (N + 1)
		primenum[0] = False
		prime = []
		for i in range(2, N + 1):
			if primenum[i] == False:
				continue
			else:
				j = 2 * i
				prime.append(i)
				while j < N + 1:
					primenum[j] = False
					j = j + i
		return prime

	def factorSum(self, N):
		if N == 1:
			return 1
		if N == 0:
			return 0
		factors = [1, N]
		i = 2
		while i < int(N ** 0.5) + 1:
			if N % i == 0:
				factors.append(i)
				if i != N // i:
					factors.append(N // i)
			i += 1
		return sum(factors)


class Solution:

	def factorSum(self, N):
		i = 1
		result = 0
		while i * i <= N:
			if N % i == 0:
				result += i
				if i != N // i:
					result += N // i
			i += 1
		return result


class Solution:

	def factorSum(self, N):
		s = 0
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				s += i
				if i != N // i:
					s += N // i
		return s


from math import sqrt

class Solution:

	def factorSum(self, N):
		s = int(sqrt(N))
		sum = 0
		for i in range(1, s + 1):
			if N % i == 0:
				sum += i
				if i != N // i:
					sum += N // i
		return sum


import math

class Solution:

	def factorSum(self, N):
		sum = 0
		if N == 1:
			return 1
		for i in range(2, int(math.sqrt(N)) + 1):
			if N % i == 0:
				if N / i == i:
					sum += N / i
				else:
					sum += i + N / i
		return 1 + N + int(sum)


class Solution:

	def factorSum(self, N):
		if N == 1:
			return 1
		tot = 0
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				if i == N / i:
					tot += i
				else:
					tot = tot + i + int(N / i)
		return tot + 1 + N


class Solution:

	def factorSum(self, N):
		sum1 = 0
		for i in range(1, int(pow(N, 0.5)) + 1):
			if N % i == 0 and N // i != i:
				sum1 += i + N // i
			elif i * i == N:
				sum1 += i
		return sum1


import math

class Solution:

	def factorSum(self, N):
		ans = 0
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				ans += i
				if N // i != i:
					ans += N // i
		return ans


class Solution:

	def factorSum(self, n):
		import math
		p = int(math.sqrt(n))
		c = 0
		for i in range(1, p + 1):
			if n % i == 0:
				c = c + i + int(n / i)
		if p * p == n:
			return c - p
		else:
			return c


from math import sqrt

class Solution:

	def factorSum(self, n):
		if n == 1:
			return 1
		res = 0
		for i in range(2, int(sqrt(n)) + 1):
			if n % i == 0:
				if i == n // i:
					res += i
				else:
					res += i + n // i
		return res + n + 1


import math

class Solution:

	def factorSum(self, N):
		count = 1
		p = N // 2 + 1
		if N == 0:
			return 0
		elif N == 1:
			return 1
		else:
			for i in range(2, int(math.sqrt(N)) + 1):
				if N % i == 0:
					if i == N / i:
						count = count + i
					else:
						count = count + (i + N // i)
		return count + N


import math

class Solution:

	def factorSum(self, N):
		n = int(math.sqrt(N)) + 1
		s = 0
		st = set()
		for i in range(1, n):
			if N % i == 0:
				st.add(i)
				st.add(N // i)
		return sum(list(st))


class Solution:

	def floorSqrt(self, x):
		if x == 0 or x == 1:
			return x
		start = 1
		end = x // 2
		ans = x // 2
		while start <= end:
			mid = (start + end) // 2
			sqr = mid * mid
			if sqr == x:
				return mid
			if sqr <= x:
				start = mid + 1
				ans = mid
			else:
				end = mid - 1
		return ans

	def factorSum(self, n):
		ans = 0
		for i in range(1, self.floorSqrt(n) + 1):
			if n % i == 0:
				ans += i
				if n / i != i:
					ans += n // i
		return ans


import math

class Solution:

	def factorSum(self, N):
		if N == 1:
			return 1
		l = []
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				j = N // i
				if i != j:
					l.append(i)
					l.append(j)
				else:
					l.append(i)
		return sum(l)


import math

class Solution:

	def factorSum(self, N):
		if N == 1:
			return 1
		total = 0
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				if i == N // i:
					total += i
				else:
					total += i + N // i
		return total
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.factorSum(N))


class Solution:

	def factorSum(self, n):
		res = 0
		for i in range(1, n + 1):
			if i * i > n:
				break
			if n % i:
				continue
			if n // i == i:
				res += i
			else:
				res += i + n // i
		return res
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.factorSum(N))


class Solution:

	def factorSum(self, N):
		n = int(N ** 0.5)
		s = set()
		for i in range(1, n + 1):
			if N % i == 0:
				s.add(i)
				s.add(N // i)
		return sum(s)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.factorSum(N))


class Solution:

	def factorSum(self, N):
		i = 1
		sum = 0
		while i * i <= N:
			if N % i == 0:
				of = N // i
				sum = sum + i
				if of != i:
					sum += of
			i += 1
		return sum


class Solution:

	def factorSum(self, N):
		i = 1
		k = N ** 0.5
		s = 0
		while i <= k:
			if N % i == 0:
				j = N / i
				s += i
				if j != i:
					s += j
			i += 1
		return int(s)


from math import sqrt

class Solution:

	def factorSum(self, N):
		rslt = 0
		for x in range(1, int(sqrt(N) + 1)):
			if N % x == 0:
				rslt += x
				if N // x != x:
					rslt += N // x
		return rslt


import math

class Solution:

	def factorSum(self, n):
		t = math.floor(math.sqrt(n))
		i = 1
		s = 0
		while i <= t:
			if n % i == 0:
				if n // i == i:
					s += i
				else:
					s += i + n // i
			i += 1
		return s


class Solution:

	def factorSum(self, n):
		if n == 0 or n == 1:
			return n
		else:
			result = 0
			for i in range(1, int(n ** 0.5) + 1):
				if n % i == 0:
					if i == n / i:
						result = result + i
					else:
						result = result + (i + n // i)
		return result


import math

class Solution:

	def factorSum(self, n):
		ls = []
		i = 1
		while i <= int(math.sqrt(n)):
			if n % i == 0:
				if n / i == i:
					ls.append(i)
				else:
					ls.append(i)
					ls.append(int(n / i))
			i = i + 1
		return sum(ls)


class Solution:

	def factorSum(self, N):
		s = 0
		i = 1
		while i * i <= N:
			if N % i == 0:
				s += i
				if i != int(N / i):
					s += int(N / i)
			i += 1
		return s


class Solution:

	def factorSum(self, n):
		if n == 1:
			return 1
		else:
			r = 0
			for i in range(2, int(n ** (1 / 2)) + 1):
				if n % i == 0:
					if n / i == i:
						r += i
					else:
						r += i + n // i
		return r + n + 1


import math

class Solution:

	def factorSum(self, N):
		fact = []
		if N == 1:
			return 1
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				fact.append(i)
				fact.append(N // i)
		return sum(list(set(fact)))


import math

class Solution:

	def factorSum(self, n):
		if n == 1:
			return 1
		res = 0
		ls = []
		for i in range(1, int(math.sqrt(n)) + 1):
			if n % i == 0:
				ls.append(i)
				res += i
			if n % (n // i) == 0:
				ls.append(n // i)
				if i != n // i:
					res += n // i
		return res


class Solution:

	def factorSum(self, n):
		ans = 0
		i = 1
		while i * i <= n:
			if n % i == 0:
				ans += i
				if n // i != i:
					ans += n // i
			i += 1
		return ans


from math import sqrt

class Solution:

	def factorSum(self, n):
		res = 1
		for i in range(2, int(sqrt(n) + 1)):
			c_s = 1
			c_t = 1
			while n % i == 0:
				n = n / i
				c_t = c_t * i
				c_s += c_t
			res = res * c_s
		if n > 2:
			res = res * (1 + n)
		return int(res)


class Solution:

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


import math

class Solution:

	def factorSum(self, N):
		list1 = []
		p = int(math.sqrt(N))
		for i in range(1, p + 1):
			if N % i == 0:
				list1.append(i)
				if N // i != i:
					list1.append(N // i)
		return sum(list1)