-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountingFractionsInARange.java
More file actions
30 lines (25 loc) · 954 Bytes
/
CountingFractionsInARange.java
File metadata and controls
30 lines (25 loc) · 954 Bytes
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
package dev;
public class CountingFractionsInARange {
public static void main(String[] args) {
int maxDenominator = 12000;
int count = 0;
for (int denominator = 2; denominator <= maxDenominator; denominator++) {
int startNumerator = (denominator / 3) + 1; // first number greater than denominator/3
int endNumerator = (denominator - 1) / 2; // last number less than denominator/2
for (int numerator = startNumerator; numerator <= endNumerator; numerator++) {
if (gcd(numerator, denominator) == 1) {
count++;
}
}
}
System.out.println("The number of reduced proper fractions between 1/3 and 1/2 for d <= 12000 is: " + count);
}
private static int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
}