<swustoj>?id=189 素数判定

链接http://acm.swust.edu.cn/problem/0189/

素数http://baike.baidu.com/view/10626.htm?fromtitle=%E7%B4%A0%E6%95%B0&fromid=115069&type=syn

在一般领域,对正整数n,如果用2到  之间的所有整数去除,均无法整除,则n为质数

#include <stdio.h>
#include <math.h>
int main()
{
    void swap(int &a,int &b);
    bool primenumber(int num);
    int i;
    int count;
    int left,right;
    while(~scanf("%d %d",&left,&right))
    {
        count=0;
        swap(left,right);
        for(i=left;i<=right;i++)
        {
            if(primenumber(i))
            {
                count++;
            }
        }
        printf("%d\n",count);
    }
    return 0;
}
void swap(int &a,int &b)
{
    if(a>b)
    {
        int temp=a;
        a=b;
        b=temp;
    }
}
bool primenumber(int num)
{
    int i;
    for(i=2;i<=sqrt(num);i++)
    {
        if(num%i==0)
        {
            return false;
        }
    }
    return true;
}

 

posted @ 2016-07-30 12:18  艹kiss灬不离  阅读(402)  评论(0编辑  收藏  举报