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