C++ isPrime()

Jmnote (토론 | 기여)님의 2024년 1월 4일 (목) 23:26 판 (새 문서: ==개요== ;C++ isPrime() <syntaxhighlight lang='cpp' run> #include <iostream> using namespace std; bool isPrime(int x) { if(x < 2) return false; for(int i=2; i*i<=x; i++) {...)
(차이) ← 이전 판 | 최신판 (차이) | 다음 판 → (차이)

1 개요[ | ]

C++ isPrime()
#include <iostream>
using namespace std;

bool isPrime(int x) {
    if(x < 2) return false;
    for(int i=2; i*i<=x; i++) {
        if(x%i == 0) return false;
    }
    return true;
}

int main() {
	for(int i=0; i<30; i++) {
		if(isPrime(i)) cout << i << ' '; // 2 3 5 7 11 13 17 19 23 29 
	}
}

2 같이 보기[ | ]

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