Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1f65251

Browse files
frankymacster64json
authored andcommitted
Add Simple Recursion/Nth Factorial (#19)
1 parent 145cc57 commit 1f65251

File tree

2 files changed

+50
-0
lines changed

2 files changed

+50
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# nth Factorial
2+
Finding nth Factorial using recursion.
3+
4+
## Complexity
5+
* **Time**: ![](https://latex.codecogs.com/svg.latex?O(2n))
6+
* **Space**: ![](https://latex.codecogs.com/svg.latex?O(2n))
7+
8+
## References
9+
* [codeburst.io](https://codeburst.io/learn-and-understand-recursion-in-javascript-b588218e87ea)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// import visualization libraries {
2+
const { Tracer, Array1DTracer, Layout, VerticalLayout } = require('algorithm-visualizer');
3+
// }
4+
5+
// define tracer variables {
6+
var tracer = new Array1DTracer('Sequence');
7+
Layout.setRoot(new VerticalLayout([tracer]));
8+
var index = 15;
9+
var D = [1];
10+
for (var i = 1; i < index; i++) {
11+
D.push(0);
12+
}
13+
tracer.set(D);
14+
Tracer.delay();
15+
// }
16+
17+
function fact(num) {
18+
if (num < 0) {
19+
return;
20+
}
21+
22+
if (num === 0) {
23+
return 1;
24+
}
25+
26+
var res = num * fact(num - 1);
27+
28+
D[num - 1] = res;
29+
30+
// visualize {
31+
tracer.select(num - 1);
32+
Tracer.delay();
33+
tracer.patch(num - 1, D[num - 1]);
34+
Tracer.delay();
35+
tracer.depatch(num - 1);
36+
tracer.deselect(num - 1);
37+
// }
38+
39+
return res;
40+
}
41+
fact(index);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /