機械試験のアルゴリズムの説明:第24題の大水題:誰が素数ですか?


/*
  :      1     1    
  n      sqrt(n)   y, z=n/y    n   ,   <=sqrt(n)


  :
1  sqrt(n)       ,       ,                  
*/


#include <stdio.h>
#include <stdlib.h>
#include <math.h>


bool isPrime(int a)
{
	if(a < 1)
	{
		return false;
	}
	else
	{
		int iBound = (int)sqrt(a*1.0);
		//for(int i = 2 ; i <= sqrt(a) ; i++)//  :  
		for(int i = 2 ; i < iBound ; i++)
		{
			if(0== a%i )
			{
				return false;
			}
		}
		return true;
	}
}


int main(int argc,char* argv[])
{
	int iNum;
	while(EOF!=scanf("%d",&iNum))
	{
		if(isPrime(iNum))
		{
			printf("Yes
"); } else { printf("NO
"); } } system("pause"); getchar(); return 0; }