-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfuscFunction.js
More file actions
53 lines (45 loc) · 1.14 KB
/
Copy pathfuscFunction.js
File metadata and controls
53 lines (45 loc) · 1.14 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
https://en.wikipedia.org/wiki/Calkin%E2%80%93Wilf_tree
function fusc(n) {
}
generateLeftNode = function(parentNode) {
return { a : parentNode.a , b: (parentNode.a + parentNode.b) };
}
generateRightNode = function(parentNode) {
return { a : (parentNode.a + parentNode.b) , b : parentNode.b }
}
calkinWilfTree = function() {
return {
tree : [[{a : 1, b: 1}]],
next : function() {
var treeLevel = [];
var lastParent = this.tree[this.tree.length - 1];
lastParent.forEach(function(p) {
treeLevel.push(generateLeftNode(p),generateRightNode(p));
});
this.tree.push(treeLevel)
},
sternSeries : function() {
stern = [];
this.tree.forEach(function(b) {
var sum = 0;
b.forEach(function(n) {
sum += n.a / n.b;
});
stern.push(sum);
});
return stern;
}
}
}
generateCalkinWilfTreeTo = function(n) {
var tree = [{a : 1, b: 1}];
for(var i = 0; i < n; i++) {
tree.push(generateTreeLevel(tree))
}
return tree;
}
var calkinWilf = new calkinWilfTree();
calkinWilf.next();
calkinWilf.next();
calkinWilf.next();
console.log(calkinWilf.tree);