Skip to main content

Egyptian fraction solution in typescript

Ts program for Egyptian fraction solution. Here problem description and other solutions.

/*
  TypeScript program for
  Egyptian fraction solution
*/
class Fraction
{
	public egyptianFraction(a: number, b: number)
	{
		if (a == 0 || b == 0)
		{
			return;
		}
		if (b >= a)
		{
			// Calculate remainder
			var remainder = b % a;
			// Calculate divisor
			var divisor = parseInt(b / a);
			if (remainder != 0)
			{
				divisor++;
				console.log("1/" + divisor + " + ");
				a = a * divisor - b;
				b = b * divisor;
				this.egyptianFraction(a, b);
			}
			else
			{
				// When remainder is zero
				console.log("1/" + divisor);
			}
		}
		else
		{
			// When b < a
			console.log((parseInt(a / b)) + " + ");
			this.egyptianFraction(a % b, b);
		}
	}
	public static main(args: string[])
	{
		var task = new Fraction();
		// Two numbers
		var a = 7;
		var b = 53;
		task.egyptianFraction(a, b);
	}
}
Fraction.main([]);
/*
 file : code.ts
 tsc --target es6 code.ts
 node code.js
 */

Output

1/8 +
1/142 +
1/30104




Comment

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