Java find a simple example of prime number (prime number) within 100 _java

Source: Internet
Author: User

Prime numbers are also called primes. A natural number greater than 1, if not divisible by other natural numbers except for 1 and itself, otherwise known as composite numbers. According to the basic theorem of arithmetic, each integer that is larger than 1 is either itself a prime number or a product of a series of prime numbers, and if the order of these primes in the product is not taken into account, the written form is unique. Here is a simple example of the prime number within Java for 100

Copy Code code as follows:

public class Test {

public static void Main (string[] args) {
int i,n,k=0;
for (n = 3; n<=100; n++) {//3~100 all numbers
i=2;
while (I<n) {
if (n%i==0) break; If you can divide it by stating that N is not prime, jump out of the current loop
i++;
}


if (i==n) {//if i==n indicates that n cannot be divisible by 2~n-1, is a prime
k++; Number of statistical outputs
System.out.print (i+ "T");
if (k%6==0)//Per output 5 lines will be wrapped
System.out.println ();
}
}
}
}

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.