Java using Sieve method to find prime numbers

Source: Internet
Author: User

The description now gives you some numbers that require you to write a program that outputs the nearest prime number adjacent to these integers, and outputs its distance between the lengths. If there is an equidistant length prime, the value of the left side and the corresponding distance are output.
If the input integer itself is a prime number, the output of the prime number itself, the distance output 0

 
Input
The first line gives the number of test data groups N (0<n<=10000)
the next n rows have an integer M (0<m<1000000) per line,
Output
Outputs two integers a B per line.
where a represents the nearest prime number to the corresponding test data, and B indicates the distance between them.
Sample input
36810
Sample output
5 17 111 1

1 ImportJava.util.Scanner;2 3  Public classMain {4      Public Static voidMain (string[] args) {5         intT;6Scanner scanner=NewScanner (system.in);7         intNumber ;8         intresult;9         intdistance;Ten         inttemp; One         intn=1000010; A         intflag[]=New int[N]; -         inti; -         intJ; the          -          for(i=0;i<n;i++) -Flag[i]=0; -          +Flag[0]=1; -Flag[1]=1; +          A          for(i=2;i*i<n;i++){ at             if(flag[i]==0){ -                  for(j=i*i;j<n;j+=i) -Flag[j]=1; -             } -         } -          int=scanner.nextint (); -          while(true){ to             if(t==0) +                  Break; -t--; the              *Number=scanner.nextint (); $             Panax Notoginseng             if(number==1){ -System.out.println ("2 1"); the                 Continue; +             } A              thetemp=Number ; +              while(true){ -                 if(flag[temp]==0){ $result=temp; $distance=number-temp; -                      Break; -                 } thetemp--; -             }Wuyi              thetemp=Number ; -              while(true){ Wu                 if(flag[temp]==0){ -                     if(temp-number<distance) { Aboutresult=temp; $distance=temp-Number ; -                     } -                      Break; -                 } Atemp++; +             } the              -System.out.println (result+ "" +distance); $         } the     } the}

Java using Sieve method to find prime numbers

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.