Movatterモバイル変換


[0]ホーム

URL:


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

Commit1f65251

Browse files
frankymacster64json
authored andcommitted
Add Simple Recursion/Nth Factorial (#19)
1 parent145cc57 commit1f65251

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(2ⁿ))
6+
***Space**:![](https://latex.codecogs.com/svg.latex?O(2ⁿ))
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+
vartracer=newArray1DTracer('Sequence');
7+
Layout.setRoot(newVerticalLayout([tracer]));
8+
varindex=15;
9+
varD=[1];
10+
for(vari=1;i<index;i++){
11+
D.push(0);
12+
}
13+
tracer.set(D);
14+
Tracer.delay();
15+
// }
16+
17+
functionfact(num){
18+
if(num<0){
19+
return;
20+
}
21+
22+
if(num===0){
23+
return1;
24+
}
25+
26+
varres=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+
returnres;
40+
}
41+
fact(index);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp