Given a positive integer N. The task is to check whether a number has exactly three distinct factors or not. 
 
Example 1:
Input:
N = 10
Output:
0
Explanation:
10 has 4 factors- 1,2,5 and 10.
So, the Ouput is 0.
Example 2:
Input:
N = 9
Output:
1
Explanation:
9 has 3 factors- 1,3 and 9.
So, the Ouput is 1.
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function hasThreePrimeFac() which takes an Integer N as input and returns 1 if it has exactly 3 factors else returns 0.
 
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 10^{12}

Difficulty: EASY
Tags: Number theory

Mathematics
Expected Time Complexity: O(sqrt(N))
Expected Auxiliary Space: O(1)

Starter code:
#User function Template for python3

class Solution:
    def hasThreePrimeFac(self, N):
        # code here 

Solutions:
class Solution:

	def hasThreePrimeFac(self, N):

		def prime(n):
			if n == 1 or n == 0:
				return False
			k = int(n ** 0.5)
			for i in range(2, k + 1):
				if n % i == 0:
					return False
			return True
		num = int(N ** 0.5)
		if num * num == N:
			if prime(num):
				return 1
			else:
				return 0
		else:
			return 0


class Solution:

	def hasThreePrimeFac(self, N):
		count = 1
		for i in range(1, int(N ** 0.5) + 1):
			if N % i == 0:
				count += 1
				if count > 3:
					return 0
		if count == 3:
			return 1
		else:
			return 0


import math

def prime(n):
	if n <= 1:
		return 0
	c = int(math.sqrt(n))
	for i in range(2, c + 1):
		if n % i == 0:
			return 0
	return 1

class Solution:

	def hasThreePrimeFac(self, n):
		c = int(math.sqrt(n))
		if n == c * c and prime(c) == 1:
			return 1
		else:
			return 0


class Solution:

	def hasThreePrimeFac(self, n):
		if n == 1 or n == 0:
			return 0
		x = int(n ** 0.5)
		if x * x == n:
			for i in range(2, int(x ** 0.5) + 1):
				if x % i == 0:
					return 0
			else:
				return 1
		else:
			return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):

		def isprime(N):
			if N <= 1:
				return 0
			for i in range(2, int(math.sqrt(N)) + 1):
				if N % i == 0:
					return 0
			return 1
		n = int(math.sqrt(N))
		if N == n * n and isprime(n):
			return 1
		else:
			return 0


class Solution:

	def primarytest(self, N):
		last = int(N ** 0.5)
		for i in range(2, last + 1):
			if N % i == 0:
				return False
		return True

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		check = int(N ** 0.5)
		if int(N ** 0.5) == N ** 0.5:
			if self.primarytest(check):
				return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		if N == 1 or N == 0:
			return 0
		sq = int(math.sqrt(N))
		if sq * sq == N and is_prime(sq):
			return 1
		else:
			return 0

def is_prime(num):
	sq = int(math.sqrt(num))
	for i in range(2, sq + 1):
		if num % i == 0:
			return False
	return True


import math

class Solution:

	def hasThreePrimeFac(self, N):
		cnt = 0
		sq = int(math.sqrt(N))
		for i in range(2, sq + 1):
			if N % i == 0:
				cnt += 1
				if cnt > 1:
					break
		if cnt == 1:
			return 1
		else:
			return 0


import math as m

class Solution:

	def hasThreePrimeFac(self, N):

		def isprime(n):
			if n <= 1:
				return False
			for i in range(2, int(m.sqrt(n) + 1)):
				if n % i == 0:
					return False
			return True
		k = int(m.sqrt(N))
		if k ** 2 == N:
			if isprime(k):
				return 1
		return 0


import math
ub = int(1000000.0)
sieve = [True] * (ub + 1)
sieve[0] = sieve[1] = False
for i in range(2, int(ub ** 0.5) + 1):
	if sieve[i]:
		for j in range(i * i, ub + 1, i):
			sieve[j] = False

class Solution:

	def hasThreePrimeFac(self, N):
		x = math.sqrt(N)
		if x == int(x) and sieve[int(x)]:
			return 1
		else:
			return 0


from math import sqrt
from bisect import bisect_left
primes = [2, 3]

