-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathproblem_252.js
More file actions
36 lines (32 loc) Β· 1021 Bytes
/
problem_252.js
File metadata and controls
36 lines (32 loc) Β· 1021 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/**
* Turn ordinary fractions into Egyptian fraction (a < b)
* @param {string} exp - string representation of a fraction
*/
function printEgyptianNum(exp) {
let [numerator, denominator] = exp.split('/');
const denomList = [];
while (numerator !== 0) {
const x = Math.ceil(denominator / numerator);
denomList.push(x);
numerator = x * numerator - denominator;
denominator *= x;
}
return formStr(denomList);
}
/**
* Helper function to form Egyptian fraction
* @param {string[]} list
*/
function formStr(list) {
let str = '';
for (let i = 0; i < list.length - 1; i++) {
str += `1 / ${list[i]} + `;
}
str += `1 / ${list[list.length - 1]}`;
return str;
}
console.log(printEgyptianNum('1 / 2')); // 1 / 2
console.log(printEgyptianNum('4 / 13')); // 1 / 4 + 1 / 18 + 1 / 468
console.log(printEgyptianNum('6 / 14')); // 1 / 3 + 1 / 11 + 1 / 231
console.log(printEgyptianNum('2 / 3')); // 1 / 2 + 1 / 6
console.log(printEgyptianNum('12 / 13')); // 1 / 2 + 1 / 3 + 1 / 12 + 1 / 156