TopcoderArchiveVisit Topcoder
TCO13 Apr 2020
TCO20 Parallel 1B

EllysDifferentPrimes

Problem statement, definition, constraints, and public examples.

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.

Definition

Class:
EllysDifferentPrimes
Method:
getClosest
Parameters:
int
Returns:
int
Method signature:
int getClosest(int N)
(be sure your method is public)

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

  1. 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.
  2. 123457
    Returns: 123457
    The number 123457 itself is a different prime.
  3. 6661337
    Returns: 6701459
    Sometimes the answer is a number larger than N.
  4. 42424242
    Returns: 42398701
Back to all problems