EllysDifferentPrimes
TCO20 Round 1B · 2020-04-13 · by espr1t
Problem Statement
Elly calls a positive integer a different prime if it's a prime number and all its digits are distinct. For example, 13, 5419, and 102345697 are different primes but 1234 and 131 aren't. (The number 1234 is not a prime number and the number 131 has a repeated digit.)
Given a positive
Notes
- A prime number (or a prime) is a natural number greater than 1 that has no positive divisors other than 1 and itself. Thus, the first few primes are 2, 3, 5, 7, 11, 13, 17, ...
Constraints
- N will be between 1 and 50,000,000, inclusive.
100 Returns: 97
The closest prime number to 100 is 101, but this is not a valid answer because the digit 1 occurs twice in 101. The next two closest primes to 100 are 97 and 103. Both of them are different primes and both of them have the difference 3 from N. In this situation we should return the smaller of the two numbers, so the correct return value is 97.
123457 Returns: 123457
The number 123457 itself is a different prime.
6661337 Returns: 6701459
Sometimes the answer is a number larger than N.
42424242 Returns: 42398701
42 Returns: 41
Submissions are judged against all 105 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class EllysDifferentPrimes with a public method int getClosest(int N) · 105 test cases · 2 s / 256 MB per case