-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem071.java
More file actions
63 lines (63 loc) · 2.17 KB
/
Problem071.java
File metadata and controls
63 lines (63 loc) · 2.17 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
public class Problem071 {
private static PrimeGen pg;
private static HashMap<Integer, HashSet<Integer>> facts;
private static boolean relPrime(int num, int denom) {
if (pg.isPrime(denom)) {
return true;
}
for (int i : facts.get(num)) {
if (denom % i == 0) {
return false;
}
}
return true;
}
private static void genFactors() {
ArrayList<Integer> primes = pg.getPrimesList();
for (int num = 1; num <= 1000000; num++) {
int numCopy = num;
HashSet<Integer> factors = new HashSet<Integer>();
if (pg.isPrime(num)) {
numCopy = 1;
factors.add(num);
}
for (int factor = 0; primes.get(factor) < numCopy; factor++) {
if (facts.containsKey(numCopy)) {
factors.addAll(facts.get(numCopy));
numCopy = 1;
}
while (numCopy % primes.get(factor) == 0) {
numCopy /= primes.get(factor);
factors.add(primes.get(factor));
}
}
facts.put(num, factors);
}
}
public static void main(String[] args) {
pg = new PrimeGen(1001000);
facts = new HashMap<Integer, HashSet<Integer>>();
genFactors();
double closest = 0;
int closestNum = 0;
for (int denominator = 1; denominator <= 1000000; denominator++) {
int numerator = (int)(denominator * 3.0/7) + 1;
while (((double) numerator) / denominator > closest) {
if (relPrime(numerator, denominator)) {
double fraction = ((double) numerator) / denominator;
if (fraction > closest && fraction < 3.0/7) {
closest = fraction;
closestNum = numerator;
}
}
numerator--;
}
}
System.out.println(closestNum);
}
}