-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathP41PandigitalPrime.java
60 lines (52 loc) · 1.04 KB
/
P41PandigitalPrime.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
public class P41PandigitalPrime {
public static int max = 10;
public int maxN;
public Boolean[] Mark = new Boolean[10];
public long maxPrime = 0;
public void Init(){
for (int i=1;i<max;i++)
Mark[i] = true;
}
public String str = "";
public void Try(int n)
{
if (n > maxN)
{
long val = Long.parseLong(str);
if (IsPrime(val)){
maxPrime = maxPrime<val? val: maxPrime;
System.out.println(maxPrime + " " + val);
}
return;
}
for (int i=1;i<=maxN;i++)
if (Mark[i])
{
Mark[i] = false;
str = str + i;
Try(n+1);
Mark[i] = true;
str = str.substring(0, str.length()-1);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
P41PandigitalPrime rv = new P41PandigitalPrime();
rv.Init();
for (int i=9;i>0;i--)
{
rv.maxN = i;
rv.Try(1);
if (rv.maxPrime != 0)
break;
}
}
public static Boolean IsPrime(long n)
{
if (n == 1) return false;
for (long i=2;i <= Math.round(Math.sqrt(n));i++)
if (n%i==0)
return false;
return true;
}
}