简体   繁体   English

素数指示器指示巨大的非素数作为素数

[英]prime number indicator indicating huge non-prime numbers as prime numbers

def main():
    try:
        a = int(input())
        if isinstance(a, int):
            a = int(a)
            if a ==2:
                print('YES')
            if a > 1:
                for i in range(2, a):
                    if a % i == 0:
                        print('NO')
                        break
                    else:
                        print('YES')
                        break
            else:
                print('NO')
        else:
            print('NO')
    except EOFError:
        print('NO')
    except ValueError:
        print('NO')
main()

This code is working but it's calculating huge numbers as prime number for example 3456734572525. It is successful for 4 test cases out of 5. First i thought it was failing on 0 or decimal numbers but that wasn't the case.这段代码正在运行,但它正在计算巨大的数字作为质数,例如 3456734572525。它在 5 个测试用例中有 4 个成功。首先我认为它在 0 或十进制数上失败,但事实并非如此。

The for loop never runs to the end, since regardless of whether a % i == 0 you will break the loop. for 循环永远不会运行到最后,因为无论a % i == 0是否都会中断循环。 You should move your print("YES") outside of the loop and use return enstead of break to ensure that your function doesnt run this code if your loop breaks.您应该将print("YES")移到循环之外并使用return不是break以确保您的函数在循环中断时不会运行此代码。 Try this:尝试这个:

def main():
    try:
        a = int(input())
        if isinstance(a, int):
            a = int(a)
            if a ==2:
                print('YES')
            if a > 1:
                for i in range(2, a):
                    if a % i == 0:
                        print('NO')
                        return
                print('YES')
                return
            else:
                print('NO')
        else:
            print('NO')
    except EOFError:
        print('NO')
    except ValueError:
        print('NO')
main()

you're breaking out of the loop the on the first run through你在第一次运行时就跳出了循环
say you put 9 in, think about it说你放了 9,想想看
it would fail the == 2 condition它会失败 == 2 条件
then hit a % i ==0 condition and as 9%2 = 1, it would return YES然后达到 % i ==0 条件,当 9%2 = 1 时,它会返回 YES

There are many errors with this program alongside that, but the main issue is breaking after the first run through, that doesnt make any sense, if you removed the break and print on the else for the if a%i==0 it would work, but there are many optimizations you could throw in there.这个程序还有很多错误,但主要问题是在第一次运行后中断,这没有任何意义,如果你删除中断并在 else 上打印 if a%i==0 它会工作,但是您可以在其中进行许多优化。

I reccomend taking a few courses/tutorials on python and not trying to jump in with little prior knowledge我建议参加一些关于 python 的课程/教程,而不是试图在没有一点先验知识的情况下跳进去
good luck祝你好运

Your code is actually incorrect to even begin with.您的代码实际上甚至一开始就不正确。
You're using 'break' inside the loop, after one 'if' check, so basically you're only checking once.您在循环内使用了 'break',在一次 'if' 检查之后,所以基本上您只检查一次。 So all your code does is tell if the number is divisible by 2.所以你的代码所做的就是判断这个数字是否能被 2 整除。

try:
    a = int(input())
    flag = 0
    if isinstance(a, int):
        a = int(a)
        if a==1:
            print('Neither')
        elif a ==2:
            print('YES')
        else:
            for i in range(2, a):
                if a % i == 0:
                    flag=1
                    break
    else:
        print('NO')
except EOFError:
    print('NO')
except ValueError:
    print('NO')
if flag:
    print('NO')
else:
    print('YES')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM