1
+ import java .util .*;
2
+ import java .io .*;
3
+
4
+ class Main {
5
+
6
+ public static List <List <Integer >> permute (int [] nums ){
7
+ List <List <Integer >> result = new ArrayList <>();
8
+ Permutation (0 ,nums ,result );
9
+ return result ;
10
+ }
11
+
12
+
13
+ public static void Permutation (int i ,int []nums ,List <List <Integer >> result ){
14
+ if (i == nums .length -1 ){
15
+ List <Integer > list = new ArrayList <>();
16
+ for (int n : nums ) list .add (n );
17
+ result .add (list );
18
+ }else {
19
+ for (int j = i ,l = nums .length ;j <l ;j ++){
20
+ int temp = nums [j ];
21
+ nums [j ] = nums [i ];
22
+ nums [i ] = temp ;
23
+ Permutation (i +1 ,nums ,result );
24
+ temp = nums [j ];
25
+ nums [j ]= nums [i ];
26
+ nums [i ] = temp ;
27
+ }
28
+ }
29
+ }
30
+
31
+
32
+ public static boolean checkForPrime (int inputNumber ){
33
+ boolean isItPrime = true ;
34
+ if (inputNumber <=1 ){
35
+ isItPrime =false ;
36
+ return isItPrime ;
37
+ }else {
38
+ for (int i =2 ;i <= inputNumber /2 ;i ++){
39
+ if ((inputNumber %i )== 0 ){
40
+ isItPrime = false ;
41
+ break ;
42
+ }
43
+ }
44
+ return isItPrime ;
45
+ }
46
+ }
47
+
48
+
49
+ public static int concatenateArr (List <Integer > arr ){
50
+ int ans = arr .get (0 );
51
+ for (int i = 1 ; i <arr .size ();i ++){
52
+ int l = (int )Math .floor (Math .log10 (arr .get (i ))+1 );
53
+ ans = ans * (int )Math .pow (10 ,1 );
54
+ ans += arr .get (i );
55
+ }
56
+ return ans ;
57
+ }
58
+
59
+ public static int MathChallenge (int num ) {
60
+ String temp = Integer .toString (num );
61
+ int [] myArray = new int [temp .length ()];
62
+ for (int i = 0 ;i < temp .length ();i ++){
63
+ myArray [i ]= temp .charAt (i )-'0' ;
64
+ }
65
+ List <List <Integer >> array = permute (myArray );
66
+ //System.out.print(concatenateArr(array.get(4)));
67
+ //System.out.print(array);
68
+ for (int i =0 ;i <array .size ();i ++){
69
+ if (checkForPrime (concatenateArr (array .get (i )))==true ){
70
+ return 1 ;
71
+ }
72
+ }
73
+ return 0 ;
74
+ }
75
+
76
+ public static void main (String [] args ) {
77
+ // keep this function call here
78
+ Scanner s = new Scanner (System .in );
79
+ System .out .print (MathChallenge (s .nextLine ()));
80
+ }
81
+ }
0 commit comments