"BOJ 1929 소수 구하기"의 두 판 사이의 차이

30번째 줄: 30번째 줄:
}
}
}
}
</source>
==Python==
<source lang='python'>
def is_prime(x):
    import math
    if x<2: return False
    for i in range(2,int(math.sqrt(x))+1):
        if x%i==0: return False
    return True
M,N=map(int,input().split())
for i in range(M, N+1):
    if(is_prime(i)): print(i)
</source>
</source>



2018년 8월 25일 (토) 01:13 판

1 개요

BOJ 1929 소수 구하기

[[분류:BOJ {{{단계}}}단계]]

  • 에라토스테네스의 체를 구현해 봅니다
  • 알고리즘 분류: 에라토스테네스의 체
BOJ 단계별로 풀어보기
순번 문제 풀이

틀:BOJ 10단계 틀:BOJ 단계 푸터

2 Java

import java.util.Scanner;
public class Main {
	static boolean is_prime(int n) {
		if( n < 2 ) return false;
		if( n < 4 ) return true;
		if( n%2==0 || n%3==0 ) return false;
		for(int i=5; i*i<=n; i+=6 ) if(n%i==0 || n%(i+2)==0) return false;
		return true;
	}
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int M = sc.nextInt();
		int N = sc.nextInt();
		for(int i=M; i<=N; i++) {
			if( is_prime(i) ) System.out.println(i);
		}
	}
}

3 Python

def is_prime(x):
    import math
    if x<2: return False
    for i in range(2,int(math.sqrt(x))+1):
        if x%i==0: return False
    return True
M,N=map(int,input().split())
for i in range(M, N+1):
    if(is_prime(i)): print(i)

4 같이 보기

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}