简体   繁体   English

在N号之前打印每个素数

[英]Print every prime number before N number

Hey I am beginning to program using java and my teacher used this example in class for our homework which was to create a java program that prints out every prime number before reaching the upper limit that the user inputs. 嘿,我开始使用Java进行编程,我的老师在课堂上使用了此示例作为作业的基础,这是创建一个Java程序,该程序在达到用户输入的上限之前会打印出所有质数。 I am not understanding the second part and wondering if someone could help explaining it to me. 我不理解第二部分,不知道是否有人可以帮助向我解释。

import java.util.Scanner;

public class Primes {

    public static void main(String args[]) {

      //get input for the upper limit
      System.out.println("Enter the upper limit: ");          
      //read in the limit
      int limit = new Scanner(System.in).nextInt();

      //use for loop and isPrime method to loop through until the number reaches the limit
      for(int number = 2; number<=limit; number++){             
      //print prime numbers only before the limit 
          if(isPrime(number)){          
              System.out.print(number + ", ");                 
          }
      }    
    }    

    //this part of the program determines whether or not the number is prime by using the modulus 
    public static boolean isPrime(int number){
        for(int i=2; i<number; i++){
           if(number%i == 0){
               return false; //number is divisible so its not prime
           }
        }
        return true; //number is prime now
    }        
}

I guess that what you mean by second part is the isPrime method. 我猜您第二部分的意思是isPrime方法。

What he is doing is using '%' operator which returns the integer remainder of the division between 'number' and 'i'. 他正在使用“%”运算符,该运算符返回“ number”和“ i”之间除法的整数余数。 As a prime number is just divisor for itself and the number 1, if the remainder is 0 it means is not a prime number. 因为质数本身就是数字的除数,所以1是除数,如果余数为0,则表示不是质数。 Your teacher is looping with the 'i' variable until the limit number and checking if any of the numbers is prime by looking the result of the % operation. 您的老师正在循环使用“ i”变量,直到达到极限数为止,并通过查看%运算的结果来检查是否有任何数字是质数。

Hope this is helpful for you!! 希望这对您有帮助!

In the second part 在第二部分

if(number%i == 0)

% usually gives you a remainder if there is. 如果有%,通常会给您余数。

eg 5 % 2 gives 1 例如5%2给1

4 % 2 gives 0 4%2给出0

for(int i=2; i<number; i++)

Here you are looping through from 2 to the number. 在这里,您正在循环从2到数字。 Since all numbers are divisible by 1 you start from 2. Also you stop before number (number -1) since you dont want to check if the number is divisible by itself (because it is). 因为所有数字都可以被1整除,所以您从2开始。由于您不想检查数字本身是否可以整除(因为是数字),因此您也要在数字(数字-1)之前停止。

If a number is divisible by any other number other than 1 and itself (number from 2 to number -1) then it is not a prime number. 如果一个数字可以被除1之外的其他任何数字整除(从2到-1),那么它不是质数。

Just a short (not efficient) reference for finding prime numbers if you need it: 如果需要,仅是简短的(效率不高)参考,用于查找质数:

    int upperLimit = 30; //Set your upper limit here
    System.out.println(2);
    for(int i = 2; i < upperLimit; i++)
        for(int j = 2; j < i; j++)
            if(i % j == 0 && i != 2)
                break;
            else if(j == i - 1)
                System.out.println(i);

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

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