-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblem_042.java
More file actions
47 lines (36 loc) · 1.09 KB
/
Problem_042.java
File metadata and controls
47 lines (36 loc) · 1.09 KB
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
package euler;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.PriorityQueue;
public class Problem_042 {
static int numberOfTriangleWords() {
return numberOfTriangleWords(getWords());
}
private static int numberOfTriangleWords(PriorityQueue<String> words) {
int triangleWordCount = 0;
while (! words.isEmpty()) {
String word = words.poll();
int wordVal = 0;
for (int i = 0; i < word.length(); i++) {
wordVal += ((int) word.charAt(i) - 64);
}
int triangleNum = 0;
for (int n = 1; triangleNum <= wordVal; n++) {
triangleNum = (n*n + n)/2;
if (triangleNum == wordVal) triangleWordCount++;;
}
}
return triangleWordCount;
}
private static PriorityQueue<String> getWords() {
String words = "";
try (BufferedReader reader = new BufferedReader(new FileReader(new File("p042_words.txt")))) {
words = reader.readLine();
} catch(Exception e) {
e.printStackTrace();
}
return new PriorityQueue<String>(Arrays.asList(words.replaceAll("\"", "").split(",")));
}
}