Problem:
Problem B: Prime Gap

The sequence of n - 1 consecutive composite numbers (positive integers that are not prime and
not equal to 1) lying between two successive prime numbers p and p + n is called a prime gap of length n. For example, (24, 25, 26, 27, 28) between 23 and 29 is a prime gap of length 6.

Your mission is to write a program to calculate, for a given positive integer k, the length of the prime gap that contains k. For convenience, the length is considered 0 in case no prime gap
contains k.

Input

The input is a sequence of lines each of which contains a single positive integer. Each positive
integer is greater than 1 and less than or equal to the 100000th prime number, which is 1299709.
The end of the input is indicated by a line containing a single zero.

Output

The output should be composed of lines each of which contains a single non-negative integer. It
is the length of the prime gap that contains the corresponding positive integer in the input if it
is a composite number, or 0 otherwise. No other characters should occur in the output.

Sample Input

10
11
27
2
492170
0

Output for the Sample Input

4
0
6
0
114

Solution in Rust (judge verdict: Accepted)

extern crate core;

use std::fmt;
use std::cmp::{Ordering, min, max};
use std::fmt::{Display, Error, Formatter};
use std::f32::MAX;
use std::ops::{Add, Sub, Mul, Div, Neg, Index, IndexMut};
use std::collections::{BTreeMap, VecDeque, BinaryHeap};

fn show<T: Display>(vec: &Vec<T>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        print!("[{}", vec[0]);
        for i in 1 .. vec.len() {
            print!(", {}", vec[i]);
        }
        println!("]");
    }
}
fn show2<T: Display>(vec: &Vec<Vec<T>>) {
    if vec.is_empty() {
        println!("[]");
    }else {
        for l in vec {
            show(l);
        }
    }
}
macro_rules! read_line{
    () => {{
        let mut line = String::new();
        std::io::stdin().read_line(&mut line).ok();
        line
    }};
    (delimiter: ' ') => {
        read_line!().split_whitespace().map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (delimiter: $p:expr) => {
        read_line!().split($p).map(|x|x.to_string()).collect::<Vec<_>>()
    };
    (' ') => {
        read_line!(delimiter: ' ')
    };
    ($delimiter:expr) => {
        read_line!(delimiter: $delimiter)
    };
    (' '; $ty:ty) => {
        read_line!().split_whitespace().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
    ($delimiter:expr; $ty:ty) => {
        read_line!($delimiter).into_iter().map(|x|x.parse::<$ty>().ok().unwrap()).collect::<Vec<$ty>>()
    };
}
macro_rules! read_value{
    () => {
        read_line!().trim().parse().ok().unwrap()
    }
}
macro_rules! let_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}
macro_rules! let_mut_all {
    ($($n:ident:$t:ty),*) => {
        let line = read_line!(delimiter: ' ');
        let mut iter = line.iter();
        $(let mut $n:$t = iter.next().unwrap().parse().ok().unwrap();)*
    };
}

fn primes(upper_limit: usize) -> Vec<usize> {
    let mut is_prime = vec![true; upper_limit];
    let mut i = 4;
    while i < upper_limit {
        is_prime[i] = false;
        i += 2;
    }
    i = 3;
    while i * i < upper_limit {
        if is_prime[i] {
            let mut j = i * i;
            while j < upper_limit {
                is_prime[j] = false;
                j += i;
            }
        }
        i += 2;
    }
    let mut result = Vec::new();
    for i in 2 .. upper_limit {
        if is_prime[i] {
            result.push(i);
        }
    }
    result
}
fn lower_bound(vec: &Vec<usize>, target: usize) -> usize {
    let mut left = 0;
    let mut right = vec.len();
    while left < right {
        let mid = (left + right) / 2;
        if vec[mid] < target {
            left = mid + 1;
        }else {
            right = mid;
        }
    }
    right
}
fn upper_bound(vec: &Vec<usize>, target: usize) -> usize {
    let mut left = 0;
    let mut right = vec.len();
    while left < right {
        let mid = (left + right) / 2;
        if vec[mid] <= target {
            left = mid + 1;
        }else {
            right = mid;
        }
    }
    right
}
fn main() {
    let prime = primes(1299709 + 1);
    loop {
        let_all!(n: usize);
        if n == 0 {
            return
        }
        println!("{}", prime[lower_bound(&prime, n)] - prime[upper_bound(&prime, n) - 1]);
    }
}