-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem37.java
92 lines (81 loc) · 2.14 KB
/
Problem37.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
package com.javamultiplex.projecteuler;
import java.util.Arrays;
public class Problem37 {
private static int limit = 1000000; // 1 million
private static boolean primes[] = new boolean[limit];
/**
* Getting all prime numbers from 1 to 1 million using Sieve of Eratosthenes
* algorithm. Here we are calculating all prime numbers at the starting
* only.
*
*/
static {
// Instantiating all array elements to 'true'.
Arrays.fill(primes, true);
int sqrt = (int) Math.sqrt(limit);
// 1 is not prime number.
primes[1] = false;
for (int i = 2; i <= sqrt; i++) {
for (int j = 2 * i; j < limit; j = j + i) {
primes[j] = false;
}
}
}
public static void main(String[] args) {
int start = 11, count = 0, sum = 0;
while (true) {
if (primes[start]) {
if (isLeftToRightDigitsPrime(start)
&& isRightToLeftDigitsPrime(start)) {
count++;
sum += start;
if (count == 11) {
break;
}
}
}
start++;
}
System.out.println("The sum of eleven primes that are both truncatable from left to right and right to left is : "+sum);
}
private static boolean isRightToLeftDigitsPrime(int start) {
// Convert int to String.
String number = String.valueOf(start);
String temp1 = null;
int temp2 = 0, count = 0;
boolean result = false;
int length = number.length();
for (int i = length - 1; i > 0; i--) {
temp1 = number.substring(0, i);
// Convert String to int.
temp2 = Integer.valueOf(temp1);
if (primes[temp2]) {
count++;
}
}
if (count == length - 1) {
result = true;
}
return result;
}
private static boolean isLeftToRightDigitsPrime(int start) {
// Convert int to String.
String number = String.valueOf(start);
String temp1 = null;
int temp2 = 0, count = 0;
boolean result = false;
int length = number.length();
for (int i = 1; i < length; i++) {
temp1 = number.substring(i);
// Convert String to int.
temp2 = Integer.valueOf(temp1);
if (primes[temp2]) {
count++;
}
}
if (count == length - 1) {
result = true;
}
return result;
}
}