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
{
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
.