- Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathMathChallenge.java
81 lines (71 loc) · 2.04 KB
/
MathChallenge.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
importjava.util.*;
importjava.io.*;
classMain {
publicstaticList<List<Integer>> permute(int[] nums){
List<List<Integer>> result = newArrayList<>();
Permutation(0,nums,result);
returnresult;
}
publicstaticvoidPermutation(inti,int[]nums,List<List<Integer>> result){
if(i == nums.length-1){
List<Integer> list = newArrayList<>();
for(intn: nums) list.add(n);
result.add(list);
}else{
for(intj = i,l= nums.length;j<l;j++){
inttemp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
Permutation(i+1,nums,result);
temp = nums[j];
nums[j]= nums[i];
nums[i] = temp;
}
}
}
publicstaticbooleancheckForPrime(intinputNumber){
booleanisItPrime = true;
if(inputNumber<=1){
isItPrime=false;
returnisItPrime;
}else{
for(inti=2;i<= inputNumber/2;i++){
if((inputNumber%i)== 0){
isItPrime = false;
break;
}
}
returnisItPrime;
}
}
publicstaticintconcatenateArr(List<Integer> arr){
intans = arr.get(0);
for(inti = 1; i<arr.size();i++){
intl = (int)Math.floor(Math.log10(arr.get(i))+1);
ans = ans * (int)Math.pow(10,1);
ans += arr.get(i);
}
returnans;
}
publicstaticintMathChallenge(intnum) {
Stringtemp = Integer.toString(num);
int[] myArray = newint[temp.length()];
for(inti = 0;i< temp.length();i++){
myArray[i]= temp.charAt(i)-'0';
}
List<List<Integer>> array = permute(myArray);
//System.out.print(concatenateArr(array.get(4)));
//System.out.print(array);
for(inti=0;i<array.size();i++){
if(checkForPrime(concatenateArr(array.get(i)))==true){
return1;
}
}
return0;
}
publicstaticvoidmain (String[] args) {
// keep this function call here
Scanners = newScanner(System.in);
System.out.print(MathChallenge(s.nextLine()));
}
}