美文网首页计算机
Interview Question - How to dete

Interview Question - How to dete

作者: Richardo92 | 来源:发表于2016-09-29 01:14 被阅读3次

如何判断输入的数,是否为 Prime

My code:

public boolean isPrime(int x) {
    if (x <= 1) {
        return false;
    }
    else if (x == 2) {
        return true;
    }
    
    for (int i = 3; i * i <= x; i += 2) {
        if (x % i == 0) {
            return false;
        }
    }
    return true;
}

首先,除了 2 以外,其他质数都是奇数。
其次 , i * i <= n

Anyway, Good luck, Richardo! -- 09/28/2016

相关文章

网友评论

    本文标题:Interview Question - How to dete

    本文链接:https://www.haomeiwen.com/subject/wdbhyttx.html