Given a number N find the product of all unique prime factors of that number.
 
Example 1:
Input:
N = 5
Output:
5
Explanation:
5 has 1 prime factor 
i.e 5 only.
Example 2:
Input:
N = 24
Output:
6
Explanation:
24 has 2 prime factors 
3 and 2 where 3*2 = 6
Your Task:
You don't need to read input or print anything. Your task is to complete the function primeProduct() which takes an integer N as input parameters and returns an integer, product of all the prime factors of N.
 
Expected Time Complexity: O(sqrt(N))
Expected Space Complexity: O(1)
 
Constraints:
1 <= N <= 10^{5}

Source: geeksforgeeks | Difficulty: EASY
Tags: Number theory

Mathematics

Starter code: #User function Template for python3

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

Solutions:
import math

class Solution:

	def primeProduct(self, N):

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


from math import *

class Solution:

	def primeProduct(self, N):
		p = [1] * 10 ** 5
		p[0] = 0
		p[1] = 0
		sq = int(sqrt(N))
		for i in range(2, sq + 1):
			if p[i] == 1:
				for j in range(i * i, 10 ** 5, i):
					p[j] = 0
		pr = 1
		for i in range(2, N + 1):
			if p[i] and N % i == 0:
				pr *= i
		return pr


import math

class Solution:

	def primeProduct(self, N):
		ans = 1
		if N % 2 == 0:
			ans *= 2
			while N % 2 == 0:
				N = N // 2
		for i in range(3, int(math.sqrt(N) + 1), 2):
			if N % i == 0:
				ans *= i
				while N % i == 0:
					N = N // i
		if N > 1:
			ans *= N
		return ans


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

class Solution:

	def primeProduct(self, N):
		p = 1
		for i in range(1, N + 1):
			if N % i == 0 and prime(i) == True:
				p *= i
		return p


