能帮我看一下我的代码吗?如果限时一秒的话最多能判断到多大的质数
  • 板块学术版
  • 楼主cute_overmind
  • 当前回复9
  • 已保存回复9
  • 发布时间2022/10/31 11:20
  • 上次更新2023/10/27 04:45:17
查看原帖
能帮我看一下我的代码吗?如果限时一秒的话最多能判断到多大的质数
681313
cute_overmind楼主2022/10/31 11:20
#include <iostream>
#include <cmath>
using namespace std;
bool prime(long long x)
{
    if(x == 2 || x == 3 || x == 5 || x == 7|| x == 11 || x == 13)
        return true;
    if(x < 2)
        return false;
    if(x % 2 == 0 || x % 3 == 0 || x % 5 == 0 || x % 7 == 0 || x % 11 == 0 || x % 13 == 0)
        return false;
    for(int i = 2;i <= sqrt(x);i++)
    {
    	if(x % i == 0)
    		return false;
    }
    return true;
    	
}
int main() 
{
    long long n;
    cin >> n;
    if(prime(n) == true)
        cout << "yes";
    else 
        cout << "no";
    return 0;
}
2022/10/31 11:20
加载中...