You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/*
* Converse a fraction to a continued fraction
* @param {Number} a - numerator
* @param {Number} b - denominator
* @param {Number} limit - max number of elements returned.
* @return {Array} array of numbers
*/
var fractionToContinuedFraction = function (a, b, limit) {
limit = limit || 1e3;
var arr = [];
a = +a;
b = +b;
while (0 < limit-- && a && b && (a !== 1 || a / b !== 1)) {
arr.push( Math.floor( a/b ) );
a %= b;
tmp = b;
b = a;
a = tmp;
}
return arr;
}
Bug:
Add condition for 0 and Infinity.
Does the opposite.
var continuedFractionToFraction = function (arr) {
if(typeof arr !== "object"){
return null;
}
var a = 1,
b = 1,
tmp,
i = arr.length;
while(i--){
a += b * arr[i];
tmp = b;
b = a;
a = tmp;
}
return [a, b];
};
Need to add unit tests. There a bug in the code but will check it out later.
The text was updated successfully, but these errors were encountered:
Source:
Bug:
Does the opposite.
Need to add unit tests. There a bug in the code but will check it out later.
The text was updated successfully, but these errors were encountered: