- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
Latest commit
116 lines (88 loc) · 4.55 KB
/
Copy pathMain.java
File metadata and controls
116 lines (88 loc) · 4.55 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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
@author: mc-es
Problem 33
The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplify it may incorrectly believe that 49/98 = 4/8, which is correct, is obtained by cancelling the 9s.
We shall consider fractions like, 30/50 = 3/5, to be trivial examples.
There are exactly four non-trivial examples of this type of fraction, less than one in value, and containing two digits in the numerator and denominator.
If the product of these four fractions is given in its lowest common terms, find the value of the denominator.
Answer: 100
*/
publicclassMain {
publicstaticvoidmain(String[] args) {
intnumeratorProduct = 1; // numeratorProduct variable stores the product of all numerators of the curious fractions
intdenominatorProduct = 1; // denominatorProduct variable stores the product of all denominators of the curious fractions
for (inti = 10; i < 100; i++) { // iterate over all two-digit numerators
for (intj = i + 1; j < 100; j++) { // iterate over all two-digit denominators greater than the numerator
if (isCuriousFraction(i, j)) { // check if the fraction is curious
numeratorProduct *= i; // multiply the numerator with the previous numerators of curious fractions
denominatorProduct *= j; // multiply the denominator with the previous denominators of curious fractions
}
}
}
// calculate the greatest common divisor of the numerator product and denominator product
intgcd = gcd(numeratorProduct, denominatorProduct);
// calculate the reduced denominator by dividing the denominator product with the greatest common divisor
intreducedDenominator = denominatorProduct / gcd;
System.out.println(reducedDenominator);
}
// returns true if the fraction is curious, false otherwise
privatestaticbooleanisCuriousFraction(intnumerator, intdenominator) {
if (numerator >= denominator) {
returnfalse; // if the numerator is greater or equal to the denominator, the fraction is not proper
}
intcommonDigit = getCommonDigit(numerator, denominator); // find the common digit between numerator and denominator
if (commonDigit == -1) {
returnfalse; // if there is no common digit, the fraction is not curious
}
// remove the common digit from numerator and denominator
intreducedNumerator = removeDigit(numerator, commonDigit);
intreducedDenominator = removeDigit(denominator, commonDigit);
if (reducedDenominator == 0) {
returnfalse; // if the reduced denominator is zero, the fraction is not valid
}
// check if the reduced fraction is equal to the original fraction
returnnumerator * reducedDenominator == reducedNumerator * denominator;
}
// returns the common digit between two integers, or -1 if there is no common digit
privatestaticintgetCommonDigit(intx, inty) {
for (inti = 1; i <= 9; i++) { // iterate over all possible digits
if (hasDigit(x, i) && hasDigit(y, i)) { // check if both x and y have the digit
returni; // return the common digit
}
}
return -1; // return -1 if there is no common digit
}
// returns true if the integer x has the digit, false otherwise
privatestaticbooleanhasDigit(intx, intdigit) {
while (x > 0) { // iterate over all digits of x
if (x % 10 == digit) { // check if the digit is equal to the last digit of x
returntrue; // return true if the digit is found
}
x /= 10; // remove the last digit of x
}
returnfalse; // return false if the digit is not found
}
// returns the integer x with the digit removed
privatestaticintremoveDigit(intx, intdigit) {
intresult = 0; // initialize the result variable to zero
intmultiplier = 1; // initialize the multiplier variable to one
while (x > 0) { // iterate over all digits of x
intlastDigit = x % 10; // get the last digit of x
if (lastDigit != digit) { // if the digit is not equal to the last digit of x
result += lastDigit * multiplier; // add the last digit to the result variable
multiplier *= 10; // multiply the multiplier by 10
}
x /= 10; // remove the last digit of x
}
returnresult; // return the result variable
}
// returns the greatest common divisor of two integers
privatestaticintgcd(inta, intb) {
while (b != 0) { // while b is not zero
inttemp = b; // store the value of b in a temporary variable
b = a % b; // set b to the remainder of a divided by b
a = temp; // set a to the previous value of b
}
returna; // return the greatest common divisor
}
}