将我的素数发生器存储到数组中

时间:2013-11-16 16:02:25

标签: java arrays loops

我现在正在学习数组。

我做了这个简短的小程序。这是一个测试然后打印素数的循环。 特别是1到100之间的数字。

我想知道我是否可以接受循环并将每个素数存储到数组中。这是我的代码:

public class QC3PrimeNumbers
{

    public static void main (String[] args)
   {
      System.out.println ("Here are the prime numbers: ");

       for (int index = 2; index < 100; index++)
      {
         if (index%2 != 0 && index%3 !=0)
         System.out.print (index + " ");         
      }

   }
}

2 个答案:

答案 0 :(得分:0)

如果您使用此条件:if (index%2 != 0 && index%3 !=0),那么它将永远不会考虑2或3个素数。如果您将其更改为:

,则同样如此
 if (index%2 != 0 && index%3 !=0 && index%5 !=0 && index%7 !=0)

它不会找到2个,3个,5个或7个素数。

我更正了您的算法并决定使用ArrrayList来存储素数列表,因为大小是动态的。否则,我们首先必须找到您所在区域中的素数的数量,以确定数组的大小,然后再做同样的循环,除非这次将数字添加到数组中。

找到素数后。使用nameOfArrayList.add(primeNumberFound);

<强> _ __ _ __ _ __ _ __ _ 的__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ < / EM> __ _ __ _ __ _ __ _ __ _ __ < /强>

使用ArrayList

执行此操作
public class QC3PrimeNumbers
{

    public static void main (String[] args)
    {
        System.out.println ("Here are the prime numbers: ");

        // use a List to store your prime numbers (its size is dynamic)
        List<Integer> primeNums = new ArrayList<Integer>();

        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, and reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++) 
                // if a number is found that factors into it, it's not prime
                if (index%i == 0) isPrime = false; 

            if (isPrime) // if this current index number is prime
            {
                System.out.print (index + " "); 
                primeNums.add(index); // add it to the List
            }
        }
    }
}

<强> _ __ _ __ _ __ _ __ _ 的__ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ __ _ < / EM> __ _ __ _ __ _ __ _ __ _ __ < /强>

使用常规数组

以下是使用常规数组的方法(不推荐使用):

public class QC3PrimeNumbers
{

    public static void main (String[] args)
    {
        System.out.println ("Here are the prime numbers: ");

        int numOfPrimes = 0; // counts the # of prime nums in your region

        // this loop will count up the number of prime numbers
        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
                numOfPrimes++; // add it to the count
        }

        // array to hold the prime nums w/ size=how many prime nums we counted
        int[] primeNums = new int[numOfPrimes];

        int count = 0; // keep track of the position in the primeNums array

        for (int index = 2; index < 100; index++)
        {
            boolean isPrime = true; // initially true, reset this every loop

            // for every number that can factor into the index number
            for (int i = 2; i < index; i++)
                // if a number is found that factors into it, it's not prime
                if (index % i == 0) isPrime = false;

            if (isPrime) // if this number is prime
            {
                primeNums[count] = index; // add this prime num to the array
                count++; // change the array position tracker for next number
            }
        }

        for (int n : primeNums) // for every integer in primeNums array
           System.out.print(n + " "); // display that integer to the console
    }
}

答案 1 :(得分:0)

import java.io.*;
import java.util.*;              
class numbers
{
public static void main(String args[])
{
    int[] prime=new int[20];
    int[] nonprime=new int[20];
    int count=0;
    int count1=0;`enter code here`
    try
        {
        InputStreamReader r=new InputStreamReader(System.in);  
        BufferedReader br=new BufferedReader(r);  
        System.out.println("enter n value");
        int n=Integer.parseInt(br.readLine());
        for(int i=1;i<n;i++)
        {   
            if(i%2!=0)
            {
                if(i%3!=0)
                    {
                    prime[count]=i;
                    count++;
                    }
            }
            else    
            {
            nonprime[count1]=i;
            count1++;
            }
        }
        System.out.println("prime");
        for(int m=0;m<count;m++)
        {
            System.out.println(prime[m]);
        }
        System.out.println("Non-prime");
        for(int k=0;k<count1;k++)
        {
            System.out.println(nonprime[k]);
        }
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
}