JDOJ 1152 是否是素数

1152: 是否是素数

https://neooj.com:8082/oldoj/problem.php?id=1152

题目描述

写一个判断素数的函数,在主函数输入一个整数,输出是否是素数的消息。

输入

一个数

输出

如果是素数输出prime 如果不是输出not prime

样例输入

97

样例输出

prime
 
判素数是很常用的技巧,尤其是后面学到数论之后会对一些知识点起到启发性作用。
AC CODE:
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int prime(int x)
{
    for(int i=2;i<=sqrt(x);i++)
        if(x%i==0)
        return 0;
    return 1;
}
int main()
{
    int n,hahaha;
    scanf("%d",&n);
    hahaha = prime(n);
    if(hahaha==0)
        printf("not prime");
    else
        printf("prime");
    return 0;
} 
原文地址:https://www.cnblogs.com/fusiwei/p/11187829.html