Created
April 25, 2025 08:52
-
-
Save Khuirul-Huda/319fdeb0d6c84d6c30cf33be69b84afe to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sieve_of_eratosthenes(limit): | |
print("Masukkan batas bilangan yang diuji:") | |
if limit < 2: | |
return [] | |
# Step 6: Coret angka 0 dan 1 | |
bilangan = [True] * (limit + 1) | |
bilangan[0] = bilangan[1] = False | |
# Step 8: Mulai dari p = 2 | |
p = 2 | |
while p < limit: | |
if bilangan[p]: | |
# Step 10: Coret semua kelipatan p, kecuali p itu sendiri | |
for kelipatan in range(p * 2, limit + 1, p): | |
bilangan[kelipatan] = False | |
# Step 11: Pindah ke angka berikutnya yang belum dicoret | |
p += 1 | |
# Step 14: Laporin angka-angka yang masih True sebagai bilangan prima | |
primes = [i for i, is_prime in enumerate(bilangan) if is_prime] | |
return primes | |
# Interaksi dengan user | |
try: | |
batas = int(input("Masukkan batas atas (>=2): ")) | |
hasil = sieve_of_eratosthenes(batas) | |
print(f"Bilangan prima antara 2 dan {batas}:") | |
print(hasil) | |
except ValueError: | |
print("Bro, tolong masukin angka bulat ya 😅") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment