Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 用数组寻找素数_Java_Arrays_Loops_Primes - Fatal编程技术网

Java 用数组寻找素数

Java 用数组寻找素数,java,arrays,loops,primes,Java,Arrays,Loops,Primes,我有下面的程序,它用循环打印从1到100的素数。如何将这些素数的值存储在一个数组中并从该数组中打印出来?我尝试初始化int[]n=1,但它一点也不喜欢。谢谢大家 public class PrimeGenerator { public static void main(String[] args) { int max = 100; System.out.println("Generate Prime numbers between 1 and 100. \"1\"

我有下面的程序,它用循环打印从1到100的素数。如何将这些素数的值存储在一个数组中并从该数组中打印出来?我尝试初始化int[]n=1,但它一点也不喜欢。谢谢大家

public class PrimeGenerator 
{
  public static void main(String[] args) 
  {
    int max = 100;

    System.out.println("Generate Prime numbers between 1 and 100. \"1\" is not prime.");

    // loop through the numbers one by one
    for (int n = 1; n<max; n++) {
      boolean prime = true;
      //analyzes if n is prime      

      for (int j = 2; j < n; j++) {
        if (n % j == 0 ) {
          prime = false;
          break; // exit the inner for loop
        }
      }

      //outputs primes
      if (prime && n != 1) {    
        System.out.print(n + " ");
      }
    }
  }
}
公共类素数生成器
{
公共静态void main(字符串[]args)
{
int max=100;
System.out.println(“生成介于1和100之间的素数。\'1\'不是素数”);
//把数字一个接一个地循环一遍

对于(int n=1;n,只需使用ArrayList存储找到的所有素数

import java.util.ArrayList;

public class PrimeGenerator {
    public static void main(String[] args) {
        int max = 100;

        System.out.println("Generate Prime numbers between 1 and 100. \"1\" is not prime.");

        ArrayList<Integer> list = new ArrayList<Integer>();

        // loop through the numbers one by one
        for (int n = 1; n < max; n++) {
            boolean prime = true;
            // analyzes if n is prime

            for (int j = 2; j < n; j++) {
                if (n % j == 0) {
                    prime = false;
                    break; // exit the inner for loop
                    }
            }
            if (prime && n != 1) {
                list.add(n);
            }
        }
        for (int i : list) {
            System.out.println(i + " ");
        }
    }
}
import java.util.ArrayList;
公共类素数生成器{
公共静态void main(字符串[]args){
int max=100;
System.out.println(“生成介于1和100之间的素数。\'1\'不是素数”);
ArrayList=新建ArrayList();
//把数字一个接一个地循环一遍
对于(int n=1;n
试试这个

int max = 100;

    System.out.println("Generate Prime numbers between 1 and 100. \"1\" is not prime.");
    ArrayList<Integer> primenumbers = new ArrayList<>();
    // loop through the numbers one by one
    for (int n = 1; n < max; n++) {
        boolean prime = true;
        // analyzes if n is prime

        for (int j = 2; j < n; j++) {
            if (n % j == 0) {
                prime = false;
                break; // exit the inner for loop
            }

        }

        // outputs primes
        if (prime && n != 1) {

            primenumbers.add(n);

        }
    }
    System.out.println(primenumbers);
int max=100;
System.out.println(“生成介于1和100之间的素数。\'1\'不是素数”);
ArrayList素数=新的ArrayList();
//把数字一个接一个地循环一遍
对于(int n=1;n
一个有效的素数查找工具是“阿特金筛”,我为“欧拉计划”中的大多数问题存储了它,而不是素数。它可以在我的机器上计算多达10亿(不到一分钟)并打印出来

import java.util.Arrays;

public class SieveOfAtkin {
private static int limit = 1000000;
private static boolean[] sieve = new boolean[limit + 1];
private static int limitSqrt = (int)Math.sqrt((double)limit);

public static void main(String[] args) {
    // there may be more efficient data structure
    // arrangements than this (there are!) but
    // this is the algorithm in Wikipedia
    // initialize results array
    Arrays.fill(sieve, false);
    // the sieve works only for integers > 3, so 
    // set these trivially to their proper values
    sieve[0] = false;
    sieve[1] = false;
    sieve[2] = true;
    sieve[3] = true;

    // loop through all possible integer values for x and y
    // up to the square root of the max prime for the sieve
    // we don't need any larger values for x or y since the
    // max value for x or y will be the square root of n
    // in the quadratics
    // the theorem showed that the quadratics will produce all
    // primes that also satisfy their wheel factorizations, so
    // we can produce the value of n from the quadratic first
    // and then filter n through the wheel quadratic 
    // there may be more efficient ways to do this, but this
    // is the design in the Wikipedia article
    // loop through all integers for x and y for calculating
    // the quadratics
    for (int x = 1; x <= limitSqrt; x++) {
        for (int y = 1; y <= limitSqrt; y++) {
            // first quadratic using m = 12 and r in R1 = {r : 1, 5}
            int n = (4 * x * x) + (y * y);
            if (n <= limit && (n % 12 == 1 || n % 12 == 5)) {
                sieve[n] = !sieve[n];
            }
            // second quadratic using m = 12 and r in R2 = {r : 7}
            n = (3 * x * x) + (y * y);
            if (n <= limit && (n % 12 == 7)) {
                sieve[n] = !sieve[n];
            }
            // third quadratic using m = 12 and r in R3 = {r : 11}
            n = (3 * x * x) - (y * y);
            if (x > y && n <= limit && (n % 12 == 11)) {
                sieve[n] = !sieve[n];
            } // end if
            // note that R1 union R2 union R3 is the set R
            // R = {r : 1, 5, 7, 11}
            // which is all values 0 < r < 12 where r is 
            // a relative prime of 12
            // Thus all primes become candidates
        } // end for
    } // end for
    // remove all perfect squares since the quadratic
    // wheel factorization filter removes only some of them
    for (int n = 5; n <= limitSqrt; n++) {
        if (sieve[n]) {
            int x = n * n;
            for (int i = x; i <= limit; i += x) {
                sieve[i] = false;
            } // end for
        } // end if
    } // end for
    // put the results to the System.out device
    // in 10x10 blocks

    for( int i = 0 ; i < sieve.length ; i ++ ) {
        if(sieve[i])
            System.out.println(i);
    }
} // end main
} // end class SieveOfAtkin
导入java.util.array;
公共级西维奥法特金{
私有静态整数限制=1000000;
私有静态布尔值[]筛=新布尔值[limit+1];
私有静态int limitSqrt=(int)Math.sqrt((double)limit);
公共静态void main(字符串[]args){
//可能会有更高效的数据结构
//还有比这更好的安排,但是
//这是维基百科中的算法
//初始化结果数组
数组。填充(筛,假);
//筛子只适用于大于3的整数,所以
//将这些设置为适当的值
筛[0]=假;
筛[1]=假;
筛[2]=真;
筛[3]=真;
//循环遍历x和y的所有可能整数值
//直到筛子的最大素数的平方根
//因为
//x或y的最大值为n的平方根
//在二次曲线中
//这个定理表明,二次函数将产生所有
//也满足轮因式分解的素数,所以
//我们可以先从二次曲线中求出n的值
//然后通过轮二次型过滤n
//也许有更有效的方法可以做到这一点,但是
//这是维基百科文章中的设计吗
//循环计算x和y的所有整数
//二次曲线

对于(int x=1;x使用
列表
代替数组,因为你不知道会有多少素数。可能的重复哦!我在哪里初始化它?在for循环中?你忘了用
n=2
开始for循环。我的解决方案假设你想对素数做些什么,而不是简单地在System.out上打印它们。如果y如果要将它们丢弃,只需打印它们,而不是添加到列表中。此外,在n之前不需要检查j。如果达到n的平方根,则可以停止。最后一行应该是
System.out.println(Arrays.toString(primenumbers))
因为
素数
是一个对象。试试看。它不起作用。正常的sysout工作得很好