mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-06 00:54:32 +08:00
Add EgyptianFraction
algorithm (#5804)
This commit is contained in:
@ -0,0 +1,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;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user