Skip to content

Instantly share code, notes, and snippets.

@galihboy
Created December 18, 2021 10:53
Show Gist options
  • Save galihboy/6a198d456f06fbdd68455b0dcae3cbac to your computer and use it in GitHub Desktop.
Save galihboy/6a198d456f06fbdd68455b0dcae3cbac to your computer and use it in GitHub Desktop.
Deret bilangan bukan prima
"""
Galih Hermawan
https://blog.galih.eu
"""
# pemeriksaan bilangan prima
def prima(angka):
for i in range(2, angka):
if angka%i == 0:
return False
else:
return True
# list bilangan bukan prima
def deretBukanPrima(bmin, bmaks):
lst = []
for i in range(bmin, bmaks+1):
if not prima(i):
lst.append(i)
return lst
# seleksi deret bilangan bukan prima yang berurutan atau selisih satu, misal: 14, 15, 16
def seleksi(listAngka):
res = [[]]
for item1, item2 in zip(listAngka, listAngka[1:]):
if item2 - item1 == 1:
if not res[-1]:
res[-1].extend((item1, item2))
else:
res[-1].append(item2)
elif res[-1]:
res.append([])
if not res[-1]:
del res[-1]
return res
#----------------------------------
bMin = 2 # batas bawah deret
bMaks = 1000 # batas atas deret
jml_min_angka = 10 # jumlah urutan angka minimal bukan prima yang diinginkan
deretAngka = deretBukanPrima(bMin, bMaks)
hasilSeleksi = seleksi(deretAngka)
for i in hasilSeleksi:
if len(i) >= jml_min_angka:
print(f"Jumlah {len(i)} angka = {i}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment