C++——2828: 素数判断

Description

输入一个大于1的正整数,如果是素数则输出“yes”,如果不是素数,则输出“no”,

Input

一个大于1的正整数

Output

如果该整数是素数则输出“yes”,如果不是素数,则输出“no”

/* Copyright (c) 2014, 烟台大学计算机学院
 * All rights reserved.
 * 文件名称:test.cpp
 * 作者:陈丹妮
 * 完成日期:2015年 5 月 20 日
 * 版 本 号:v1.0
 */

#include <iostream>
#include <cmath>
using namespace std;
int is_prime(int n)
{
    int r;
    if(n==1)
        return 0;
    else
    {
        for (r=2; r<=sqrt(n); ++r)
            if(n%r==0)
                break;
        if(r>sqrt(n))
            return 1;
    }
}
int main()
{
    int flag,n;
    int is_prime(int);
    cin>>n;
    flag=is_prime(n);
    if(flag==1)
        cout<<"yes"<<endl;
    else
        cout<<"no"<<endl;
    return 0;
}

心得体会:加油加油,时刻保持好的心情,才有动力坚持做好自己想做的事!!

时间: 2024-08-14 11:14:20

C++——2828: 素数判断的相关文章

POJ 1811 大素数判断

数据范围很大,用米勒罗宾测试和Pollard_Rho法可以分解大数. 模板在代码中 O.O #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> using namespace std; __int64 pri[]= {2,3,5,7,11,13,17,19,23,29,31};//用小素数表做随机种子避免第一类卡米

POJ1811 Prime Test(miller素数判断&amp;&amp;pollar_rho大数分解)

http://blog.csdn.net/shiyuankongbu/article/details/9202373 发现自己原来的那份模板是有问题的,而且竟然找不出是哪里的问题,所以就用了上面的链接上的一份代码,下面只是寄存一下这份代码,以后打印出来当模板好了. #pragma warning(disable:4996) #include <iostream> #include <cstring> #include <algorithm> #include <c

POJ3641 Pseudoprime numbers(快速幂+素数判断)

POJ3641 Pseudoprime numbers p是Pseudoprime numbers的条件: p是合数,(p^a)%p=a;所以首先要进行素数判断,再快速幂. 此题是大白P122 Carmichael Number 的简化版 /* * Created: 2016年03月30日 22时32分15秒 星期三 * Author: Akrusher * */ #include <cstdio> #include <cstdlib> #include <cstring&g

POJ 2262 Goldbach&#39;s Conjecture (素数判断)

Goldbach's Conjecture Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37693   Accepted: 14484 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conject

csu 1552: Friends(大素数判断+二分图)

1552: Friends Time Limit: 3 Sec  Memory Limit: 256 MB Submit: 525  Solved: 136 [Submit][Status][Web Board] Description On an alien planet, every extraterrestrial is born with a number. If the sum of two numbers is a prime number, then two extraterres

POJ2262_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38024 Accepted: 14624 Description In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in which he made the following conjecture:

POJ2909_Goldbach&#39;s Conjecture【素数判断】【水题】

Goldbach's Conjecture Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10116 Accepted: 5973 Description For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 and p2 such that n = p1 + p2 This c

模板C++ 02数论算法 1最大公约数 AND 2素数判断

2.1最大公约数Greatest Common Divisor 补充知识:x*y=最小公倍数*最大公约数 int Euclid(int a,int b) { if(b==0) return a; return Euclid(b,a%b); } 2.2素数判断Prime #include<cmath> bool Prime(int n) { int t=sqrt(n); for(int i=2;i<=t;i++) if(n%i==0) return false; return true;

poj2262 - 素数判断

筛选法: 先把N个自然数按次序排列起来. * 1不是质数,也不是合数,要划去. * 第二个数2是质数留下来,而把2后面所有能被2整除的数都划去. * 2后面第一个没划去的数是3,把3留下,再把3后面所有能被3整除的数都划去. * 3后面第一个没划去的数是5,把5留下,再把5后面所有能被5整除的数都划去. * 这样一直做下去,就会把不超过N的全部合数都筛掉,留下的就是不超过N的全部质数. /* poj2262 - 素数判断 题目大意: 给定一个数n,把它分解成两个素数的和,在这些分解中,这两个素数