储存我的主要发电机到一个数组 [英] Storing my prime generator into an array

查看:88
本文介绍了储存我的主要发电机到一个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习有关数组现在。

我在这短短的小程序。这是一个循环测试然后打印素数。
具体地1到100之间的数字。

我想知道如果我可以把我的循环,每个素数存入数组。这里是我的code:

 公共类QC3PrimeNumbers
{    公共静态无效的主要(字串[] args)
   {
      的System.out.println(这里是素数:);       对于(INT指数= 2;指数< 100;指数++)
      {
         如果(索引%2 = 0&安培;!&安培;!索引%3 = 0)
         System.out.print(索引+);
      }   }
}


解决方案

如果您使用此条件:如果(!索引%2 = 0&放大器;&安培;索引%3 = 0),那么它会不会考虑2或3的质数。同样的事情,如果你把它改成:

 如果(索引%2 = 0&安培;!&安培;索引%3 = 0&安培;!&安培;索引%5 = 0&安培;!&安培;!索引%7 = 0 )

它将找不到2,3,5,或7是素数。

我纠正你的算法,并决定使用 ArrrayList 以存储素数的列表,因为大小是动态的。否则,我们首先要找到您所在区域的素数的个数来确定数组的大小,然后做同样的事情,另一个循环,但这次添加数字到数组。

一旦你找到一个素数。使用 nameOfArrayList.add(primeNumberFound);

_________________________________________________________________________________

与一个ArrayList

做吧

 公共类QC3PrimeNumbers
{    公共静态无效的主要(字串[] args)
    {
        的System.out.println(这里是素数:);        //用一个列表来存储你的素数(它的大小是动态的)
        清单<整数GT; primeNums =新的ArrayList<整数GT;();        对于(INT指数= 2;指数< 100;指数++)
        {
            布尔isPrime = TRUE; //最初真实的,这重置每次循环            //对于每个数字可以因素纳入索引号
            的for(int i = 2; I<指数;我++)
                //如果一个数发现因素考虑它,它不是素数
                如果(指数%I == 0)isPrime = FALSE;            如果(isPrime)//如果当前的索引数是素
            {
                System.out.print(索引+);
                primeNums.add(索引); //将其添加到列表
            }
        }
    }
}

_________________________________________________________________________________

具有规则排列就做

下面是如何使用常规数组做到这一点(不推荐,不过):

 公共类QC3PrimeNumbers
{    公共静态无效的主要(字串[] args)
    {
        的System.out.println(这里是素数:);        INT numOfPrimes = 0; //计算素NUMS的#您所在地区        //这个循环将计数素数数
        对于(INT指数= 2;指数< 100;指数++)
        {
            布尔isPrime = TRUE; //最初真实的,这重置每次循环            //对于每个数字可以因素纳入索引号
            的for(int i = 2; I<指数;我++)
                //如果一个数发现因素考虑它,它不是素数
                如果(指数%I == 0)isPrime = FALSE;            如果(isPrime)//如果这个数字是素数
                numOfPrimes ++; //它添加到计数
        }        //数组来保存黄金NUMS W /大小=我们有多少黄金数NUMS
        INT [] = primeNums新INT [numOfPrimes]        诠释计数= 0; //跟踪位置primeNums阵列        对于(INT指数= 2;指数< 100;指数++)
        {
            布尔isPrime = TRUE; //最初真实的,这重置每次循环            //对于每个数字可以因素纳入索引号
            的for(int i = 2; I<指数;我++)
                //如果一个数发现因素考虑它,它不是素数
                如果(指数%I == 0)isPrime = FALSE;            如果(isPrime)//如果这个数字是素数
            {
                primeNums [计数] =指数; //这个黄金NUM添加到数组
                算上++; //改变下一个数字数组位置跟踪器
            }
        }        对于(INT N:primeNums)//在primeNums阵列的每个整数
           System.out.print(N +); //显示整数控制台
    }
}

I'm learning about arrays right now.

I made this short little program. It's a loop that tests then prints prime numbers. Specifically the numbers between 1 and 100.

I want to know if I can take my loop and store each prime number into an array. Here's my code:

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

   }
}

解决方案

If you use this condition: if (index%2 != 0 && index%3 !=0), then it will never consider 2 or 3 prime numbers. Same thing if you changed it to:

 if (index%2 != 0 && index%3 !=0 && index%5 !=0 && index%7 !=0)

it won't find 2, 3, 5, or 7 to be prime numbers.

I corrected your algorithm and decided to use an ArrrayList to store the list of prime numbers because the size is dynamic. Otherwise, we would first have to find the number of prime numbers in your region to determine the size of the array, and then do another loop of the same thing, except this time adding the numbers to the array.

Once you find a prime number. Use nameOfArrayList.add(primeNumberFound);.

_________________________________________________________________________________

Do it with an 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
            }
        }
    }
}

_________________________________________________________________________________

Do it with a regular Array

Here is how to do it using a regular array (not recommended, however):

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
    }
}

这篇关于储存我的主要发电机到一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