Given an integer N, print all the divisors of N in the ascending order.
 
Example 1:
Input : 20
Output: 1 2 4 5 10 20
Explanation: 20 is completely 
divisible by 1, 2, 4, 5, 10 and 20.
Example 2:
Input: 21191
Output: 1 21191
Explanation: As 21191 is a prime number,
it has only 2 factors(1 and the number itself).
Your Task:
Your task is to complete the function print_divisors() which takes N as input parameter and prints all the factors of N as space seperated integers in sorted order. You don't have to print any new line after printing the desired output. It will be handled in driver code.
 
Expected Time Complexity: O(sqrt(N))
Expected Space Complexity: O(sqrt(N))
 
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3



class Solution:

    def print_divisors(self, N):

        # code here

Solutions:
import math

class Solution:

	def print_divisors(self, N):
		ans = []
		for i in range(1, int(N ** 0.5 + 1)):
			if N % i == 0:
				ans.append(i)
				if i != N // i:
					ans.append(N // i)
		ans.sort()
		for i in ans:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		l = []
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				l.append(i)
				if N / i not in l:
					l.append(int(N / i))
		l.sort()
		for i in l:
			print(i, end=' ')


import math

class Solution(object):

	def print_divisors(self, N):
		(divisors, divisor) = ([], 1)
		while divisor * divisor <= N:
			if N % divisor == 0:
				divisors.append(divisor)
				if divisor * divisor != N:
					divisors.append(N // divisor)
			divisor += 1
		for divisor in sorted(divisors):
			print(divisor, end=' ')


class Solution:

	def print_divisors(self, N):
		left_elements = []
		right_elements = []
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				left_elements.append(str(i))
				other = N // i
				if other != i:
					right_elements.append(str(other))
		print(' '.join(left_elements + right_elements[::-1]), end='')


class Solution:

	def print_divisors(self, N):
		ans = []
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				ans.append(i)
		j = len(ans) - 1
		while j != -1:
			if N // ans[j] != ans[j]:
				ans.append(N // ans[j])
			j -= 1
		for i in range(0, len(ans)):
			print(ans[i], end=' ')


class Solution:

	def print_divisors(self, N):
		divisors = []
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				divisors.append(i)
				if i != N // i:
					divisors.append(N // i)
		divisors.sort()
		for divisor in divisors:
			print(divisor, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		divisors = []
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				divisors.append(i)
				if i != N // i:
					divisors.append(N // i)
		divisors.sort()
		for div in divisors:
			print(div, end=' ')


class Solution:

	def print_divisors(self, N):
		i = 1
		ans = []
		while i <= N ** (1 / 2):
			if N % i == 0:
				ans.append(i)
				if N // i != i:
					ans.append(N // i)
			i = i + 1
		ans.sort()
		for a in ans:
			print(a, end=' ')


class Solution:

	def print_divisors(self, N):
		l = []
		m = []
		for i in range(1, int(N ** (1 / 2) + 1)):
			if N % i == 0:
				l.append(i)
				if N // i != i:
					m.append(N // i)
		for i in l:
			print(i, end=' ')
		for i in range(len(m) - 1, -1, -1):
			print(m[i], end=' ')


import math

class Solution:

	def print_divisors(self, N):
		ls = []
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				ls.append(i)
				if N // i != i:
					ls.append(N // i)
		ls.sort()
		for it in ls:
			print(it, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		if N == 1:
			print(1, end='')
		else:
			ans = set()
			l = int(math.sqrt(N))
			for i in range(1, l + 1):
				if N % i == 0:
					ans.add(i)
					ans.add(N // i)
			arr = [i for i in ans]
			arr.sort()
			for i in arr:
				print(i, end=' ')


class Solution:

	def print_divisors(self, N):
		c = set()
		i = 1
		while i * i <= N:
			if N % i == 0:
				c.add(i)
				c.add(N // i)
			i += 1
		c = list(c)
		c.sort()
		for x in c:
			print(x, end=' ')


import math

class Solution:

	def isPrime(self, n):
		if n == 1:
			return False
		else:
			lim = int(math.sqrt(n)) + 1
			for i in range(2, lim):
				if n % i == 0:
					return False
			return True

	def print_divisors(self, N):
		res = []
		if self.isPrime(N):
			res = [1, N]
		else:
			lim = int(math.sqrt(N)) + 1
			for i in range(1, lim):
				if N % i == 0:
					if N // i == i:
						res.append(i)
					else:
						res.extend([i, N // i])
			res.sort()
		print(*res, end=' ')


class Solution:

	def print_divisors(self, N):
		v = []
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				if N // i == i:
					print(i, end=' ')
				else:
					print(i, end=' ')
					v.append(N // i)
		for i in range(len(v) - 1, -1, -1):
			print(v[i], end=' ')


import math

class Solution:

	def print_divisors(self, N):
		lst = []
		for i in range(1, math.floor(math.sqrt(N)) + 1):
			if N % i == 0:
				lst.append(i)
				lst.append(N // i)
		print(*sorted(set(lst)), end='')


class Solution:

	def print_divisors(self, N):
		l = []
		if N == 1:
			print(1, end=' ')
			return
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0 and i != int(N / i):
				l.append(i)
				l.append(int(N / i))
			elif N % i == 0:
				l.append(i)
		l.sort()
		for i in l:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		arr = []
		t = int(math.sqrt(N))
		for i in range(1, t + 1):
			if N % i == 0:
				arr.append(i)
				if N // i != i:
					arr.append(N // i)
		arr.sort()
		for i in arr:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		res = []
		limit = int(math.sqrt(N))
		for i in range(2, limit + 1):
			if N % i == 0:
				res += [i, int(N / i)]
		res += [1, N]
		res = sorted(set(res))
		for i in res:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, n):
		ans = []
		for i in range(1, int(math.sqrt(n)) + 1):
			if n % i == 0:
				ans.append(i)
				if n // i != i:
					ans.append(n // i)
		ans.sort()
		for val in ans:
			print(val, end=' ')


class Solution:

	def print_divisors(self, n):
		import math
		a = []
		for i in range(1, int(math.sqrt(n)) + 1):
			if n % i == 0:
				print(i, end=' ')
				if i * i != n:
					a.append(n // i)
		for i in range(len(a) - 1, -1, -1):
			print(a[i], end=' ')


class Solution:

	def print_divisors(self, N):
		from math import sqrt
		ans = []
		c = 0
		for i in range(1, int(sqrt(N)) + 1):
			if N % i == 0:
				print(i, end=' ')
			c += 1
		for i in range(c, 0, -1):
			if N % i == 0:
				if i != N // i:
					print(N // i, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		output = []
		for i in range(1, int(math.sqrt(N)) + 1):
			if N % i == 0:
				output.append(i)
				j = N // i
				if j != i:
					output.append(j)
		output.sort()
		for item in output:
			print(item, end=' ')


class Solution:

	def print_divisors(self, N):
		lis = []
		import math
		var = int(math.sqrt(N))
		for i in range(1, var + 1):
			if N % i == 0:
				lis.append(i)
				if N // i != i:
					lis.append(N // i)
		for j in sorted(lis):
			print(j, end=' ')


class Solution:

	def print_divisors(self, N):
		import math
		var = int(math.sqrt(N))
		for i in range(1, var + 1):
			if N % i == 0:
				print(i, end=' ')
		for j in range(var, 0, -1):
			if N % j == 0 and N // j != j:
				print(N // j, end=' ')


class Solution:

	def print_divisors(self, N):
		res = []
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				if i == N / i:
					print(i, end=' ')
				else:
					print(i, end=' ')
					res.append(int(N / i))
		for i in reversed(res):
			print(i, end=' ')


class Solution:

	def print_divisors(self, N):
		i = 1
		while i * i <= N:
			if N % i == 0:
				print(i, end=' ')
			i += 1
		i -= 1
		if i * i == N:
			i = i - 1
		while i > 0:
			if N % i == 0:
				print(N // i, end=' ')
			i -= 1


class Solution:

	def print_divisors(self, N):
		res = []

		def solve(i):
			if i * i > N:
				return
			if N % i == 0:
				print(i, end=' ')
				solve(i + 1)
				if i != N // i:
					print(N // i, end=' ')
			else:
				solve(i + 1)
		solve(1)


class Solution:

	def print_divisors(self, N):
		l = set()
		i = 1
		while i * i <= N:
			if N % i == 0:
				l.add(i)
				l.add(N // i)
			i += 1
		for x in sorted(l):
			print(x, end=' ')


class Solution:

	def print_divisors(self, N):
		if N == 1:
			print(1, end=' ')
			return
		i = 1
		l = []
		while i * i <= N:
			if N % i == 0:
				if i == N // i:
					l.append(i)
				else:
					l.append(i)
					l.append(N // i)
			i += 1
		l.sort()
		for ele in l:
			print(ele, end=' ')


from math import sqrt

class Solution:

	def print_divisors(self, N):
		ans = []
		for i in range(1, int(sqrt(N)) + 1):
			if N % i == 0:
				ans.append(i)
				if N // i != i:
					ans.append(N // i)
		ans.sort()
		for j in ans:
			print(j, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		divisors1 = []
		divisors2 = []
		no = 1
		dividend = N
		sqrt_N = int(pow(N, 0.5))
		while no <= sqrt_N:
			if dividend % no == 0:
				divisors1.append(no)
				if no * no != N:
					divisors2.append(N // no)
			no += 1
		for i in divisors1:
			print(i, end=' ')
		for i in reversed(range(len(divisors2))):
			print(divisors2[i], end=' ')
		return


class Solution:

	def print_divisors(self, N):
		l = [1]
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				if N // i != i:
					l.append(i)
					l.append(N // i)
				else:
					l.append(i)
		l.sort()
		if N != 1:
			l.append(N)
		for i in l:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, n):
		v = []
		i = 1
		while i <= math.sqrt(n):
			if n % i == 0:
				if n // i == i:
					print(i, end=' ')
				else:
					print(i, end=' ')
					v.append(n // i)
			i += 1
		j = len(v) - 1
		while j >= 0:
			print(v[j], end=' ')
			j -= 1


class Solution:

	def print_divisors(self, N):
		ans = []
		for i in range(1, int(N ** 0.5) + 1):
			if not N % i:
				ans.append(i)
		for num in ans[::-1]:
			if N // num != num:
				ans.append(N // num)
		for num in ans:
			print(num, end=' ')


class Solution:

	def print_divisors(self, N):
		k = 1
		m = set()
		while k * k <= N:
			if N % k == 0:
				m.add(k)
				m.add(N // k)
			k += 1
		m = list(m)
		m.sort()
		print(*m)


class Solution:

	def print_divisors(self, N):
		import math

		def isprime(N):
			if N <= 1:
				return 0
			if N <= 3:
				return 1
			if N % 2 == 0 or N % 3 == 0:
				return 0
			i = 5
			while i * i <= N:
				if N % i == 0 or N % (i + 2) == 0:
					return 0
				i = i + 6
			return 1
		if isprime(N) == 1:
			print(1, end=' ')
			print(N, end=' ')
		else:
			x = []
			for i in range(1, int(math.sqrt(N)) + 1):
				if N % i == 0:
					if int(N / i) == i:
						x.append(i)
					else:
						x.append(i)
						x.append(int(N / i))
			x.sort()
			for i in range(0, len(x)):
				print(x[i], end=' ')


class Solution:

	def print_divisors(self, N: int) -> None:
		(arr1, arr2) = ([], [])
		for i in range(2, int(N ** 0.5) + 1):
			if N % i == 0:
				if i == N // i:
					arr1.append(i)
				else:
					arr1.append(i)
					if N % (N // i) == 0:
						arr2.insert(0, N // i)
		if N == 1:
			divisors = [1]
		else:
			divisors = [1] + arr1 + arr2 + [N]
		for divisor in divisors:
			print(divisor, end=' ')


import math

class Solution:

	def print_divisors(self, n):
		res = []
		i = 1
		while i <= math.sqrt(n):
			if n % i == 0:
				if n / i == i:
					res.append(i)
				else:
					res.append(i)
					res.append(n // i)
			i += 1
		res.sort()
		for i in res:
			print(i, end=' ')


class Solution:

	def print_divisors(self, N):
		import math
		x = int(math.sqrt(N))
		list1 = []
		for i in range(1, x + 1):
			if N % i == 0:
				print(i, end=' ')
				y = N // i
				if i != y:
					list1.append(y)
		list1.sort()
		for i in list1:
			print(i, end=' ')


import math

class Solution:

	def print_divisors(self, N):
		output = list()
		for i in range(1, int(math.sqrt(N)) + 1):
			if int(N % i) == 0:
				output.append(i)
				if i != int(N / i):
					output.append(int(N / i))
		output.sort()
		for i in range(len(output)):
			print(output[i], end=' ')


class Solution:

	def print_divisors(self, n):
		div = 1
		large = []
		while div * div <= n:
			if n % div == 0:
				(factor1, factor2) = (div, n // div)
				if factor1 == factor2:
					print(factor1, end=' ')
				else:
					print(factor1, end=' ')
					large.append(factor2)
			div += 1
		for i in range(len(large) - 1, -1, -1):
			print(large[i], end=' ')