Determine the prime number within 100 (prime number)

Source: Internet
Author: User
Tags square root

 Public classDemo3 { Public Static voidMain (string[] args) {Booleanb;  for(inti = 2; I < 100; i++) {//Traverse all 2-100 of the number B=true; intK = (int) math.sqrt (i); Open Square for(intj = 2; J <= K; J + +) {                if(i% j = 0) {b=false;  Break; }            }            if(b) {System.out.println (i); }        }    }}

Here, there are 2 key variables, and I estimate that you can see the algorithm by explaining it.
1. The function of the variable k is to optimize the entire algorithm, because for example, to determine whether a number 13 is a prime, we do not need to cycle from 2 to 13. As long as the loop to the 13 square root. 13 The open root is about 3.6, and the strong to int type is 3. That is, if you check 2, 3 divisible by 13. If not, 13 is definitely a prime number. Because, for example, 48, you have detected that 4 is divisible equal to 12, then the continuation loop is more than Math.sqrt (48), which is simply to get a result that is 12 equals 4 in turn. This is not necessary.

2. About Variables J. Note that the 1:J is defined in the outermost loop body. At this point, the value of J is the initial 0. Then J starts at 2 and ends at less than or equal to K. This is the number of loops that control the attempt to divide the integer. Once it is found that there are several divisible I in this range, the loop is out

So, for the part you don't understand, first make sure that the program just executes to break, which means the number is prime.
For example, if we are k = 10, then the detection of I from j = 2 to 10 will not be divisible by J. When J = 7, for example, can be rounded out, and then jump out of the current inner layer loop. At this time, J is obviously not greater than K, because as long as it is halfway out, because the inner loop (j = K J + +) control, as long as in the cycle of jumping out of, then J. certainly <= K.

Only the loop to J = 10 is still not break, according to the order of execution of the For loop, J + + is executed, then the J <= K is true, then the next loop is true, otherwise the loop ends. And here, if it's not divisible by 10, J is self-increasing on the basis of 10. This time J = 11.

Then if (J > K) is not established, then I will not be output.

To sum up: if Midway or the last cycle, to find the number of divisible, then because of the break relationship, the final will not execute J + +, so J <= K condition is guaranteed. In other words, if J > K (that is, the inverse of J <= K) indicates that the number that can be divisible is not found. In fact, J Max can only be equal to k+1.

Determine the prime number within 100 (prime number)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.