# The first day of Java Learning: Data base, the implementation of printing prime numbers

Source: Internet
Author: User
Tags square root

The first day of the formal study of Java, write down this article on the solution of prime numbers, hoping to improve better.

First of all, the problem of solving prime numbers on C was not very much at that time. The method that has been used is to increment from 2 to n-1, if a number can be divisible by n in this process, then this number is not prime. This is, of course, the simplest way to do it without problems.

After reading the introduction of some articles, with the growth of mathematical knowledge, today in the study of the Java language to achieve this idea, the process is recorded as follows:

1, except 2, all prime numbers are odd, so the number of cycles can be reduced in general.

2, the increment of the boundary should not be n-1, can be optimized. For the integer n, there is the largest n, so that n squared <=n, in the natural number range, n must be decomposed into two number of products, assuming that the result of decomposition is n1*n2=n, and n1<n2; so there must be n1<n<n2. This concludes that, in the process of seeking prime numbers, the maximum limit of increment is the two square root of N.

3. Implementation code: (Java)

Determine if one is prime
Scanner input=new Scanner (system.in);
int Number=input.nextint ();
Boolean flag=true;

If.. Else: When the number entered is less than 2 o'clock, an error is displayed, and when the input is 2 o'clock, the display is prime, and when the input is greater than 2, the loop is judged
if (number<2) {
SYSTEM.OUT.PRINTLN ("Input is illegal, please enter an integer greater than 2.") Termination of the program ");
}else if (number==2) {
System.out.println ("2 is Prime");
}else{

If you can divide the flag by 2, otherwise start with 3 for loop to determine if flag is modified
if (number%2==0) {
Flag=false;
}else{
for (int i=3;i<=math.sqrt (number); i+=2) {
if (number%i==0) {
Flag=false;
Break
}
}
}

Based on whether the flag has been modified, the prime number is judged
if (flag) {
System.out.println (number+ "is prime");
}else{
System.out.println (number+ "not prime number");
}
}

4, for the printing of all the prime numbers in the problem, with the above method one of the judgment is often wasted resources. It is much more efficient to print using the Sun method.

5, the basic idea of the sun is to remove all the number of mass in N, the remaining is prime, such as the printing of 10 of prime numbers within:

Starting from the a=2, first remove twice times the number, namely 4, 6, 8;

a++,3, is not removed must be prime, and then removed 3 times times the number of numbers, 6, 9;

a++,4, has been removed, the cycle continues;

a++,5, not removed must be prime, minus 5 multiples of 10;

At this stage, the maximum number of loops is N/2, since at least twice times the number of prime numbers needs to be removed. For greater than N/2 re-processing, if it is not removed by the previous operation, then it must be prime.

Code implementation (Java):

Filtering method for printing of all prime numbers within 100
Byte[] Bl=new byte[100];

K is used to store each array subscript to be dropped
int k;
Set all values to 1
for (int j=0;j<100;j++) {
Bl[j]=1;
}

The Sun method removes all non-prime
for (int i=1;i<=50;i+=1) {
If the array value is not 1, continue
if (bl[i]==0) {
Continue
}
for (int j=i;j<100/(i+1); j + +) {
k= (i+1) * (j+1)-1;
bl[k]=0;
}
}
System.out.print (' 2 ' + "");
for (int i=2;i<100;i+=2) {
if (bl[i]==1)
System.out.print ((i+1) + "");
}
System.out.println (); The result should be 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
}

The first day of Java Learning: Data base, the implementation of printing prime numbers

Related Keywords:

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.