繁体   English   中英

"从数组中找到所有素数"

[英]find all prime numbers from array

我想创建一个程序,要求用户使用数组输入 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 ");
    }
}
}

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

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

我没有测试过这个。

更新

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

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

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

伪代码:

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

从数组中找出所有素数。

         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 ");
}

}

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 ");
        }
    }

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

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

由于您明确要求输入 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] + " ");
    }
  }
}

这是接受“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");
    }}

暂无
暂无

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

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