자바 7, 139 141 바이트
long c(int a,String b){for(long n=2,i,x;;n++){for(x=n,i=2;i<x;x=x%i++<1?0:x);if(x>1&(n+"").length()==a&(n+"").matches("["+b+"]+"))return n;}}
상기 32 비트 수를 지원하여 +2 바이트 (변경 int으로 long)
입력 형식 : 정수 (ie 4) 및 문자열 (ie"12" )
설명:
long c(int a, String b){ // Method with the two input parameters specified above
for(long n = 2, i, x; ; n++){ // Loop from 2 going upwards
for(x = n, i = 2; i < x; x = x % i++ < 1 ? 0 : x); // Prime check for `n`
if (x > 1 // if `n` is a prime (if `x` > 1 after the loop above it means `n` is a prime)
& (n+"").length() == a // AND if `n` has a length equal to the input integer
& (n+"").matches("["+b+"]+")){ // AND if `n` only contains the specified digits of the input String (using a regex)
return n; // Then we have our answer
}
} // If no answer is available for the given input, it continues looping
}
테스트 코드 :
여기에서 시도하십시오.
참고 : 두 번째 테스트 사례는 매우 오랜 시간 동안 반복되므로 비활성화됩니다.
class M{
static long c(int a,String b){for(long n=2,i,x;;n++){for(x=n,i=2;i<x;x=x%i++<1?0:x);if(x>1&(n+"").length()==a&(n+"").matches("["+b+"]+"))return n;}}
public static void main(String[] a){
System.out.println(c(4, "12"));
//System.out.println(c(10, "047"));
System.out.println(c(6, "555555555515555555555"));
}
}
산출:
2111
115151