Prime Number distance problem, prime number distance

Source: Internet
Author: User

Prime Number distance problem, prime number distance

/* The description now gives you some numbers. You need to write a program, output the adjacent prime numbers of these integers, and output their respective lengths. If there is a prime number with an equal-distance length between the left and right, the value on the left and the corresponding distance are output. If the input integer itself is a prime number, the output is the prime number, and the number of test data groups is N (0 <N <= 10000) from the first line of the output 0) each row of the next N Rows has an integer M (0 <M <1000000), and two integers a B are output for each row. A Indicates the prime number closest to the test data, and B indicates the distance between them. Sample input 36810 sample output 5 17 111 100*00 * // * 365 187 11011 1 */# include <stdio. h> # include "stdlib. h "// determine whether it is a prime number int isPrime (int num) {if (num = 1) {return 0 ;}for (int I = 2; I * I <= num; I ++) {if (num % I = 0) {return 0 ;}} return 1 ;}int main (int argc, const char * argv []) {// insert code here... int n, num, numUp, numDown, deltUp, deltDown; scanf ("% d", & n); while (n --) {scanf ("% d ", & num); if (isPrime (num) {printf ("% d 0 \ N ", num);} else {numUp = numDown = num; while (! IsPrime (numUp) {numUp + = 1;} while (! IsPrime (numDown) & numDown> 0) {numDown-= 1;} deltUp = numUp-num; deltDown = num-numDown; if (numDown = 0) {printf ("% d \ n", numUp, deltUp);} else if (deltUp> = deltDown) {printf ("% d \ n", numDown, deltDown);} else {printf ("% d \ n", numUp, deltUp) ;}} return 0 ;}

 

Related Article

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.