Java 从数组中找出所有素数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30150251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 09:14:09  来源:igfitidea点击:

find all prime numbers from array

javaarrays

提问by Programmer19

I want to create a program that will ask the user to input 5 integers using array and determine all the prime numbers entered. But I have difficulty with it. What seems to be the problem? I use JCreator for this.

我想创建一个程序,要求用户使用数组输入 5 个整数并确定输入的所有素数。但我有困难。似乎是什么问题?我为此使用 JCreator。

import java.util.Scanner;
public class PrimeNumbers{
public static void main (String[] args){
    int[] array = new int [5];
    Scanner in = new Scanner (System.in);

    System.out.println("Enter the elements of the array: ");
    for(int i=0; i<5; i++)
    {
        array[i] = in.nextInt();
    }
    //loop through the numbers one by one
    for(int i=0; i<array.length; i++){
        boolean isPrime = true;

        //check to see if the numbers are prime
        for (int j=2; j<i; j++){

            if(i%j==0){
                isPrime = false;
                break;
            }
        }
        //print the number
        if(isPrime)

            System.out.println(i + " are the prime numbers in the array ");
    }
}
}

采纳答案by Peter Smith

You are checking the loop counters, not the values in the array. Try something like

您正在检查循环计数器,而不是数组中的值。尝试类似的东西

for (int j=2; j<array[i]; j++){
    if(array[I]%j==0){
            isPrime = false;
            break;
        }

I haven't tested this.

我没有测试过这个。

UPDATE

更新

To print out the results either print each on as it is found, or, copy the prime numbers into an output array and then print that when you have finished the checks. The details will depend on the language you are using.

要打印结果,可以在找到时打印每个结果,或者将质数复制到输出数组中,然后在完成检查后打印。详细信息将取决于您使用的语言。

Please note, you are not using a very efficient detection algorithm; Google for a better one.

请注意,您没有使用非常有效的检测算法;谷歌搜索更好的。

回答by Thusitha Thilina Dayaratne

public static void main(String[] args) {
        int[] array = new int[5];
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the elements of the array: ");
        for (int i = 0; i < 5; i++) {
            array[i] = in.nextInt();
        }
        // loop through the numbers one by one
        for (int i = 0; i < array.length; i++) {
            boolean isPrime = true;
            if (array[i] == 1)
                isPrime = false;
            else {
                // check to see if the numbers are prime
                for (int j = 2; j <= array[i] / 2; j++) {
                    if (array[i] % j == 0) {
                        isPrime = false;
                        break;
                    }
                }
            }
            // print the number
            if (isPrime)
                System.out.println(array[i] + " is a prime number in the array ");
        }
    }

回答by TomerM

You can check all integer numbers until root of the required number

您可以检查所有整数,直到所需数字的根

Pseudo code:

伪代码:

 for(i=2; i<sqrt(number);i++){
  if(number/i===0){
    //not prime number
  }
}

回答by David

Here is more efficient code finding prime number. We only need to check the odd number up to the square root of N, assume the number is greater than 2.

这是查找素数的更有效的代码。我们只需要检查直到 N 的平方根的奇数,假设这个数大于 2。

boolean isPrime(int n) {
        //check if n is a multiple of 2
        if (n%2==0) return false;
        //if not, then just check the odds
        for(int i=3;i*i<=n;i+=2) {
            if(n%i==0)
                return false;
        }
        return true;
    }

回答by Sasi

This is the simplest form of finding the prime numbers from the given array. We can also use scanner by assigning n instead of an array to check whether if it is a prime or non prime from the given input(commented in program).Hope this helps you..!!

这是从给定数组中查找素数的最简单形式。我们还可以通过分配 n 而不是数组来使用扫描仪来检查它是给定输入中的素数还是非素数(在程序中注释)。希望这对您有所帮助..!!

public static void main(String[] arg) {
    int a[]= {1,2,3,4,5,6,7,8,9,10}; //int n=0 or n=values       
    int num =0;
    String  primeNumbers = "";
    for (int i = 0; i <= a.length; i++)  /*change a.length to n*/      
      {                   
         int counter=0;           
         for(num =i; num>=1; num--)
         {
        if(i%num==0)
        {
        counter = counter + 1;
        }
     }
     if (counter ==2)
     {
        //Appended the Prime number to the String
        primeNumbers = primeNumbers + i + " ";
     }  
      } 
      System.out.println("Prime numbers from given array :");
      System.out.println(primeNumbers);
   }
}