Egyptian fraction solution in kotlin
Kotlin program for Egyptian fraction solution. Here more information.
/*
Kotlin program for
Egyptian fraction solution
*/
class Fraction
{
fun egyptianFraction(a: Int, b: Int): Unit
{
if (a == 0 || b == 0)
{
return;
}
if (b >= a)
{
// Calculate remainder
val remainder: Int = b % a;
// Calculate divisor
var divisor: Int = b / a;
if (remainder != 0)
{
divisor += 1;
print("1/" + divisor + " + ");
this.egyptianFraction(a * divisor - b, b * divisor);
}
else
{
// When remainder is zero
print("1/" + divisor);
}
}
else
{
// When b < a
print("" + (a / b) + " + ");
this.egyptianFraction(a % b, b);
}
}
}
fun main(args: Array < String > ): Unit
{
val task: Fraction = Fraction();
// Two numbers
val a: Int = 7;
val b: Int = 53;
task.egyptianFraction(a, b);
}
Output
1/8 + 1/142 + 1/30104
Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.
New Comment