class Solution:

	def primeProduct(self, N):

		def isPrime(n):
			if n == 1:
				return False
			else:
				lim = int(n ** 0.5) + 1
				for i in range(2, lim):
					if n % i == 0:
						return False
				return True
		(res, lim) = (1, int(N ** 0.5) + 1)
		for i in range(1, lim):
			if N % i == 0:
				if i == N // i:
					if isPrime(i):
						res = res * i
				else:
					if isPrime(i):
						res = res * i
					if isPrime(N // i):
						res = res * (N // i)
		return res


import functools

class Solution:

	def primeProduct(self, N):
		i = 1
		j = N
		factors = []
		while i < j:
			if N % i == 0:
				j = N // i
				factors.append([i, j])
			i += 1
		set_f = set()
		for i in factors:
			set_f.add(i[0])
			set_f.add(i[1])
		return functools.reduce(lambda x, y: x * y, list(filter(self.is_prime, set_f)))

	def is_prime(self, N):
		i = 2
		j = 1
		while i * i <= N:
			if N % i == 0:
				return 0
			i = 2 * j + 1
			j += 1
		return 1


import math

class Solution:

	def primeProduct(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
		prod = 1
		for i in range(2, N + 1):
			if N % i == 0 and isprime(i):
				prod *= i
		return prod


class Solution:

	def primeProduct(self, N):
		x = 1
		i = 2
		while i * i <= N:
			f = False
			while N % i == 0:
				N //= i
				f = True
			if f:
				x *= i
			i += 1
		if N >= 2:
			x *= N
		return x


class Solution:

	def primeProduct(self, N):
		p = 2
		k = 1
		l = []
		while N > 1:
			if N % p == 0:
				if p not in l:
					k = k * p
				l.append(p)
				N = N // p
			else:
				p = p + 1
		return k


from math import sqrt

class Solution:

	def primeProduct(self, N):
		prod = 1
		for i in range(2, int(sqrt(N)) + 1):
			flag = False
			while N % i == 0:
				N = N // i
				flag = True
			if flag:
				prod = prod * i
		if N > 1:
			return prod * N
		return prod


class Solution:

	def primeProduct(self, N):

		def p(a):
			if a == 0 or a == 1:
				return False
			s = int(a ** 0.5)
			for i in range(2, s + 1):
				if a % i == 0:
					return False
			return True
		e = 1
		for i in range(2, N + 1):
			if N % i == 0:
				if p(i):
					e *= i
		return e


class Solution:

	def primeProduct(self, N):
		k = int(N ** 0.5)
		s = {}
		for i in range(2, k + 1):
			while N % i == 0:
				s[i] = 1
				N = N // i
		if N != 1:
			s[N] = 1
		p = 1
		for i in s:
			p = p * i
		return p


def prime(n):
	for i in range(2, n):
		if n % i == 0:
			return 0
	return 1

class Solution:

	def primeProduct(self, n):
		p = 1
		for i in range(2, n + 1):
			if n % i == 0:
				if prime(i):
					p = p * i
		return p


class Solution:

	def primeProduct(self, N):
		spf = []
		for i in range(N + 1):
			spf.append(i)
		for i in range(2, int(N ** 0.5) + 1):
			if spf[i] == i:
				for j in range(i * i, N + 1, i):
					if spf[j] == j:
						spf[j] = i
		ans = []
		mul = 1
		while N > 1:
			x = spf[N]
			if x not in ans:
				mul *= x
			while N % x == 0:
				N = N // x
		return mul


import math as mt

class Solution:

	def primeProduct(self, N):

		def prime(n):
			count = 0
			for i in range(2, n):
				if n % i == 0:
					count += 1
			if count == 0:
				return True
			else:
				return False
		l = []
		for j in range(1, N + 1):
			if N % j == 0 and prime(j) == True:
				l.append(j)
		mul = 1
		for i in range(len(l)):
			mul = mul * l[i]
		return mul


import math

class Solution:

	def primeProduct(self, N):
		product = 1
		if N % 2 == 0:
			product *= 2
			while N % 2 == 0:
				N = N / 2
		for i in range(3, int(math.sqrt(N)) + 1, 2):
			if N % i == 0:
				product *= i
				while N % i == 0:
					N = N / i
		if N > 2:
			product *= N
		return int(product)
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		N = int(input())
		ob = Solution()
		print(ob.primeProduct(N))


class Solution:

	def primeProduct(self, N):

		def isprime(num):
			flag = True
			for i in range(2, int(num ** 0.5) + 1):
				if num % i == 0:
					flag = False
					break
			return flag
		fact = 1
		for i in range(2, N + 1):
			if N % i == 0 and isprime(i):
				fact = fact * i
		return fact


class Solution:

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

	def primeProduct(self, N):
		primes = []
		for i in range(1, N + 1):
			if N % i == 0 and self.checkPrime(i):
				primes.append(i)
		p = 1
		for i in primes:
			p = p * i
		return p


class Solution:

	def primeProduct(self, N):
		prime = [True for i in range(N + 1)]
		p = 2
		while p * p <= N:
			if prime[p]:
				for i in range(p * p, N + 1, p):
					prime[i] = False
			p = p + 1
		res = 1
		for i in range(2, N + 1):
			if prime[i] and N % i == 0:
				res = res * i
		return res


import math

class Solution:

	def primeProduct(self, N):
		if N == 1:
			return 1
		c = 0
		l = []
		p = 1
		arr = [True for i in range(N)]
		arr[0] = False
		arr[1] = False
		for i in range(2, int(math.sqrt(N)) + 1):
			if N % i == 0:
				c += 1
			if arr[i] == True:
				for j in range(i * i, N, i):
					arr[j] = False
		for i in range(N):
			if arr[i] and N % i == 0:
				p = p * i
		if c == 0:
			return N * p
		return p


class Solution:

	def primeProduct(self, n):
		qw = n
		p = [1] * (n + 2)
		p[0] = 0
		p[1] = 1
		for i in range(2, n + 2):
			if p[i] == 1:
				p[i] = i
				for j in range(i * i, n + 2, i):
					if p[j] == 1:
						p[j] = i
		l = 1
		q = qw
		s = []
		while p[q] > 1:
			r = p[q]
			if r not in s:
				l *= r
				s.append(r)
			q //= r
		return l


class Solution:

	def primeProduct(self, N):
		k = 1
		l = []
		p = 2
		primes = [True for i in range(N + 1)]
		while p * p <= N:
			if primes[p] == True:
				for i in range(p * p, N + 1, p):
					primes[i] = False
			p += 1
		for j in range(2, N + 1):
			if primes[j]:
				l.append(j)
		for i in l:
			if N % i == 0:
				k = k * i
		return k


import math

class Solution:

	def primeProduct(self, N):

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


class Solution:

	def primeProduct(self, N):
		prime = [1 for i in range(N + 1)]
		prime[0] = 0
		prime[1] = 0
		l = 1
		p = 2
		while p * p <= N:
			if prime[p] == 1:
				for i in range(p * p, N + 1, p):
					prime[i] = 0
			p += 1
		for i in range(0, N + 1, 1):
			if prime[i] == 1 and N % i == 0:
				l *= i
		return l


import math as m

class Solution:

	def primeProduct(self, N):

		def isprime(n):
			for i in range(2, int(m.sqrt(n) + 1)):
				if n % i == 0:
					return False
			return True
		l = []
		for i in range(2, N + 1):
			if N % i == 0:
				l.append(i)
		temp = 1
		for i in l:
			if isprime(i):
				temp *= i
		return temp


class Solution:

	def primeProduct(self, N):
		product = 1
		for i in range(1, N + 1):
			if N % i == 0:
				if i == 1:
					pass
				elif i == 2:
					product *= 2
				else:
					result = True
					for j in range(2, i):
						if i % j == 0:
							result = False
							break
						else:
							pass
					if result:
						product *= i
					else:
						pass
		return product


class Solution:

	def primeProduct(self, N):
		product = 1
		for i in range(2, N + 1):
			if N % i == 0:
				isPrime = 1
				for j in range(2, int(i / 2 + 1)):
					if i % j == 0:
						isPrime = 0
						break
				if isPrime:
					product = product * i
		return product


import math

class Solution:

	def primeProduct(self, N):
		product = 1
		for i in range(2, N + 1):
			if N % i == 0:
				isPrime = True
				for j in range(2, int(math.sqrt(i)) + 1):
					if i % j == 0:
						isPrime = False
						break
				if isPrime:
					product *= i
		return product


class Solution:

	def primeProduct(self, N):
		k = 1
		l = []
		m = []
		for i in range(1, N + 1):
			if N % i == 0:
				l.append(i)
		for i in l:
			for j in range(2, i // 2 + 1):
				if i % j == 0:
					m.append(i)
					break
		for i in l:
			k *= i
		for i in m:
			k //= i
		return k


class Solution:

	def primeProduct(self, N):
		c = 1
		import math
		for i in range(2, N + 1):
			if N % i == 0:
				p = 0
				for j in range(2, int(math.sqrt(i)) + 1):
					if i % j == 0:
						p = 1
						break
				if p == 0:
					c = c * i
		return c


from math import sqrt

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

def fun2(n):
	div2 = set()
	for i in range(1, int(sqrt(n)) + 1):
		if n % i == 0:
			if approach2(i):
				div2.add(i)
			if approach2(n // i):
				div2.add(n // i)
	return list(div2)

class Solution:

	def primeProduct(self, N):
		p = 1
		for i in fun2(N):
			p *= i
		return p


class Solution:

	def primeProduct(self, N):
		l = []
		d = 2
		p = 1
		while d <= N:
			if N % d == 0:
				l.append(d)
				N //= d
			else:
				d += 1
		l = list(set(l))
		for i in l:
			p *= i
		return p


class Solution:

	def isprime(n):
		for i in range(2, int(n ** 0.5) + 1):
			if n % i == 0:
				return False
		return True

	def primeProduct(self, N):
		l = []
		for i in range(1, N + 1):
			if N % i == 0:
				l.append(i)
		p = [x for x in l if Solution.isprime(x)]
		r = 1
		for i in p:
			r *= i
		return r


class Solution:

	def primeProduct(self, N):
		divisor = 2
		fector = []
		product = 1
		while divisor <= N:
			if N % divisor == 0:
				fector.append(divisor)
				N = N / divisor
			else:
				divisor += 1
		commonfector = list(set(fector))
		for j in commonfector:
			product *= j
		return product


class Solution:

	def primeProduct(self, N):
		pd = 1
		for i in range(1, N + 1):
			p = True
			if N % i == 0:
				if i == 2:
					p = True
				elif i <= 1:
					p = False
				else:
					for j in range(2, i):
						if i % j == 0:
							p = False
				if p:
					pd *= i
		return pd


def isprime(i):
	count = 0
	for j in range(1, i + 1):
		if i % j == 0:
			count += 1
	if count == 2:
		return True
	else:
		return False

class Solution:

	def primeProduct(self, n):
		k = 1
		for i in range(1, n + 1):
			if n % i == 0:
				if isprime(i) == True:
					k = k * i
		return k


def prime(n):
	if n == 1:
		return False
	c = 0
	for i in range(2, n + 1):
		if n % i == 0:
			c += 1
	if c == 1:
		return True
	return False

class Solution:

	def primeProduct(self, N):
		if N == 1:
			return 1
		k = 1
		for i in range(1, N + 1):
			if N % i == 0:
				if prime(i) == True:
					k *= i
		return k


class Solution:

	def primeProduct(self, N):
		p = []
		for i in range(2, N + 1):
			if N % i == 0:
				p.append(i)
		mul = 1
		for i in p:
			for j in range(2, i):
				if i % j == 0:
					break
			else:
				mul = mul * i
		return mul


import math

class Solution:

	def primeProduct(self, n):
		p = 1
		if n % 2 == 0:
			p *= 2
			while n % 2 == 0:
				n = n / 2
		for i in range(3, int(math.sqrt(n)) + 1, 2):
			if n % i == 0:
				p = p * i
				while n % i == 0:
					n = n / i
		if n > 2:
			p = p * n
		return int(p)


class Solution:

	def primeRange(self, n):
		if n == 0 or n == 1:
			return []
		primes = [1] * (n + 1)
		primes[0] = primes[1] = 0
		for i in range(4, n + 1, 2):
			primes[i] = 0
		for i in range(3, n + 1, 2):
			if primes[i] == 1:
				for j in range(2 * i, n + 1, i):
					primes[j] = 0
		return [x for x in range(n + 1) if primes[x] and primes[x]]

	def primeProduct(self, N):
		arr = self.primeRange(N)
		ans = 1
		for x in arr:
			if N % x == 0:
				ans *= x
		return ans


def prime(n):
	flag = 1
	for i in range(2, n // 2 + 1):
		if n % i == 0:
			flag = -1
			break
	return flag

class Solution:

	def primeProduct(self, N):
		result = 1
		for i in range(2, N + 1):
			if N % i == 0 and prime(i) == 1:
				result = result * i
		return result