简体   繁体   English

确定素数[Turing]

[英]Determining primality [Turing]

put "enter a number to determine if it is or is not prime"
get primenum
% for i : 1 .. primenum by 1
% end for
if (primenum / primenum) = 1 or primenum / 1 = 0 then
    put primenum, " is a prime number"
else
    put primenum, " is not a prime number"
end if

Output says that 12 is a prime number, we know that's wrong so... 输出显示12是质数,我们知道这是错误的,所以...
What's wrong with my code, and how might I fix it? 我的代码有什么问题,我该如何解决?

Well, I don't know that language, but it looks to be fairly easy to read. 好吧,我不懂那种语言,但是看起来很容易阅读。 Your first problem is this line: 您的第一个问题是这一行:

if (primenum / primenum) = 1 or primenum / 1 = 0 then

This is testing to see if primenum divided by primenum is one or if primenum divided by one is zero. 这是在测试是否将primenum除以primenum是1,还是将primenum除以1是零。 The first condition will always be true and thus your algorithm will report that every integer is a prime number. 第一个条件将始终为true,因此您的算法将报告每个整数都是质数。

Let's recall the definition of a prime number. 让我们回想一下素数的定义。 A natural number n is prime if it has exactly two distinct natural number divisors. 如果自然数n恰好具有两个不同的自然数除数,则它是质数。 This means that to check and see if n is prime you must validate that there are no other divisors of n except for 1 and n itself (note that implicitly n can not be equal to 1 otherwise its only divisors are 1 and 1 which are not distinct). 这意味着要检查n是否为素数,必须验证n1n本身外没有其他除数(请注意, n不能隐含等于1否则它的除数只有11而不是除数)不同)。 To do this, just consider all possible numbers that could be divisors of n excluding 1 and n itself and check if any of them divide n . 为此,只需考虑所有可能为n除数的数字(不包括1n本身),然后检查是否有任何数除以n This means that we just loop from 2 to n - 1 checking if any of these numbers evenly divide n . 这意味着我们只是从2循环到n - 1检查这些数字中的任何一个是否均分n The natural numbers in the range 2 to n - 1 are the only possible numbers that could invalidate n from being prime. 2n - 1范围内的自然数是使n不能成为质数的唯一可能数。

Thus, the most naive way to implement a test as to whether a number is prime is following. 因此,下面是进行有关数字是否为质数的测试的最幼稚的方法。 Accept as input a number n . 接受数字n作为输入。 Then, check to see if n is less than 2 . 然后,检查n是否小于2 If it is it can not be a prime number. 如果是,则不能是素数。 Then, loop from 2 to n - 1 ; 然后,从2循环到n - 1 call the loop variable k . 调用循环变量k Check to see if any k in 2 to n - 1 evenly divides n ( if n mod k = 0 ). 检查以查看是否有任何k2n - 1整除nif n mod k = 0 )。 If there is such a k , then n can not be prime and you can break from the loop. 如果有一个k ,则n不能为素数,您可以从循环中中断。 Otherwise, if the loop terminates without breaking then n is prime. 否则,如果循环终止而不中断,则n为质数。 So, in pseudocode 所以,用伪代码

integer n
get n
boolean flag
if n < 2
    flag = false
else
    flag = true
    for k = 2 to n - 1
        if n mod k = 0 
            flag = false
            break
if flag
    print "prime"
else
    print "not prime"

Now, just one minor comment about your code. 现在,仅对您的代码发表一点评论。 Don't name the input primenum . 不要命名输入primenum A reader of your code might that think that primenum is in fact a prime number because you named it so. 您的代码阅读者可能认为primenum实际上是质数,因为您是这样命名的。 A name like valueToTest would be strongly preferred here. 这里强烈建议使用诸如valueToTest类的名称。

Your code ... doesn't make a lot of sense. 您的代码...没有多大意义。

% for i : 1 .. primenum by 1
% end for

Wooh. Empty loop. 空循环。 Doesn't do anything except burn clock cycles. 除了刻录时钟周期外,不执行任何操作。

if (primenum mod primenum) = 0

Will always be true . 永远是true

Also, you've got your condition the wrong way around. 此外,您的状况也不正确。 If it is divisible by something (other than itself and 1), then it is not prime. 如果它由东西(除了本身和1以外)整除,那么它是不是素数。

The solution? 解决方案? Probably rewrite it in psuedocode and then turn that into real code, rather than trying to hack up something without understanding it at all. 可能用psuedocode重写它,然后将其转化为真实代码,而不是试图完全不了解它。

The defining property of a prime is not that it is divisible by 1 and itself (all number have those properties), but that it is not divisible by anything else. 一个主要的定义属性不在于它整除1和它本身(所有的数字有那些属性),但它不是通过别的整除。

Try working from there. 尝试从那里开始。

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

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