September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
37
Task/Almost-prime/Python/almost-prime-2.py
Normal file
37
Task/Almost-prime/Python/almost-prime-2.py
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# k-Almost-primes
|
||||
# Python 3.6.3
|
||||
# no imports
|
||||
|
||||
def prime_factors(m=2):
|
||||
|
||||
for i in range(2, m):
|
||||
r, q = divmod(m, i)
|
||||
if not q:
|
||||
return [i] + prime_factors(r)
|
||||
return [m]
|
||||
|
||||
def k_almost_primes(n, k=2):
|
||||
multiples = set()
|
||||
lists = list()
|
||||
for x in range(k+1):
|
||||
lists.append([])
|
||||
|
||||
for i in range(2, n+1):
|
||||
if i not in multiples:
|
||||
if len(lists[1]) < 10:
|
||||
lists[1].append(i)
|
||||
multiples.update(range(i*i, n+1, i))
|
||||
print("k=1: {}".format(lists[1]))
|
||||
|
||||
for j in range(2, k+1):
|
||||
for m in multiples:
|
||||
l = prime_factors(m)
|
||||
ll = len(l)
|
||||
if ll == j and len(lists[j]) < 10:
|
||||
lists[j].append(m)
|
||||
|
||||
print("k={}: {}".format(j, lists[j]))
|
||||
|
||||
k_almost_primes(200, 5)
|
||||
# try:
|
||||
#k_almost_primes(6000, 10)
|
||||
Loading…
Add table
Add a link
Reference in a new issue