class Solution:

	def hasThreePrimeFac(self, N):
		num = sqrt(N)
		if int(num) != num:
			return 0
		while num > primes[-1]:
			i = primes[-1]
			while True:
				i += 2
				flag = True
				for item in primes:
					if i % item == 0:
						flag = False
						break
				if flag:
					primes.append(i)
					break
		if primes[-1] == num:
			return 1
		return int(primes[bisect_left(primes, int(num))] == num)


import math

class Solution:

	def hasThreePrimeFac(self, N):
		a = math.sqrt(N)
		a = int(a)
		if a * a == N and N != 1:
			xx = a ** 0.5
			countt = 0
			for i in range(2, int(xx) + 1):
				if a % i == 0:
					countt += 1
			if countt > 0:
				return 0
			else:
				return 1
		else:
			return 0


class Solution:

	def pt(self, N):
		l = int(N ** 0.5)
		for i in range(2, l + 1):
			if N % i == 0:
				return False
		return True

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		c = int(N ** 0.5)
		if int(N ** 0.5) == N ** 0.5:
			if self.pt(c):
				return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):

		def isPrime(x):
			if x == 1:
				return False
			if x == 2 or x == 3:
				return True
			for i in range(2, int(math.sqrt(x)) + 1):
				if x % i == 0:
					return False
			return True
		if N == 1:
			return 0
		t = math.sqrt(N)
		if t == int(t) and isPrime(t):
			return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		(lim, f, c) = (math.sqrt(N), 0, 0)
		if lim == int(lim):
			f = 1
		for i in range(1, int(lim)):
			if N % i == 0:
				c += 1
				if c >= 2:
					return 0
		if f:
			fact = 2 * c + 1
		else:
			fact = 2 * c
		return int(fact == 3)


class Solution:

	def hasThreePrimeFac(self, N):
		if N > 1:
			a = N ** 0.5
			for i in range(2, int(a)):
				if N % i == 0:
					return 0
			if a == int(a):
				return 1
		else:
			return 0


class Solution:

	def hasThreePrimeFac(self, N):
		if N > 1:
			a = N ** 0.5
			divi = False
			for i in range(2, int(a)):
				if N % i == 0:
					divi = True
					break
			if a == int(a) and N % a == 0 and (divi == False):
				return 1
			else:
				return 0
		else:
			return 0


class Solution:

	def primefactors(self, ls):
		power_ls = []
		j = 0
		i = 1
		init = ls[0]
		num_fact = 1
		for n in ls:
			if len(power_ls) == 0:
				power_ls.insert(j, i)
			elif init == n:
				i += 1
				power_ls[j] = i
			else:
				j += 1
				i = 1
				power_ls.insert(j, i)
				init = n
		for n in power_ls:
			num_fact = num_fact * (n + 1)
		return num_fact

	def hasThreePrimeFac(self, N):
		sqr1 = N ** (1 / 2)
		ans = 0
		if N == 1:
			return ans
		elif N % sqr1 == 0:
			fac_ls = []
			i = 2
			n = N
			sqrt = N ** (1 / 2)
			while n != 1:
				if n == sqrt:
					break
				while n % i == 0 and n != 0:
					fac_ls.append(i)
					n = n / i
				i += 1
			return 1 if self.primefactors(fac_ls) == 3 else 0
		else:
			return 0


class Solution:

	def hasThreePrimeFac(self, N):

		def isPrime(n):
			k = int(n ** 0.5) + 1
			for i in range(3, k, 2):
				if not n % i:
					return False
			return True
		if N == 4:
			return 1
		if not N % 2 or N < 9:
			return 0
		k = int(N ** 0.5)
		if k ** 2 == N and isPrime(k):
			return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		if N <= 3:
			return 0
		s = int(math.sqrt(N))
		if s * s == N:
			count = 2
			for i in range(2, s + 1):
				if N % i == 0:
					count += 1
			if count == 3:
				return 1
			return 0
		return 0


from math import sqrt

