为什么正数 return 的增量为负数? (Java)

Why does an increment by positive one return a negative? (Java)

我有一个列出素数的程序。我从一个正整数开始,它返回一个包含 negative 数字(不应该这样做)的数组(应该这样做)。问题是代码中的任何地方都没有递减运算符,只有递增运算符和模数函数。这是完整的代码和输出供您参考,完整注释以便于理解:

package calculations;

import java.util.ArrayList;

public class PrimeNumbers {
 public static void main(String[] args) {
    
     //Declaring variables to be used...
     ArrayList<Integer> listOfPrimes = new ArrayList<Integer>();
     listOfPrimes.add(2);
     int numOfPrimesToList = 10;
     int possiblePrimeCounter  = 0;
     
     /* We start testing at the number 2 and increment until 
      * the list of primes we have listed is equal to the
      * number of primes want to list. 
      */
     for(int atNum = 2; listOfPrimes.size() < numOfPrimesToList; atNum++) {
        
         /* While the index of the number to divide from 
          * is less than the length of our list, we do 
          * the following:
          */
         for(int intIndexToCheck = 0; intIndexToCheck < listOfPrimes.size(); intIndexToCheck++) {
            
             /* If the modulus of the number we are testing 
              * and the one are dividing by is greater than
              * zero, we increment its possibility of being
              * a prime by one. 
              */
             if(atNum % listOfPrimes.get(intIndexToCheck) < 0) {
                 possiblePrimeCounter++;
                
                 /* If its chance of being a prime is 1 (that is, the
                  * times it has been given a chance is equal to the
                  * total number of possible chances it is given), then
                  * we can consider it to be a prime.
                  */
                 if(possiblePrimeCounter == listOfPrimes.size()) {
                     //We then add this number to the list of primes.
                     listOfPrimes.add(atNum);
                    
                     possiblePrimeCounter = 0;
                 }
             }
         }
     }
     //Finally, we print out the list of primes.
     System.out.println(listOfPrimes);
 }
}

Returns:

[2, -2147483647, -2147483645, -2147483644, -2147483643, -2147483641, -2147483640, -2147483639, -2147483638, -2147483637]

发生的事情称为整数溢出。 当 int 成为最大值 (Integer.MAX_VALUE) 时,如果您尝试递增,它将返回到它的最小值 (Integer.MIN_VALUE),这就是您得到的数字。

问题是你的代码没有按照你的评论所说的那样做。你有

         /* If the modulus of the number we are testing 
          * and the one are dividing by is greater than
          * zero, we increment its possibility of being
          * a prime by one. 
          */
         if(atNum % listOfPrimes.get(intIndexToCheck) < 0) {

它说您检查 大于 是否为零,但实际上它检查的是 小于 零。更改行以实际执行评论所说的

         if(atNum % listOfPrimes.get(intIndexToCheck) > 0) {

由于您正在测试小于零的余数,而这对于正数永远不会发生,因此您不断增加数字,直到它溢出并变为负数,此时余数确实变得小于零。

对于您的后续问题,如果您的数字不是素数,则 possiblePrimeCounter 不会重置,因此 possiblePrimeCounter 会继续测试下一个数字。此外,在添加一个素数之后,您还需要进行另一项测试并检查它是否可以被自身整除(因为您刚刚增加了列表的大小)。

一个简单的解决方法是

     possiblePrimeCounter = 0; // Need to reset before trying each new number
     for(int intIndexToCheck = 0; intIndexToCheck < listOfPrimes.size(); intIndexToCheck++) {

或者使循环更高效,一旦我们知道数字不是素数就没有必要继续测试。

     possiblePrimeCounter = 0;

     for(int intIndexToCheck = 0; intIndexToCheck < listOfPrimes.size(); intIndexToCheck++) {
         if(atNum % listOfPrimes.get(intIndexToCheck) !=0) {
             possiblePrimeCounter++;
             if(possiblePrimeCounter == listOfPrimes.size()) {
                 //We then add this number to the list of primes.
                 listOfPrimes.add(atNum);
                
                 possiblePrimeCounter = 0;
             }
         } else {
             break; // Not prime
         }
     }