Skip to content

Commit 4b6623f

Browse files
authored
Update primorial.py
The previous implementation used the import from sympy which I don't think provided much value to the repository.
1 parent 2b71abd commit 4b6623f

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

Primorials/primorial.py

+20-12
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
1-
from sympy import primorial
1+
def nats(n):
2+
i = n
3+
while True:
4+
yield i
5+
i += 1
26

3-
###############################
4-
### Primorials ###
5-
###############################
7+
def sieve(s):
8+
n = next(s)
9+
yield n
10+
yield from sieve(i for i in s if i%n != 0)
611

7-
#Explanation : The product of the first n primes.
8-
#Example : 1, 2, 6, 30, 210, 2310, 30030, 510510, 9699690, 223092870, ...
12+
def primoral(n):
13+
res = 1
14+
i = 1
15+
primes = sieve(nats(2))
16+
while i <= n:
17+
res *= next(primes)
18+
i += 1
19+
return res
920

10-
11-
n = int(input("Enter the nth value to find : "))
12-
if(n == 0):
13-
print(n,"th primorial term is 1")
14-
else:
15-
print(n,"th primorial term is ",primorial(n))
21+
n = int(input('Enter n: '))
22+
print(primoral(n))
23+

0 commit comments

Comments
 (0)