-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP37TruncatablePrimes.java
More file actions
47 lines (43 loc) · 963 Bytes
/
P37TruncatablePrimes.java
File metadata and controls
47 lines (43 loc) · 963 Bytes
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
public class P37TruncatablePrimes {
public static void main(String[] args) {
// TODO Auto-generated method stub
long max = 1000000;
int count = 0;
long sum = 0;
for (int n=13;n<=max;n++)
{
if (!IsTruncatablePrimes(n)) continue;
count++;
System.out.println(count + " " + n);
sum += n;
}
System.out.println("" + sum);
}
public static Boolean IsTruncatablePrimes(long n)
{
if (!IsPrime(n)) return false;
String str = "" + n;
while (str.length() > 1)
{
str = str.substring(1);
long val = Long.parseLong(str);
if (!IsPrime(val)) return false;
}
str = "" + n;
while (str.length() > 1)
{
str = str.substring(0, str.length()-1);
long val = Long.parseLong(str);
if (!IsPrime(val)) return false;
}
return true;
}
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;
}
}