"BOJ 4948 베르트랑 공준"의 두 판 사이의 차이

1번째 줄: 1번째 줄:
[[분류: BOJ 10단계]]
[[분류: BOJ 10단계|4]]
[[분류: 소수]]
[[분류: 소수]]
{{DEFAULTSORT:4}}
==개요==
==개요==
* {{BOJ|4948}}
* {{BOJ|4948}}

2018년 7월 20일 (금) 07:48 판

1 개요

BOJ 4948 베르트랑 공준

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

  • n이 주어졌을때, n보다 크고 2n보다 작거나 같은 소수를 구하는 문제
  • 알고리즘 분류: 에라토스테네스의 체, 구현

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;
	}
	static int bertrand_count(int n) {
		int count = 0;
		for(int i=n+1; i<=2*n; i++) {
			if( is_prime(i) ) count++;
		}
		return count;
	}
	public static void main(String args[]) {
		Scanner sc = new Scanner(System.in);
		int n;
		while( true ) {
			n = sc.nextInt();
			if( n == 0 ) return;
			System.out.println( bertrand_count(n) );
		}
	}
}

3 같이 보기

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