【问题标题】:find all prime numbers from array从数组中找到所有素数
【发布时间】:2015-05-10 10:22:47
【问题描述】:

我想创建一个程序,要求用户使用数组输入 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<array[i]; j++){
            
            if(array[i]%j==0){
                isPrime = false;
                break;
            }
        }
        //print the number
        if(isPrime)

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

【问题讨论】:

  • 您能否详细说明您的问题是什么。
  • 我希望输出是这样的:输入数组的元素——23 98 45 101 6 数组中所有的素数都是——23 101
  • 但它给了我 0 1 2 3 作为输出。
  • edit您的问题并在此处而不是在 cmets 中添加此信息。
  • 只是对已经建议的答案的改进:为了测试数字 (n) 是否为素数,您可以检查该数字是否可以被 2 到 SquareRoot(n) 之间的任何数字整除。无需使用小于 n 的所有数字对其进行测试。利用该因素的示例实现。 davidsekar.com/algorithms/sieve-of-eratosthenes-prime

标签: java arrays


【解决方案1】:

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

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

我还没有测试过。

更新

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

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

【讨论】:

  • 我认为 print 语句有问题。我试过你的代码,它输出 0 和 3。
  • 我想要类似 studystreet.com/c-program-find-prime-array/#comment-14385 的东西,但无法将其转换为 Java
  • 所以我通过 System.out.println(array[i] + " are the prime numbers in the array ");给我输出:23 是数组中的素数 101 是数组中的素数。谢谢您的帮助。但是我现在的问题是如何输出,23, 101 是数组中的素数?
【解决方案2】:

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

伪代码:

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

【讨论】:

    【解决方案3】:

    从数组中找出所有素数。

             package com.myfirstproject;
    
    public class array_Uni_work {
    public static void main(String[] args) {
        int[] array = {6,48,47,7 , 98,51,87,99,2,0,1,191,157};
    
        // 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){
                if (array[i] == 0){}
                else {
                    System.out.print(array[i] + " , ");
                }
        }}
        System.out.println(" Are the prime number in the array ");
    }
    

    }

    【讨论】:

      【解决方案4】:
      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 ");
              }
          }
      

      【讨论】:

        【解决方案5】:

        这里是查找素数的更有效的代码。我们只需要检查直到 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;
            }
        

        【讨论】:

          【解决方案6】:

          这是从给定数组中找到素数的最简单形式。我们还可以通过分配 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);
             }
          }
          

          【讨论】:

            【解决方案7】:

            由于您明确要求输入 5 个数字,因此代码已硬编码为 5 个数字

            import java.util.Scanner;
            
            public class PracticeTest {
            
              public static void main(String args[]) {
                int arr[] = new int[5];
                Scanner sc = new Scanner(System.in);
                System.out.println("Enter 5 numbers");
                for (int i = 0; i < 5; i++) {
                  arr[i] = sc.nextInt();
                }
                checkPrimeNumbers(arr);
              }
            
              public static void checkPrimeNumbers(int arr[]) {
                loop1:
                for (int j = 0; j < arr.length; j++) {
                  loop2:
                  for (int i = 2; i < arr[j]; i++) {
                    if (arr[j] % i == 0) {
                      continue loop1;
                    }
                  }
                  System.out.print(arr[j] + " ");
                }
              }
            }
            

            【讨论】:

              【解决方案8】:

              这是接受“n”个整数并显示该数组中存在的素数和非素数的最简单方法。不使用函数

              import java.util.Scanner;
              // Author Dot-Coin
              public class The_Prime {
              
                  public static void main(String[] args) {
                      Scanner sc=new Scanner(System.in);
                      System.out.println("Enter the size of the array");
                      int size=sc.nextInt();
                      int Primer[]=new int [size];
                      int Non_Primer[]=new int [size];
                      int Numbers[]=new int [size];
                      int counter=0;int j=0,k=0,m=0,n=0;
                      System.out.println("Enter the numbers ");
                      for(int i=0;i<size;i++)
                          Numbers[i]=sc.nextInt();
                      for(int i=0;i<size;i++)
                      {
                          if(Numbers[i]==1||Numbers[i]==0)
                          {
                              continue;
                          }
                          
                              for(n=1;n<=Numbers[i];n++)
                                  {
                                  if(Numbers[i]%n==0)
                                      counter++;
                                  }
                              if(counter>2)
                              {
                                  Non_Primer[k]=Numbers[i];k++;counter=0;continue;
                              }
                              if(counter==2)
                              {
                                  Primer[m]=Numbers[i];m++;counter=0;
                              }
                          }
                       System.out.println("The prime numbers are");
                       for(k=0;k<Primer.length;k++)
                              {
                                  if(Primer[k]==0)
                                      {
                                      j++;continue;
                                      }
                                  else
                                  System.out.println(Primer[k]);
                              }
                          if(j==Primer.length)
                              System.out.println("Null");j=0;
                          System.out.println("The Non prime numbers are ");
                      for( k=0;k<Non_Primer.length;k++)
                      {
                          if(Non_Primer[k]==0)
                              {
                              j++;continue;
                              }
                          System.out.println(Non_Primer[k]);
                      }
                      if(j==Non_Primer.length)
                          System.out.println("Null");
                  }}
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2018-05-21
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2012-04-09
                • 1970-01-01
                • 2016-01-08
                • 1970-01-01
                相关资源
                最近更新 更多