Connection Status:
Competition Arena > EllysDifferentPrimes
TCO20 Round 1B · 2020-04-13 · by espr1t · Brute Force, Simple Math, Simple Search, Iteration
Class Name: EllysDifferentPrimes
Return Type: int
Method Name: getClosest
Arg Types: (int)
Problem Statement

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 int N, return the different prime that is closest to N. If there are two different primes tied for being the closest one to N, return the smaller of the two.

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.
Examples
0)
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.

1)
123457
Returns: 123457

The number 123457 itself is a different prime.

2)
6661337
Returns: 6701459

Sometimes the answer is a number larger than N.

3)
42424242
Returns: 42398701
4)
42
Returns: 41

Submissions are judged against all 105 archived test cases, of which 5 are shown here. Case numbers match the judge’s.

Coding Area

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

Submitting as anonymous