简体   繁体   English

打印素数的程序逻辑

[英]program logic of printing the prime numbers

Can any body help to understand this java program? 任何人都可以帮助理解这个java程序吗?

It just prints prime numbers, as you enter how many you want and it works well. 它只是打印素数,当你输入你想要的数量时它运作良好。

class PrimeNumbers
{      
     public static void main(String args[])       
     {         
       int n, status = 1, num = 3;             
       Scanner in = new Scanner(System.in);

       System.out.println("Enter the number of prime numbers you want");         
       n = in.nextInt();

       if (n >= 1)
       {
         System.out.println("First "+n+" prime numbers are :-");
         System.out.println(2);
       }

       for ( int count = 2 ; count <=n ;  )
       {
         for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )
         {
            if ( num%j == 0 )
            {
               status = 0;
               break;
            }
         }
         if ( status != 0 )
         {
            System.out.println(num);
            count++;
         }
         status = 1;
         num++;
      }         
   }
}

I don't understand this for loop condition 我不理解这个循环条件

for ( int j = 2 ; j <= Math.sqrt(num) ; j++ )

why we are taking sqrt of num...which is 3....why we assumed it as 3? 为什么我们采用sqrt的num ...这是3 ....为什么我们假设它为3?

Imagine that n can be divided by a number k that is greater than sqrt(n) . 想象一下, n可以除以大于sqrt(n)的数k Then you have: 然后你有:

n = k * j

where j is a number which must be less than sqrt(n) (if both k and j are greater than sqrt(n) then their product would be greater than n ). 其中j是一个必须小于sqrt(n) (如果kj都大于sqrt(n)那么它们的乘积将大于n )。

So you only need to find the divisors that are less than or equals to sqrt(n) and you can find those that are greater than or equals to sqrt(n) by a simple division. 所以你只需要找到小于或等于sqrt(n)的除数,你可以通过一个简单的除法找到大于或等于sqrt(n)的除数。 In my example, once you have found j , you can find k = n / j . 在我的例子中,一旦找到j ,你就可以找到k = n / j

The line in question is basically trying to find numbers that are factors of your given number (and eliminating them as not-primes). 有问题的线基本上是试图找到作为给定数字因子的数字(并将它们作为非素数消除)。 If you find no factors of a given number then you can say that the number is prime. 如果您没有找到给定数字的因子,那么您可以说该数字是素数。

As far as finding factors goes, you only need to go up to sqrt(N) because if you go any higher you are looking at numbers you have already seen before. 至于找到因素,你只需要达到sqrt(N),因为如果你走得更高,你会看到你以前见过的数字。 This is because every time you find a factor you actually find two factors. 这是因为每次找到一个因素,你实际上找到了两个因素。 If a is a factor of N then N/a and a are both factors of N. 如果a是N的因子,那么N / a和a都是N的因子。

A number N is prime if the only integers that satisfy N = A*B are 1 and N (or N and 1). 如果满足N = A * B的唯一整数是1和N(或N和1),则数N是素数。

Now to check a number N as prime we could search all A from 2 to N and B from 2 to N, to see if N=A*B. 现在检查数字N作为素数我们可以搜索从2到N的所有A和从2到N搜索B,以查看N = A * B. That would take O(N^2) time, and is really inefficient. 这将花费O(N ^ 2)时间,并且效率非常低。

It turns out that we only need to divide N by a number and see if there is a remainder. 事实证明,我们只需要将N除以一个数字,看看是否有余数。 This brings it down to O(N). 这将其归结为O(N)。

And further, we don't need to check all the way from A=2 to A=N. 而且,我们不需要从A = 2到A = N一直检查。 If A is greater than sqrt(N), then B must be less than sqrt(N). 如果A大于sqrt(N),则B必须小于sqrt(N)。 Therefore we only need to check A from 2 to sqrt(N) to see if N/A has a remainder. 因此,我们只需要检查A从2到sqrt(N)以查看N / A是否有余数。

如果我是对的,那么在数学中有一个理论,说在检查从2到2的平方根的数字时,几乎所有素数都可以确定,而不是检查从2到2或2到n / 2的所有数字。

The factor of a number can lie only from 1 to sqrt of num. 数字的因子只能从1到sqrt的num。 So instead of checking from 1 till num for its factors, we check for all numbers from 1 to sqrt(num) so see if any of them divides num. 因此,我们不会检查从1到num的因子,而是检查从1到sqrt(num)的所有数字,看看是否有任何数字除以num。 If any divides num, it is not prime, else it is. 如果有任何除数num,它不是素数,否则它是。 This improves efficiency of code. 这提高了代码的效率。

A quick but dirty solution.. 快速但肮脏的解决方案..

import java.util.Scanner;

class testing
{
    public static void main(String args[])
    {
        Scanner input = new Scanner(System.in);
        int n, i, j, count = 1;
        System.out.print("How many Numbers ? : ");
        n = input.nextInt();
        for(j = 1;count<=n;j++)
        {

            if(j==1 || j==2)
            {
                System.out.println(j);
                count++;
                continue;
            }
            for(i=2;i<=j/2;i++)
            {
                if(j%i==0)
                    break;
                else if(i == j/2 && j%i != 0)
                {
                    System.out.println(j);
                    count++;
                }  
            }
        }       
    }
}
public class PrimeNumber {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    ArrayList a = new ArrayList();
    for (int i = 1; i <= 100; ++i) {
        if (isPrime(i))
            a.add(i);
    }
    System.out.println("List : " + a);

}

public static boolean isPrime(int value) {
    if (value <= 1)
        return false;


    if ((value % 2) == 0)
        return (value == 2);

    for (int i = 3; i <= value - 1; i++) {
        if (value % i == 0) {
            return false;             
        }
    }     

    return true;
}

}

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

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