Twin Primes

Twin Primes

Twin primes are consecutive prime numbers whose difference is 2. For example, (3,5), (11,13), (17,19) are all twin primes. We define the distance of any twin prime pair from a positive integer as follows:

If (p1,p2) is a twin prime pair and n is a positive integer then the distance of the twin prime pair from n is: minimum(abs(n-p1), abs(n-p2)) where abs returns the absolute value of its argument, and minimum returns the smaller of its two arguments.

Write a program that reads in a positive integer n and prints out the twin prime pair that has the least distance from n.

For example if n is 30 the pair is (29,31), if n is 13 it is (11,13), if n is 49 it is (41,43), if n is 54 it is (59,61).

Sample data:

Input:    Give the number: 34
Output: Number read in is:34
p1=29 p2=31
Input: Give the number:60
Output: Number read in is:60

Have something to say? Log in to comment on this post.