Given a number n, the task is to calculate its primorial. Primorial of a number is similar to factorial of a number. In primorial, not all the natural numbers get multiplied; only prime numbers are multiplied to calculate the primorial of a number. It is denoted with P#.
 
Example 1:
Input:
N = 5
Output: 30
Explanation:
Priomorial = 2 * 3 * 5 = 30
As a side note, factorial is 2 * 3 * 4 * 5
 
Example 2:
Input:
N = 12
Output: 2310
Your Task:  
You don't need to read input or print anything. Your task is to complete the function primorial() which takes the integer N as input parameters and returns the primorial of number n. Since the answer can be quite large print it modulo 10^{9}+7.
Expected Time Complexity: O(N*sqrt(N))
Expected Auxiliary Space: 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 primorial (self, N):

        pass

        # code here

    def precompute (self):

        pass

        # code here



Solutions:
class Solution:

	def precompute(self):
		pass

	def primorial(self, N):
		fact = 1
		for i in range(2, N + 1):
			flag = 1
			for j in range(2, i // 2 + 1):
				if i % j == 0:
					flag = 0
					break
			if flag == 1:
				fact *= i
		return fact % 1000000007


import math

class Solution:

	def primorial(self, n):

		def isPrime(n):
			prime_flag = 0
			if n > 1:
				for i in range(2, int(math.sqrt(n)) + 1):
					if n % i == 0:
						prime_flag = 1
						break
				if prime_flag == 0:
					return True
				else:
					return False
			else:
				return False
		ans = 1
		for i in range(2, n + 1):
			if isPrime(i) == True:
				ans *= i
			else:
				continue
		return ans % (10 ** 9 + 7)

	def precompute(self):
		pass


class Solution:

	def precompute(self):
		pass

	def isPrime(self, num):
		i = 2
		while i * i <= num:
			if num % i == 0:
				return False
				break
			i += 1
		return True

	def primorial(self, N):
		mul = 1
		mod = 1000000007
		i = N
		while i >= 2:
			if self.isPrime(i):
				mul *= i
				mul = mul % mod
			i -= 1
		return mul


import math

class Solution:

	def __init__(self):
		self.pl = []

	def primorial(self, N):
		m = int(1000000000.0) + 7
		sl = self.bsf(self.pl, N)
		res = 1
		for i in sl:
			res = res * i
		return res % m

	def precompute(self):
		n = int(100000.0)
		pl = [True] * (n + 1)
		lim = int(math.sqrt(n)) + 1
		pl[0] = pl[1] = False
		for i in range(2, lim):
			for j in range(i * i, n + 1, i):
				if pl[i]:
					pl[j] = False
		self.pl = [i for i in range(2, n + 1) if pl[i]]

	def bsf(self, arr, k):
		(l, h, idx) = (0, len(arr) - 1, -1)
		while l <= h:
			m = (l + h) // 2
			if arr[m] > k:
				h = m - 1
			else:
				idx = m
				l = m + 1
		return arr[:idx + 1]


prime = [True for i in range(100000 + 1)]

class Solution:

	def primorial(self, N):
		mod = int(1000000000.0 + 7)
		res = 1
		for i in range(2, N + 1):
			if prime[i]:
				res *= i
			res = res % mod
		return res

	def precompute(self):
		global prime
		p = 2
		while p * p <= 100000 + 1:
			if prime[p] == True:
				for i in range(p * p, 100000 + 1, p):
					prime[i] = False
			p += 1


class Solution:

	def primorial(self, N):
		isPrime = self.primes(N)
		p = 1
		for i in range(2, N + 1):
			if isPrime[i]:
				p = p * i % 1000000007
		return p

	def primes(self, N):
		isPrime = [True for i in range(N + 1)]
		i = 2
		while i * i <= N + 1:
			if isPrime[i]:
				for j in range(i * i, N + 1, i):
					isPrime[j] = False
			i += 1
		return isPrime

	def precompute(self):
		pass


class Solution:

	def primorial(self, N):

		def is_prime(n):
			i = 2
			while i * i <= n:
				if n % i == 0:
					return False
				i += 1
			return True
		res = 1
		mod = 1000000007
		for i in range(2, N + 1):
			if is_prime(i):
				res *= i
				res = res % mod
		return res
		pass

	def precompute(self):
		pass


class Solution:

	def primorial(self, N):
		l = [0 for i in range(N + 1)]
		b = []
		i = 2
		while i * i <= N:
			h = i * i
			while h <= N:
				l[h] = 1
				h += i
			i += 1
		p = 1
		for i in range(2, N + 1):
			if l[i] == 0:
				p = p * i
				p = p % 1000000007
		return p


class Solution:

	def primorial(self, N):
		pass
		Mod = 10 ** 9 + 7
		primes = [0] * (N + 1)
		for i in range(2, N + 1):
			primes[i] = i
		for i in range(2, int(N ** 0.5) + 1):
			if primes[i] == i:
				for j in range(i * i, N + 1, i):
					primes[j] = 0
		prod = 1
		for i in range(2, len(primes)):
			if primes[i] == i:
				prod = prod % Mod * i % Mod % Mod
		return prod

	def precompute(self):
		pass


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

class Solution:

	def primorial(self, N):
		x = 1
		for i in range(2, N + 1):
			if isprime(i):
				x *= i
		return x % (10 ** 9 + 7)

	def precompute(self):
		pass


class Solution:

	def primorial(self, N):
		pass
		p = 2
		prime = [True for i in range(N + 1)]
		while p * p <= N:
			if prime[p]:
				for i in range(p * p, N + 1, p):
					prime[i] = False
			p = p + 1
		l = []
		for i in range(2, N + 1):
			if prime[i]:
				l.append(i)
		p = 1
		for i in l:
			p = p * i
		return p % 1000000007

	def precompute(self):
		pass


import math

class Solution:

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

	def primorial(self, N):
		f = 1
		for i in range(2, N + 1):
			if self.isprime(i):
				f *= i
		return f % 1000000007

	def precompute(self):
		pass


import math

class Solution:

	def primorial(self, N):
		l = 100001
		siv = [0] * l
		siv[0] = 1
		siv[1] = 1
		for i in range(2, int(math.sqrt(l)) + 1):
			if siv[i] == 0:
				for j in range(2 * i, l, i):
					siv[j] = 1
		p = 1
		for i in range(2, N + 1):
			if siv[i] == 0:
				p *= i
		return p % (10 ** 9 + 7)

	def precompute(self):
		pass