728x90

import sys

minimum, maximum = [int(x) for x in sys.stdin.readline().split()]


def prime_candidate_generator_less_than(n):
    # 5, 7, 11, 13, 17, 19, ...
    candidate = 5
    increment = 2
    increment_changer = {2: 4, 4: 2}
    while candidate <= n:
        yield candidate
        candidate += increment
        increment = increment_changer[increment]


def get_primes_by_sieve_less_than(n):
    if n == 1:
        return []
    elif n == 2:
        return [2]
    elif n == 3:
        return [2, 3]
    else:
        prime_list = [2, 3]
        sieve_list = [True] * (n + 1)
        for candidate in prime_candidate_generator_less_than(n):
            if sieve_list[candidate]:
                prime_list.append(candidate)
                for i in range(2, 1 + (n // candidate)):
                    sieve_list[candidate * i] = False
        return prime_list


res = get_primes_by_sieve_less_than(maximum)
for i in res:
    if i >= minimum:
        print(i)
  • 포인트
    • 에라토스테네스 체(sieve) 사용
728x90

'Algorithm-Problems > 백준' 카테고리의 다른 글

[백준][1978] 소수찾기  (0) 2022.05.08
[백준][1934] 최소공배수  (0) 2022.05.08
[백준][1759] 암호 만들기  (0) 2022.02.09
[백준][1707] 이분 그래프  (0) 2022.02.09
[백준][1697] 숨바꼭질  (0) 2022.02.06

+ Recent posts