Skip to content

Commit 132e93d

Browse files
authored
Print the nth Sphenic Number
A Sphenic number is a product of 3 distinct primes.
1 parent bb9dbfe commit 132e93d

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

Sphenic Numbers/sphenic_numbers.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import math
2+
3+
def primes(n):
4+
sieve = [True] * n
5+
for i in range(3, int(n**0.5) + 1, 2):
6+
if sieve[i]:
7+
for j in range(2*i, n, i):
8+
sieve[j] = False
9+
return [2] + [i for i in range(3, n, 2) if sieve[i]]
10+
11+
def isSpehnic(num):
12+
p = primes(num)
13+
# The very smallest combiantion of primes would contain 2 and 3
14+
# We only need to check numbers <= num/6
15+
p = [n for n in p if n <= num//6]
16+
l = len(p)
17+
for i in range(l - 2):
18+
for j in range(i+1, l-1):
19+
for k in range(j+1, l):
20+
if p[i] * p[j] * p[k] == num:
21+
return True
22+
return False
23+
24+
def main():
25+
n = int(input('Enter n: '))
26+
count = -1
27+
curr = 0
28+
while count < n:
29+
curr += 1
30+
if isSpehnic(curr):
31+
count += 1
32+
print(curr)
33+
34+
main()

0 commit comments

Comments
 (0)