Prime Gap Sequence
InterviewTime limit1sMemory limit128 MB
Given a number k, find the gap length between the two consecutive primes surrounding it if k is composite, otherwise output 0.
- Level
Easy3 of 10
- Topics
- Math, Number theory, Binary search
- Solved
- No attempts yet
Problem
For two consecutive primes and , the composite numbers strictly between them (a composite number is a positive integer that is neither prime nor 1) form a prime gap sequence of length .
Given a positive integer , write a program that determines the length of the prime gap sequence containing . If no prime gap sequence contains , its length is 0.
For example, the prime gap sequence between the primes 23 and 29 is , and its length is 6.
Input
The first line contains the number of test cases . Each test case is a single line containing one integer . Every integer is greater than 1 and at most the 100000th prime, which is 1299709.
Output
For each test case, if is a composite number, output the length of the prime gap sequence that contains . Otherwise, output 0.