using System; using System.IO; using System.Linq; using System.Collections.Generic; using System.Text; public class Program { public void Proc() { int fromNum = int.Parse(Reader.ReadLine()); int toNum = int.Parse(Reader.ReadLine()); SetPrimeList(toNum); int[] flags = new int[10].Select(a=>-1).ToArray(); int maxlen = 0; int firstPrime = 0; for (int i = 0; i < PrimeList.Length; i++) { if (PrimeList[i] < fromNum) { continue; } if(PrimeList[i]>toNum) { if (i - flags.Where(a=>a>=0).Min()>=maxlen) { firstPrime = PrimeList[flags.Where(a => a >= 0).Min()]; } break; } int prime = PrimeList[i]; int hash = GetHash(prime); if(flags[hash]>=0) { int idx = flags.Where(a => a >= 0).Min(); int len = flags.Where(a => a >= 0).Max() - idx + 1; if (maxlen <= len) { maxlen = len; firstPrime = PrimeList[idx]; } int target = flags[hash]; for (int j = 0; j < flags.Length; j++) { if (flags[j] <= target) { flags[j] = -1; } } } flags[hash] = i; } int lastIdx = flags.Where(a => a >= 0).Min(); int lastLen = flags.Where(a => a >= 0).Max() - lastIdx + 1; if(maxlen <= lastLen) { firstPrime = PrimeList[lastIdx]; } Console.WriteLine(firstPrime); } private int GetHash(int num) { int tmp = num; while(tmp >= 10) { tmp = tmp.ToString().Select(a => int.Parse(a.ToString())).Sum(); } return tmp; } private void SetPrimeList(int num) { bool[] flags = new bool[num + 1]; List tmp = new List(); for (int i = 2; i <= num; i++) { if (flags[i]) { continue; } tmp.Add(i); int max = num / i; for (int j = 1; j <= max; j++) { flags[i * j] = true; } } this.PrimeList = tmp.ToArray(); } private int[] PrimeList; public class Reader { private static StringReader sr; public static bool IsDebug = false; public static string ReadLine() { if (IsDebug) { if (sr == null) { sr = new StringReader(InputText.Trim()); } return sr.ReadLine(); } else { return Console.ReadLine(); } } private static string InputText = @" 1 11 "; } public static void Main(string[] args) { #if DEBUG Reader.IsDebug = true; #endif Program prg = new Program(); prg.Proc(); } }