Java 打印从1到100的素数

Java 打印从1到100的素数,java,Java,这个程序应该输出1到100之间的素数。 有人能给我解释一下下面节目的流程吗?我写下面的程序有困难。我确实在互联网上找到了它,但我仍然不知道它是如何运作的,以及节目的流程如何 public class GeneratePrimeNumbersExample { public static void main(String[] args) { //define limit int limit = 100; Syst

这个程序应该输出1到100之间的素数。 有人能给我解释一下下面节目的流程吗?我写下面的程序有困难。我确实在互联网上找到了它,但我仍然不知道它是如何运作的,以及节目的流程如何

public class GeneratePrimeNumbersExample {

    public static void main(String[] args) {

            //define limit
            int limit = 100;

            System.out.println("Prime numbers between 1 and " + limit);

            //loop through the numbers one by one
            for(int i=1; i < 100; i++){

                    boolean isPrime = true;

                    //check to see if the number is prime
                    for(int j=2; j < i ; j++){

                            if(i % j == 0){
                                    isPrime = false;
                                    break;
                            }
                    }
                    // print the number
                    if(isPrime)
                            System.out.print(i + " ");
            }
    }
}
质数的输出示例如下 介于1和100之间的素数

1 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97

你如何用普通的解决方案找到素数? 若数是素数。它不会是除自身之外的任何数字的倍数。假设数字是x。从2开始到x-1,此数字不能被任何数字整除。为什么从2开始而不是1,因为每个数字都可以被1整除。 上面的代码试图复制相同的行为。按照循环查找1到99之间的所有素数:

从2到外环的数字-1 试着把这个数字除,然后检查它是否可以除。余数应为零。 如果真数不是素数。否则这个数就是素数。 你如何用普通的解决方案找到素数? 若数是素数。它不会是除自身之外的任何数字的倍数。假设数字是x。从2开始到x-1,此数字不能被任何数字整除。为什么从2开始而不是1,因为每个数字都可以被1整除。 上面的代码试图复制相同的行为。按照循环查找1到99之间的所有素数:

从2到外环的数字-1 试着把这个数字除,然后检查它是否可以除。余数应为零。 如果真数不是素数。否则这个数就是素数。
如果将各个部分拆分为具有适当名称的各自方法,则更容易理解:

for (int n = 1; n < 100; n++)
    if (isPrime(n))
        System.out.println(n);

private boolean isPrime(int n) {
    for (int f = 2; f < n; f++) {
        if (isFactor(f, n))
            return false;
    }
    return true;
}

private boolean isFactor(int factor, int number) {
    return number % factor == 0;
}
这也是Java 8流可以让事情变得更清楚的一个领域:

List<Integer> primes = IntStream.range(1, 100)
    .filter(this::hasNoFactors)
    .collect(Collectors.toList());

private boolean hasNoFactors(int number) {
    return IntStream.range(2, number)
        .noneMatch(f -> number % f == 0);
}
还要注意的是,这是一个非常低效的算法。不需要检查从2到n的所有可能因子,只需要检查素数。您还可以利用多处理器机器:

List<Integer> primes = new ArrayList<>();
IntStream.range(2, 100)
    .filter(n -> primes.parallelStream().noneMatch(p -> n % p == 0))
    .forEach(primes::add);

如果将各个部分拆分为具有适当名称的各自方法,则更容易理解:

for (int n = 1; n < 100; n++)
    if (isPrime(n))
        System.out.println(n);

private boolean isPrime(int n) {
    for (int f = 2; f < n; f++) {
        if (isFactor(f, n))
            return false;
    }
    return true;
}

private boolean isFactor(int factor, int number) {
    return number % factor == 0;
}
这也是Java 8流可以让事情变得更清楚的一个领域:

List<Integer> primes = IntStream.range(1, 100)
    .filter(this::hasNoFactors)
    .collect(Collectors.toList());

private boolean hasNoFactors(int number) {
    return IntStream.range(2, number)
        .noneMatch(f -> number % f == 0);
}
还要注意的是,这是一个非常低效的算法。不需要检查从2到n的所有可能因子,只需要检查素数。您还可以利用多处理器机器:

List<Integer> primes = new ArrayList<>();
IntStream.range(2, 100)
    .filter(n -> primes.parallelStream().noneMatch(p -> n % p == 0))
    .forEach(primes::add);

仅能被自身和1整除的数称为素数。下面是查找1到100之间素数的最简单版本的代码

import java.io.*;
import java.util.*;
class solution
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int n = 100;
        /*
            A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself.
        */
        for(int i=2;i<=n;i++) // 1.So we are starting with initialization i = 2
        {
            int flag = 1;
            for(int j=2;j<=i/2;j++)  // 2.Try dividing the number by half check whether it divisible
            {
                if(i%j==0) // 3. If the number is divisible by other number ->Not a prime Number
                {
                    flag = 0;
                    break;
                }

            }
            if(flag==1) // 4. If the number is not divisible by any other numbers but only by itself and 1 -> prime no
            {
                System.out.print(i+" ");
            }
        }
    }
}
/*
    Output:
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/

仅能被自身和1整除的数称为素数。下面是查找1到100之间素数的最简单版本的代码

import java.io.*;
import java.util.*;
class solution
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        int n = 100;
        /*
            A prime number is a whole number greater than 1, whose only two whole-number factors are 1 and itself.
        */
        for(int i=2;i<=n;i++) // 1.So we are starting with initialization i = 2
        {
            int flag = 1;
            for(int j=2;j<=i/2;j++)  // 2.Try dividing the number by half check whether it divisible
            {
                if(i%j==0) // 3. If the number is divisible by other number ->Not a prime Number
                {
                    flag = 0;
                    break;
                }

            }
            if(flag==1) // 4. If the number is not divisible by any other numbers but only by itself and 1 -> prime no
            {
                System.out.print(i+" ");
            }
        }
    }
}
/*
    Output:
    2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
*/

公共静态ArrayList素数限制{

    ArrayList<Integer> primes = new ArrayList<>();
    for(int p = 2; p <= limit; p++) {
        int count = 0;
        for(int i=2; i < p; i++) {

            if (p%i == 0) {
                count++;
            }
        }

        if (count == 0) {
            primes.add(p);
        }   
    }
    return primes;
}

公共静态ArrayList素数限制{

    ArrayList<Integer> primes = new ArrayList<>();
    for(int p = 2; p <= limit; p++) {
        int count = 0;
        for(int i=2; i < p; i++) {

            if (p%i == 0) {
                count++;
            }
        }

        if (count == 0) {
            primes.add(p);
        }   
    }
    return primes;
}


你不懂什么?很简单很好的逻辑你不懂什么?很简单很好的逻辑为什么是x-1?当它进入第二个循环时,程序是如何工作的?请你解释一下,如果我们在检查x时一直到x,那么x总是可以被x整除。如果你不循环到x-1,那么就没有数字了r将永远是你的素数。第1点、第2点和第3点解释了内循环中有什么。为什么是x-1?当它进入第二个循环时,程序是如何工作的?如果你能解释我们在检查x时是否一直到x,请求求你。这有什么意义吗?x总是可以被x整除。如果你不循环到x-1,那么就永远不会有数字为您准备。第1、2和3点也解释了内部循环中的内容。请添加一些描述以回答问题的帮助lz添加一些描述以回答问题的帮助代码在SO中只有答案是不受欢迎的。请添加一些解释,以便其他人阅读您的代码!在SO中只有代码答案是不受欢迎的。请添加一些解释,以便其他人阅读您的代码!与lambdas相关的最后一个代码不起作用。在封闭范围中定义的局部变量n必须是final或effectivefinal@Braislyn实际上是最终版本。我已经修复了代码中的一些拼写错误,但此代码工作正常。最后一个与lambdas相关的代码不工作。Loca在封闭范围中定义的变量n必须是最终的或有效的final@Braislyn实际上是最终版本。我已经修复了代码中的一些拼写错误,但此代码工作正常。请在您的答案中添加一些解释。目前它无法回答问题,无法解释程序的流程。此函数返回素数列表给定参数中的数字。在获得结果后,您可以输入从1到100的参数。更新答案的最佳方法是单击其下方的编辑链接。请为您的答案添加一些说明。目前它无法回答解释程序流程的问题。此功能
n从给定参数返回素数列表。在得到结果后,您可以输入从1到100的参数。更新答案的最佳方法是单击其下方的“编辑”链接。