简体   繁体   English

打印素数从1到100

[英]Printing prime number from 1 to 100

This program is supposed to output the prime numbers between 1 and 100. Will anyone please explain me the flow the programme below? 该程序应该输出1到100之间的质数。有人可以向我解释下面程序的流程吗? I am having difficulty in writing the programme below. 我在编写下面的程序时遇到困难。 I did find it on the internet but still I can't figure out how it works and how the flow of the programme will be? 我确实在互联网上找到了它,但仍然不知道它是如何工作的以及程序的流程如何?

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

Output of Prime Numbers example would be Prime numbers between 1 and 100 质数示例的输出将是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 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

How would you find a prime number with plain vanilla solution? 您将如何用普通香草溶液找到素数? If number is prime. 如果数字是素数。 It will not be a multiple of any number other than itself. 除自身外,它不会是任何数字的倍数。 So assume number is x. 因此,假设数字为x。 This number will not be divisible by any number when starting from 2 till x-1. 从2到x-1,此数字将不能被任何数整除。 Why start from 2 and not 1 because every number is divisible by 1. The above code is trying to replicate the same behavior. 为什么从2而不是1开始,因为每个数字都可以被1整除。上面的代码试图复制相同的行为。 To find all primes between 1 till 99 (as per the loop): 要查找1到99之间的所有素数(根据循环):

  1. From 2 till number from (outer loop - 1) 从2到数字(外循环-1)
  2. Try dividing the number and check if it's divisible. 尝试将数字相除并检查是否可整除。 (remainder should be zero). (余数应为零)。
  3. If true number is not prime. 如果真数字不是素数。 Else number is prime. 其他数字是素数。

If you split the various parts out into their own methods with appropriate names it becomes a bit easier to understand: 如果使用适当的名称将各个部分拆分为各自的方法,则将更容易理解:

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

This is also an area where Java 8 streams can make things a bit clearer: 这也是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);
}

Also note that this is a horribly inefficient algorithm. 另请注意,这是一种效率极低的算法。 You don't need to check every possible factor from 2 to n, just the primes. 您无需检查从2到n的所有可能因子,只需检查素数。 You can also take advantage of multi-processor machines: 您还可以利用多处理器计算机:

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

The number which is only divisible by itself and 1 is known as prime number. 只能被自身整除的数字称为1。 Here is the simplest version of the code for finding prime numbers between 1 to 100. 这是用于查找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
*/
    public static List<Integer> getPrimeNumbers(int from , int to) {
    List<Integer> list = new ArrayList<>();
    for (int i = from;i <= to; i++) {
        int count = 0;
        for (int num = i; num>=1;num--) {
            if(i%num == 0){
                count++;
            }
        }
        if(count ==2) {
            list.add(i);
        }
    }
    return list;
}
public class Prime {
    public static void main(String arg[])
    {
        int count=0;

        for(int i=2;i<100;i++)
        {
            for(int j=2;j<i;j++)
            {
                if(i%j!=0)
                {
                    count++;
                    if(count==i-2)
                    {
                        System.out.print(i+" ");
                    }
                }
            }
            count = 0;
        }
    }
}

public static ArrayList prime(int limit){ 公共静态ArrayList素数(int限制){

    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;
}
public class primenum {

    public static void main(String[] args) {
        for(int i=2;i<100;i++){
            int count=1;
            for(int j=2;j<i;j++){
                if(i%j ==0){
                    count=0;
                    break;
                }

            }if(count==1){
                System.out.println(i);

            }
        }

    }

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

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