[[練習問題/回答例]]

**素数判定 [#rafca80c]
#geshi(cpp){{
#include <iostream>
#include <cmath>
#include <sstream>

using namespace std;
int main( int argc, char** argv ){
	int x;
	bool is_prime = true;

	if(argc < 2){
		cout << "Input x > ";
		cin >> x;
	}else{
		istringstream is(argv[1]);
		is >> x;
	}

	for( int i = 2; i < static_cast<int>(sqrt(x)) + 1; i++){
		if(( x % i ) == 0 ){
			is_prime = false;
			break;
		}
	}

	if( is_prime )
		cout << x << "は素数" << endl;
	else
		cout << x << "は非素数" << endl;
	return 0;
}
}}


トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS