|
Last change
on this file since 2058e5c was 2058e5c, checked in by istevanoska <ilinastevanoska@…>, 6 months ago |
|
Working / before login
|
-
Property mode
set to
100644
|
|
File size:
585 bytes
|
| Line | |
|---|
| 1 | /*
|
|---|
| 2 | Fraction.js v5.0.0 10/1/2024
|
|---|
| 3 | https://raw.org/article/rational-numbers-in-javascript/
|
|---|
| 4 |
|
|---|
| 5 | Copyright (c) 2024, Robert Eisele (https://raw.org/)
|
|---|
| 6 | Licensed under the MIT license.
|
|---|
| 7 | */
|
|---|
| 8 | const Fraction = require('fraction.js');
|
|---|
| 9 |
|
|---|
| 10 | // Based on http://www.maths.surrey.ac.uk/hosted-sites/R.Knott/Fractions/egyptian.html
|
|---|
| 11 | function egyptian(a, b) {
|
|---|
| 12 |
|
|---|
| 13 | var res = [];
|
|---|
| 14 |
|
|---|
| 15 | do {
|
|---|
| 16 | var t = Math.ceil(b / a);
|
|---|
| 17 | var x = new Fraction(a, b).sub(1, t);
|
|---|
| 18 | res.push(t);
|
|---|
| 19 | a = Number(x.n);
|
|---|
| 20 | b = Number(x.d);
|
|---|
| 21 | } while (a !== 0n);
|
|---|
| 22 | return res;
|
|---|
| 23 | }
|
|---|
| 24 | console.log("1 / " + egyptian(521, 1050).join(" + 1 / "));
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.