File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.* ;
3+ import java.util.* ;
4+
5+ public class Main {
6+ static int N ;
7+ static StringBuilder sb = new StringBuilder ();
8+
9+ public static void main (String [] args ) throws Exception {
10+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
11+ N = Integer . parseInt(br. readLine());
12+
13+ int [] start = {2 , 3 , 5 , 7 };
14+ for (int s : start) dfs(s, 1 );
15+
16+ System . out. print(sb);
17+ }
18+
19+ static boolean isPrime (int x ) {
20+ if (x < 2 ) return false ;
21+ if (x == 2 ) return true ;
22+ if (x % 2 == 0 ) return false ;
23+ for (int i = 3 ; i * i <= x; i += 2 ) {
24+ if (x % i == 0 ) return false ;
25+ }
26+ return true ;
27+ }
28+
29+ static void dfs (int num , int depth ) {
30+ if (depth == N ) {
31+ sb. append(num). append(' \n ' );
32+ return ;
33+ }
34+ for (int d = 1 ; d <= 9 ; d++ ) {
35+ int next = num * 10 + d;
36+ if (isPrime(next)) dfs(next, depth + 1 );
37+ }
38+ }
39+ }
40+ ```
You can’t perform that action at this time.
0 commit comments