-
Notifications
You must be signed in to change notification settings - Fork 19.6k
/
Copy pathEgyptianFraction.java
35 lines (32 loc) · 1014 Bytes
/
EgyptianFraction.java
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
package com.thealgorithms.greedyalgorithms;
import java.util.ArrayList;
import java.util.List;
/**
* Class to represent a fraction as a sum of unique unit fractions.
* Example:
* 2/3 = 1/2 + 1/6
* 3/10 = 1/4 + 1/20
*
* @author Hardvan
*/
public final class EgyptianFraction {
private EgyptianFraction() {
}
/**
* Calculates the Egyptian Fraction representation of a given fraction.
*
* @param numerator the numerator of the fraction
* @param denominator the denominator of the fraction
* @return List of unit fractions represented as strings "1/x"
*/
public static List<String> getEgyptianFraction(int numerator, int denominator) {
List<String> result = new ArrayList<>();
while (numerator != 0) {
int x = (int) Math.ceil((double) denominator / numerator);
result.add("1/" + x);
numerator = numerator * x - denominator;
denominator = denominator * x;
}
return result;
}
}