Alice and Bob begin their day with a quick game. They first choose a starting number X0 ≥ 3 and try to reach one million by the process described below.
Alice goes first and then they take alternating turns. In the i-th turn, the player whose turn it is selects a prime number smaller than the current number, and announces the smallest multiple of this prime number that is not smaller than the current number.
Formally, he or she selects a prime p < Xi - 1 and then finds the minimum Xi ≥ Xi - 1 such that p divides Xi. Note that if the selected prime palready divides Xi - 1, then the number does not change.
Eve has witnessed the state of the game after two turns. Given X2, help her determine what is the smallest possible starting number X0. Note that the players don't necessarily play optimally. You should consider all possible game evolutions.
The input contains a single integer X2 (4 ≤ X2 ≤ 106). It is guaranteed that the integer X2 is composite, that is, is not prime.
Output a single integer — the minimum possible X0.
14
6
20
15
8192
8191
In the first test, the smallest possible starting number is X0 = 6. One possible course of the game is as follows:
- Alice picks prime 5 and announces X1 = 10
- Bob picks prime 7 and announces X2 = 14.
In the second case, let X0 = 15.
- Alice picks prime 2 and announces X1 = 16
- Bob picks prime 5 and announces X2 = 20.
//用到的算法:埃式筛法,这个算法在这里是以较快的时间找出一个数是最大质因子,有一个之前疑惑的点是x1,x0的范围,但是,经过大牛的点拨,豁然开朗
#include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned LL
#define fi first
#define se second
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define max3(a,b,c) max(a,max(b,c))
const int INF = 0x3f3f3f3f;
const LL mod = 1e9+7;
typedef pair<int,int> pll;
const int N = 1e6+5;
int Find[N];
int vis[N];
void init(){
for(int i = 2; i < N; i++){
if(!vis[i]){
Find[i] = -1;
for(int j = i*2; j < N; j += i)
vis[j] = 1, Find[j] = i;
}
}
}
int main(){
int n;
scanf("%d", &n);
int ans = n;
init();
for(int i = n - Find[n] + 1; i <= n; i++)
ans = min(ans, i-Find[i]+1);
printf("%d", ans);
return 0;
}