-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution33.java
More file actions
61 lines (56 loc) · 2.04 KB
/
Copy pathSolution33.java
File metadata and controls
61 lines (56 loc) · 2.04 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
import java.lang.StringBuilder;
public class Solution33{
static double gcd(double a, double b){
if(a == 0){
return b;
}
else{
return gcd(b % a, a);
}
}
static double removeCommon(double a, double b){
StringBuilder str1 = new StringBuilder(Double.toString(a));
StringBuilder str2 = new StringBuilder(Double.toString(b));
if(str1.charAt(0) == str2.charAt(1)){
str1.deleteCharAt(0);
str2.deleteCharAt(1);
return Double.parseDouble(str1.toString()) / Double.parseDouble(str2.toString());
}
if(str1.charAt(1) == str2.charAt(0)){
str1.deleteCharAt(1);
str2.deleteCharAt(0);
return Double.parseDouble(str1.toString()) / Double.parseDouble(str2.toString());
}
return 1;
}
static boolean shareDigit(double a, double b){
//works for two-digit numbers.
StringBuilder str1 = new StringBuilder(Double.toString(a));
StringBuilder str2 = new StringBuilder(Double.toString(b));
return str1.charAt(0) == str2.charAt(0) || str1.charAt(1) == str1.charAt(1);
}
public static void main(String[] args) {
double numerator = 1;
double denominator = 1;
for(double i = 10; i <= 98; i++){
for(double j = i + 1; j <= 99; j++){
if(i % 10 == 0|| j % 10 == 0){
continue;
}
else{
//if i and j share common digit
if(shareDigit(i, j)){
double expected = i / j;
double fact = gcd(i,j);
double reduced = removeCommon(i, j);
if(reduced == expected){
numerator *= (i / fact);
denominator *= (j / fact);
}
}
}
}
}
System.out.println(denominator / gcd(numerator,denominator));
}
}