<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;
}
时间: 2024-07-31 03:16:59

<swustoj>?id=189 素数判定的相关文章

数学#素数判定Miller_Rabin+大数因数分解Pollard_rho算法 POJ 1811&amp;2429

素数判定Miller_Rabin算法详解: http://blog.csdn.net/maxichu/article/details/45458569 大数因数分解Pollard_rho算法详解: http://blog.csdn.net/maxichu/article/details/45459533 然后是参考了kuangbin的模板: http://www.cnblogs.com/kuangbin/archive/2012/08/19/2646396.html 模板如下: //快速乘 (a

素数判定(给你两个数a、b,现在的问题是要判断这两个数组成的区间内共有多少个素数)

1 #include<stdio.h> 2 #include<math.h> 3 int func(int x)//自定义函数实现寻找素数功能 4 { 5 int i, flag = 1; 6 for (i = 2; i <= (int)sqrt((float)x); i++) //取到平方根就好,(float)x,强制将int x型转化成float型,再将平方根转化为int型 7 { 8 if (x%i == 0) //是合数,则标记 9 flag = 0; 10 } 11

素数判定 AC 杭电

素数判定 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 87861    Accepted Submission(s): 30699 Problem Description 对于表达式n^2+n+41,当n在(x,y)范围内取整数值时(包括x,y)(-39<=x<y<=50),判定该表达式的值是否都为素数. Input 输入数

[Miller-Rabin][CODEVS1702]素数判定2 解题报告

题面描述:判定一个数P∈[1,2^63-1]∩N是素数么. 按照朴素的判定素数方法,至少也需要O(P^0.5)的,但这道题就是霸气到连这样的时间复杂度都过不了的地步. 实在是不会做了,就学习了传说中的Miller-Rabin素数判定法. 两个引理: ①费马小定理: 设p为质数,且不满足p|a, 则a^(p-1)=a(mod p). 证: 又一个引理,若n与p互质,且a与p互质,则n*a与p互质. 这真的是一个看似很简单的引理,但它却意味着一些看似不那么简单的事情. 设A=(0,p)∩N,则 ①对

miller_robin大素数判定

参考了ACdreamer大神的博客http://blog.csdn.net/acdreamers/article/details/7913786 在51nod上看了个10^30范围的素数判定,打表肯定是不行了,应该用miller-robin素数判定加java的BigInteger 首先基于fermat小定理就是gcd(a,p)=1时,a^(p-1)%p=1,所以在生成rand(2,p-1)的随机数后,如果输入的是个素数,那么必有pow(a,p-1)%p=1,这里采用快速幂取模,在一次探查后,若结

1702 素数判定 2

1702 素数判定 2 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 题目描述 Description 一个数,他是素数么? 设他为P满足(P<=263-1) 输入描述 Input Description P 输出描述 Output Description Yes|No 样例输入 Sample Input 2 样例输出 Sample Output Yes 数据范围及提示 Data Size & Hint 算法导论——数论那一节注意Carmich

1430 素数判定

1430 素数判定 时间限制: 1 s 空间限制: 1000 KB 1 #include<iostream> 2 #include<cstdio> 3 #include<cmath> 4 using namespace std; 5 int vis[30001]; 6 int main() 7 { 8 int n; 9 cin>>n; 10 for(int i=2;i<=n;i++) 11 { 12 if(vis[i]==0) 13 { 14 for(

素数判定相关资料

素数(质数)的判定 (1)最基本素数判定方法大家熟悉,只用看看2到n(或n的平方根)之间有没有n的约数: #include<stdio.h> void main() { int i,n; scanf("%d",&n); for(i=2;i<n;i++) if(n%i==0)break; if(i<n||n==1)puts("No"); else puts("Yes"); } 此方法适用于判定较少数,数据量大时会超时

&lt;swustoj&gt;?id=203 Jack&#39;s problem

链接http://acm.swust.edu.cn/problem/203/ #include <stdio.h> int main() { double x1,x2,x3,y1,y2,y3; double s; while(~scanf("%lf %lf %lf %lf %lf %lf",&x1,&y1,&x2,&y2,&x3,&y3)) { s=(x1*y2+x2*y3+x3*y1-x1*y3-x2*y1-x3*y2)*1