| 1 | /** |
| 2 | * @license Fraction.js v2.7.0 01/06/2015 |
| 3 | * http://www.xarg.org/2014/03/rational-numbers-in-javascript/ |
| 4 | * |
| 5 | * Copyright (c) 2015, Robert Eisele ([email protected]) |
| 6 | * Dual licensed under the MIT or GPL Version 2 licenses. |
| 7 | **/ |
| 8 | |
| 9 | // Based on http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fractions/egyptian.html |
| 10 | function egyptian(a, b) { |
| 11 | |
| 12 | var res = []; |
| 13 | |
| 14 | do { |
| 15 | var t = Math.ceil(b / a); |
| 16 | var x = new Fraction(a, b).sub(1, t); |
| 17 | res.push(t); |
| 18 | a = x.n; |
| 19 | b = x.d; |
| 20 | } while (a !== 0); |
| 21 | return res; |
| 22 | } |
| 23 | console.log("1 / " + egyptian(521, 1050).join(" + 1 / ")); |