class Solution:

	def perfectSquares(self, N):
		n = int(sqrt(N))
		return n * n == N

	def hasThreePrimeFac(self, N):
		if self.perfectSquares(N):
			factors = [1, N]
			for i in range(2, int(sqrt(N) + 1)):
				if N % i == 0 and i not in factors:
					factors.extend([i, N // i])
			return 1 if len(set(factors)) == 3 else 0
		else:
			return 0


from math import sqrt

class Solution:

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		check = int(N ** 0.5)
		if int(N ** 0.5) == N ** 0.5:
			if self.primarytest(check):
				return 1
		return 0

	def primarytest(self, N):
		last = int(N ** 0.5)
		for i in range(2, last + 1):
			if N % i == 0:
				return False
		return True


class Solution:

	def hasThreePrimeFac(self, N):
		i = 2
		c = 2
		while i * i <= N:
			if N % i == 0:
				c += 1
			if c > 3:
				break
			i += 1
		else:
			if c == 3:
				return 1
		return 0


class Solution:

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		p = N ** 0.5
		p1 = int(N ** 0.5)
		if p != p1:
			return 0
		for i in range(2, p1):
			if N % i == 0:
				return 0
		return 1


import math

class Solution:

	def hasThreePrimeFac(self, N):

		def isprime(N):
			if N == 1:
				return False
			for i in range(2, int(math.sqrt(N)) + 1):
				if N % i == 0:
					return False
			return True
		x = int(math.sqrt(N))
		if N == x * x and isprime(x):
			return 1
		return 0


from math import sqrt
from math import ceil
from math import floor

class Solution:

	def hasThreePrimeFac(self, N):

		def isPrime(n):
			if n <= 1:
				return False
			if n <= 3:
				return True
			if n % 2 == 0 or n % 3 == 0:
				return False
			k = int(sqrt(n)) + 1
			for i in range(5, k, 6):
				if n % i == 0 or n % (i + 2) == 0:
					return False
			return True
		s = int(sqrt(N))
		if isPrime(s) and s * s == N:
			return 1
		else:
			return 0


import math

class Solution:

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


class Solution:

	def hasThreePrimeFac(self, N):

		def isPrime(num):
			if num < 2:
				return 0
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					return 0
			return 1
		if int(N ** 0.5) ** 2 == N and isPrime(int(N ** 0.5)) == 1:
			return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		factor = False
		if N == 1:
			return 0
		for i in range(2, int(math.sqrt(N))):
			if factor == True:
				return 0
			if N % i == 0:
				factor = True
		if factor == True:
			return 0
		else:
			return 1


import math

class Solution:

	def hasThreePrimeFac(self, n):
		if n == 1:
			return 0
		else:
			s = int(n ** 0.5)
			if s * s == n:
				for i in range(2, s):
					if n % i == 0:
						return 0
			else:
				return 0
		return 1


class Solution:

	def primarytest(self, N):
		last = int(N ** 0.5)
		for i in range(2, last + 1):
			if N % i == 0:
				return False
		return True

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		check = int(N ** 0.5)
		if int(N ** 0.5) == N ** 0.5:
			if self.primarytest(check):
				return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


class Solution:

	def hasThreePrimeFac(self, N):
		sqrt = int(N ** 0.5)
		if sqrt * sqrt != N:
			return 0
		if sqrt <= 1:
			return 0
		if sqrt <= 3:
			return 1
		if sqrt % 2 == 0 or sqrt % 3 == 0:
			return 0
		for i in range(5, int(sqrt ** 0.5) + 1, 6):
			if sqrt % i == 0 or sqrt % (i + 2) == 0:
				return 0
		return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


class Solution:

	def hasThreePrimeFac(self, n):
		if n == 1:
			return 0
		else:
			s = int(n ** 0.5)
			if s * s == n:
				for i in range(2, s):
					if n % i == 0:
						return 0
			else:
				return 0
		return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


import math

class Solution:

	def hasThreePrimeFac(self, N):
		count = 0
		for itr in range(2, math.ceil(math.sqrt(N + 1))):
			if N % itr == 0:
				count += 1
			if count > 1:
				return 0
		if count == 1:
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


class Solution:

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


import math

class Solution:

	def hasThreePrimeFac(self, N):
		factor = False
		if N == 1:
			return 0
		for i in range(2, int(math.sqrt(N))):
			if factor == True:
				return 0
			if N % i == 0:
				factor = True
		if factor == True:
			return 0
		else:
			return 1
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


class Solution:

	def hasThreePrimeFac(self, n):
		flag = 1
		s = 2
		if n == 1 or n == 2:
			return 0
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				s += 1
				if s > 3:
					flag = 0
					break
		return flag
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


from math import sqrt

class Solution:

	def hasThreePrimeFac(self, N):
		n = N

		def isPrime(n):
			if n <= 1:
				return 0
			if n <= 3:
				return 1
			if n % 2 == 0 or n % 3 == 0:
				return 0
			k = int(sqrt(n)) + 1
			for i in range(5, k, 6):
				if n % i == 0 or n % (i + 2) == 0:
					return 0
			return 1
		sq = int(sqrt(n))
		if 1 * sq * sq != n:
			return 0
		if isPrime(sq):
			return 1
		else:
			return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


import math

class Solution:

	def hasThreePrimeFac(self, N):
		if N <= 3:
			return 0
		elif N % 2 == 0 or N % 3 == 0 or N % 5 == 0 or (N % 10 == 0) or (N % 15 == 0) or (N % 25 == 0):
			return 0
		else:
			c = 0
			e = int(math.sqrt(N))
			for i in range(2, 2 * e):
				if N % i == 0:
					c += 1
			if c == 1:
				return 1
			else:
				return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		if N == 1:
			return 0
		x = math.sqrt(N)
		if x != math.floor(x):
			return 0
		else:
			i = 2
			while i * i <= x:
				if x % i == 0:
					return 0
				i += 1
			return 1


import math

class Solution:

	def hasThreePrimeFac(self, N):
		if N <= 3:
			return 0
		elif N % 2 == 0 or N % 3 == 0 or N % 10 == 0 or (N % 15 == 0) or (N % 25 == 0):
			return 0
		else:
			c = 0
			e = int(math.sqrt(N))
			for i in range(2, 2 * e):
				if N % i == 0:
					c += 1
			if c == 1:
				return 1
			else:
				return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


import math

class Solution:

	def hasThreePrimeFac(self, N):
		x = int(math.sqrt(N))
		if x * x == N and x > 2:
			for i in range(2, x):
				if x % i == 0:
					return 0
			else:
				return 1
		return 0
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.hasThreePrimeFac(N))


from math import sqrt
from math import floor

class Solution:

	def hasThreePrimeFac(self, N):
		if floor(sqrt(N)) - sqrt(N) == 0 and self.isPrime(N):
			return 1
		return 0

	def isPrime(self, n):
		if n <= 1:
			return False
		for i in range(2, floor(sqrt(n))):
			if n % i == 0:
				return False
		return True


from math import sqrt

def isPrime(n):
	if n <= 1:
		return False
	if n <= 3:
		return True
	if n % 2 == 0 or n % 3 == 0:
		return False
	k = int(sqrt(n)) + 1
	for i in range(5, k, 6):
		if n % i == 0 or n % (i + 2) == 0:
			return False
	return True

class Solution:

	def hasThreePrimeFac(self, n):
		sq = int(sqrt(n))
		if 1 * sq * sq != n:
			return 0
		if isPrime(sq):
			return 1
		else:
			return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		c = 0
		if N == 1:
			return 0
		for j in range(1, int(math.sqrt(N)) + 1):
			if N % j == 0:
				if N // j == j:
					c = c + 1
				else:
					c = c + 2
				if c > 3:
					return 0
		return 1


class Solution:

	def hasThreePrimeFac(self, N):
		if int(N ** 0.5) * int(N ** 0.5) == N:
			n = int(N ** 0.5)
			j = 2
			while j * j <= n:
				if n % j == 0:
					break
				j += 1
			if j * j > n and n != 1:
				return 1
			else:
				return 0
		else:
			return 0


from math import sqrt

def isprime(N):
	if N <= 1:
		return 0
	for i in range(2, int(sqrt(N)) + 1):
		if N % i == 0:
			return 0
	return 1

class Solution:

	def hasThreePrimeFac(self, N):
		sqr = int(sqrt(N))
		if sqr * sqr == N and isprime(sqr) == 1:
			return 1
		return 0


class Solution:

	def hasThreePrimeFac(self, N):
		x = int(N ** 0.5)
		if N < 4:
			return 0
		if int(N ** 0.5) == N ** 0.5:
			flag = True
			for i in range(2, x):
				if x % i == 0:
					flag = False
					return 0
			if flag:
				return 1
		return 0


import math

class Solution:

	def hasThreePrimeFac(self, N):
		sum1 = 2
		for i in range(2, int(math.sqrt(N)) + 1):
			if N % i == 0:
				sum1 = sum1 + 1
				if sum1 > 3:
					break
		if sum1 == 3:
			return 1
		else:
			return 0