program logic of printing the prime numbers

前端 未结 7 566
别那么骄傲
别那么骄傲 2021-01-25 22:15

Can any body help to understand this java program?

It just prints prime numbers, as you enter how many you want and it works well.

class PrimeNumbers
{           


        
7条回答
  •  生来不讨喜
    2021-01-25 22:31

    Imagine that n can be divided by a number k that is greater than sqrt(n). 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).

    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. In my example, once you have found j, you can find k = n / j.

提交回复
热议问题