Composite Prime
Time limit1sMemory limit1024 MB
Given up to 1e5 queries with N as large as 10^7, find the largest composite number at most N whose every proper contiguous digit substring of length 2 or more is prime.
- Level
Medium6 of 10
- Topics
- Backtracking, Math, Binary search, String
- Solved
- No attempts yet
Problem
factors as , so it is composite. However, every proper contiguous subnumber of made from at least two consecutive digits, namely , , , , and , is prime.
For a positive integer with at least three digits, define a proper contiguous subnumber as follows. Take two or more consecutive digits from the decimal representation of , keep their order, and interpret the result as a nonnegative integer. The number itself is excluded.
A positive integer is called a composite prime if all three conditions below hold.
- has at least three digits.
- is composite.
- Every proper contiguous subnumber of is prime.
A proper contiguous subnumber may start with , and the value itself is possible. For example, the proper contiguous subnumbers of include , , , , , , , , and . Since is not prime, is not a composite prime.
Given a positive integer , find the largest composite prime not greater than .
Input
The first line contains the number of test cases .
Each of the next lines contains one integer .
Output
For each test case, print the largest composite prime not greater than on its own line. If no such number exists, print instead.
Print the answers in the same order as the input test cases.