Skip to content

Commit 87dfb1b

Browse files
committed
added continuing fractions
1 parent f539c29 commit 87dfb1b

File tree

4 files changed

+57
-4
lines changed

4 files changed

+57
-4
lines changed

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ exports.inefficient = require('./modules/sorts/inefficient.js');
55
exports.simple = require('./modules/sorts/simple.js');
66
exports.other = require('./modules/sorts/other.js');
77
exports.Stack = require('./modules/data-types/stack.js');
8-
exports.isPrime = require('./modules/algorithms/is-prime.js');
8+
exports.isPrime = require('./modules/algorithms/is-prime.js');
9+
exports.continuingFraction = require('./modules/algorithms/continuing-fraction.js');
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
var decimal = z =>{
3+
var d = 0
4+
while(z.length){
5+
d=1/(d+z.pop())
6+
}
7+
return d;
8+
}
9+
10+
var reducedFraction = z => {
11+
var d = decimal(z);
12+
var e = 1.0E-15
13+
var h, f, j, k, l, m, a, x
14+
x = d
15+
a = ~~x
16+
f = 1
17+
l = 0
18+
h = a
19+
k = 1
20+
while (x-a > e*k*k) {
21+
x = 1/(x-a)
22+
a = ~~x
23+
j = f
24+
f = h
25+
m = l
26+
l = k
27+
h = j + a*f
28+
k = m + a*l
29+
}
30+
return h+'/'+k
31+
}
32+
33+
var product = z => {
34+
var rf = reducedFraction(z);
35+
rf = rf.split('/');
36+
return rf[0] * rf[1];
37+
}
38+
39+
module.exports = {
40+
'product': product,
41+
'reducedFraction': reducedFraction,
42+
'decimal': decimal
43+
};

modules/algorithms/readme.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,10 @@ Various algorithms developed for different purposes. Some are standard, some are
77
* isPrime(number) ![status](https://img.shields.io/badge/status-completed-brightgreen.svg)
88
* *This checks to see if the number passed to it is prime.*
99

10+
* continuingFraction ![status](https://img.shields.io/badge/status-completed-brightgreen.svg)
11+
* decimal
12+
* *This takes an array of integers and returns the results of running them as a continuing fraction. Returns as decimal number.*
13+
* reducedFraction
14+
* *This takes an array of integers and returns the results of running them as a continuing fraction. Returns as a reduced fraction number.*
15+
* product
16+
* *This takes an array of integers and returns the results of running them as a continuing fraction. Returns the product of the numerator and the denominator of the reduced fraction.*

test/simpletest.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ var x = [1,5,7,2,43,6,26,7,2356,7,346,1,42,0];
2121
// console.log(theStack.pop());
2222
// console.log(theStack.pop());
2323

24-
for(var i=0; i < x.length; i++){
25-
console.log(dt.isPrime(x[i]), x[i]);
26-
}
24+
// for(var i=0; i < x.length; i++){
25+
// console.log(dt.isPrime(x[i]), x[i]);
26+
// }
27+
28+
//console.log(dt.continuingFraction.decimal(x));

0 commit comments

Comments
 (0)