-
Notifications
You must be signed in to change notification settings - Fork 4
/
p037.java
104 lines (84 loc) · 1.6 KB
/
p037.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/**
*
* @author gouravrusiya
*
*/
public class p037 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
//int j =0;
//int i =0;
long startTime = System.currentTimeMillis();
int n = 3797 ;
int count = 0;
int count1 = 0,count2=0;
int x = 0;
int sum = 0;
int digit = 0;
int z = 0;
for(n=11;;n++){
count1=0;
count2=0;
digit=0;
x = n;
boolean status = isPrime(x);
if(status){
//System.out.println(x);
while(x!=0){
x = (x/10);
if(x==0)
break;
//System.out.println(x);
boolean s2 = isPrime(x);
if(s2==true){
count2++;
//System.out.println("kk");
}
digit++;
}
//System.out.println(digit+" "+count2);
String str = "" + n;
if((count2==(digit))){
for(int k=0;k<(str.length()-1);++k)
{
String str1 = "";
str1 = str.substring(k+1);
z = Integer.parseInt(str1);
//System.out.println(z);
if(isPrime(z))
count1++;
else
break;
}
//System.out.println(count1);
if((count1==(digit))){
sum = sum + n;
count++;
}
}
}
//System.out.println(count);
if(count==11)
break;
}
System.out.println(sum);
long endTime = System.currentTimeMillis();
System.out.println("Time = "+(endTime-startTime)+" ms");
}
// Logic to check Prime
static boolean isPrime(int n){
int j = 0;
int i = n;
if(n==1)
return false;
for(j=2;j<=(int)Math.sqrt(i);j++){
if((i%j)==0){
break;
}
}
if(j>(int)Math.sqrt(i))
return true;
else
return false;
}
}