This commit is contained in:
赵鑫 2023-02-20 22:40:56 +08:00
parent 912ec4a639
commit d1862b5dc2
2 changed files with 27 additions and 9 deletions

27
古埃及分数.js Normal file
View File

@ -0,0 +1,27 @@
const process = require("process");
const 求最大公约数 = (a, b) => (b ? 求最大公约数(b, a % b) : a);
const 求最小公倍数 = (a, b) => (a * b) / 求最大公约数(a, b);
function 求约分数(分子, 分母) {
const 最大公约数 = 求最大公约数(分子, 分母);
return [分子 / 最大公约数, 分母 / 最大公约数];
}
function 求分数的差(a, b, c, d) {
let 分子 = a * d - c * b;
let 分母 = b * d;
return 求约分数(分子, 分母);
}
let [分子, 分母] = 求约分数(process.argv[2], process.argv[3]);
分子 = 分子 % 分母;
const 埃及分数 = [];
const [a, b] = [分子, 分母];
while (分子 != 1) {
let n = Math.floor(分母 / 分子) + 1;
埃及分数.push(`1/${n}`);
[分子, 分母] = 求分数的差(分子, 分母, 1, n);
}
埃及分数.push(`1/${分母}`);
console.log(`${a}/${b} = ${埃及分数.join(" + ")}`);

View File

@ -1,9 +0,0 @@
const process = require("process");
let a = process.argv[2];
let b = process.argv[3];
const gcd = (a, b) => (b ? gcd(b, a % b) : a);
const scm = (a, b) => (a * b) / gcd(a, b);
console.log(gcd(a, b));