From 1d78de3629336d99db6b0af5f2edc1d27daad465 Mon Sep 17 00:00:00 2001
From: Swapnil Tripathi
Date: Thu, 1 Oct 2020 09:11:54 +0530
Subject: [PATCH 1/5] Added new Javascript Algorithm
---
Factorial_Of_Number/factorial.js | 33 ++++++++++++++++++++++++++++++++
1 file changed, 33 insertions(+)
create mode 100644 Factorial_Of_Number/factorial.js
diff --git a/Factorial_Of_Number/factorial.js b/Factorial_Of_Number/factorial.js
new file mode 100644
index 0000000..d12ab68
--- /dev/null
+++ b/Factorial_Of_Number/factorial.js
@@ -0,0 +1,33 @@
+
+//1. Using For Loop.
+
+function factorial(n){
+ let answer = 1;
+ if (n == 0 || n == 1){
+ return answer;
+ }else{
+ for(var i = n; i >= 1; i--){
+ answer = answer * i;
+ }
+ return answer;
+ }
+ }
+ let n = 4;
+ answer = factorial(n)
+ console.log("The factorial of " + n + " is " + answer);
+
+
+//2. Using Recursion.
+
+function factorial(n){
+ //base case
+ if(n == 0 || n == 1){
+ return 1;
+ //recursive case
+ }else{
+ return n * factorial(n-1);
+ }
+}
+let n = 4;
+answer = factorial(n)
+console.log("The factorial of " + n + " is " + answer);
\ No newline at end of file
From 46c16c1964aef869fc0cdc2a2a71b142395733de Mon Sep 17 00:00:00 2001
From: Swapnil Tripathi
Date: Thu, 1 Oct 2020 09:46:26 +0530
Subject: [PATCH 2/5] Added a new Javascript Algorithm
---
.../factorial.js | 0
README.md | 57 +++++++++++++++++--
2 files changed, 53 insertions(+), 4 deletions(-)
rename {Factorial_Of_Number => Factorial_Of_Number_6}/factorial.js (100%)
diff --git a/Factorial_Of_Number/factorial.js b/Factorial_Of_Number_6/factorial.js
similarity index 100%
rename from Factorial_Of_Number/factorial.js
rename to Factorial_Of_Number_6/factorial.js
diff --git a/README.md b/README.md
index 13d142d..4eaae31 100644
--- a/README.md
+++ b/README.md
@@ -447,15 +447,64 @@ method tests whether all elements pass the test or not which is implemented by p
-6. Name
+6. Factorial of a number
-__The challenge:__
+The Factorial of a non-negative number ,n , is computed as the product of all integers between 1 and n (both inclusive).
+ factorial(n) = n * (n-1) * ..... * 1
+ It can be thought of as:
+ factorial(n) = n * factorial(n-1).
+__The challenge:__ Given a number, print the factorial of this number
-__Algorithmic Thinking:__
+```js
+ answer = factorial(n) // will return the factorial.
+```
+__Algorithmic Thinking:__ Approaches
+There are two ways to compute the factorial of a number in JavaScript.
-__code Implementation:__
+1. Iterative
+2. Recursive
+Both of these approaches will be explored below.
+
+
+__code Implementation:__ 1. The Iterative approach
+Keeping in mind the first definition of a factorial, the variable i is initially set equal to n and is gradually decremented to 1. In each step, the result of the multiplication is stored in the variable answer.
+
+```js
+ function factorial(n){
+ let answer = 1;
+ if (n == 0 || n == 1){
+ return answer;
+ }else{
+ for(var i = n; i >= 1; i--){
+ answer = answer * i;
+ }
+ return answer;
+ }
+ }
+ let n = 4;
+ answer = factorial(n)
+ console.log("The factorial of " + n + " is " + answer);
+```
+
+2. The Recursive approach
+As stated above, the factorial of n can be found by finding the factorial of a number one less than n, and then multiplying this answer with n. So the factorial of n-1 can be thought of as a subproblem that needs to be computed first.
+
+```js
+ function factorial(n){
+ //base case
+ if(n == 0 || n == 1){
+ return 1;
+ //recursive case
+ }else{
+ return n * factorial(n-1);
+ }
+ }
+ let n = 4;
+ answer = factorial(n)
+ console.log("The factorial of " + n + " is " + answer);
+```
From 6cb32ce978f2785bd6a9752dbe85632a8011fde5 Mon Sep 17 00:00:00 2001
From: Swapnil Tripathi
Date: Thu, 1 Oct 2020 09:53:23 +0530
Subject: [PATCH 3/5] Added a Factorial Algorithm.
---
README.md | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/README.md b/README.md
index 4eaae31..f780892 100644
--- a/README.md
+++ b/README.md
@@ -449,12 +449,12 @@ method tests whether all elements pass the test or not which is implemented by p
6. Factorial of a number
-The Factorial of a non-negative number ,n , is computed as the product of all integers between 1 and n (both inclusive).
- factorial(n) = n * (n-1) * ..... * 1
+
The Factorial of a non-negative number ,n , is computed as the product of all integers between 1 and n (both inclusive).
+ * factorial(n) = n * (n-1) * ..... * 1
It can be thought of as:
- factorial(n) = n * factorial(n-1).
+ * factorial(n) = n * factorial(n-1).
-__The challenge:__ Given a number, print the factorial of this number
+__The challenge:__ Given a number, print the factorial of this number
```js
answer = factorial(n) // will return the factorial.
@@ -464,12 +464,12 @@ __Algorithmic Thinking:__ Approaches
There are two ways to compute the factorial of a number in JavaScript.
1. Iterative
-2. Recursive
+2. Recursive
Both of these approaches will be explored below.
-__code Implementation:__ 1. The Iterative approach
-Keeping in mind the first definition of a factorial, the variable i is initially set equal to n and is gradually decremented to 1. In each step, the result of the multiplication is stored in the variable answer.
+__code Implementation:__ 1. The Iterative approach
+Keeping in mind the first definition of a factorial, the variable i is initially set equal to n and is gradually decremented to 1. In each step, the result of the multiplication is stored in the variable answer.
```js
function factorial(n){
@@ -487,9 +487,9 @@ Keeping in mind the first definition of a factorial, the variable i is initia
answer = factorial(n)
console.log("The factorial of " + n + " is " + answer);
```
-
-2. The Recursive approach
-As stated above, the factorial of n can be found by finding the factorial of a number one less than n, and then multiplying this answer with n. So the factorial of n-1 can be thought of as a subproblem that needs to be computed first.
+
+2. The Recursive approach
+As stated above, the factorial of n can be found by finding the factorial of a number one less than n, and then multiplying this answer with n. So the factorial of n-1 can be thought of as a subproblem that needs to be computed first.
```js
function factorial(n){
@@ -505,6 +505,7 @@ As stated above, the factorial of n can be found by finding the factorial of a n
answer = factorial(n)
console.log("The factorial of " + n + " is " + answer);
```
+
From e767535b9257c2ef1d4c43d56cf4a4d40cc6fcb9 Mon Sep 17 00:00:00 2001
From: Swapnil Tripathi
Date: Thu, 1 Oct 2020 09:57:36 +0530
Subject: [PATCH 4/5] Added a new Factorial Algorithm
---
README.md | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/README.md b/README.md
index f780892..3f32012 100644
--- a/README.md
+++ b/README.md
@@ -454,7 +454,7 @@ method tests whether all elements pass the test or not which is implemented by p
It can be thought of as:
* factorial(n) = n * factorial(n-1).
-__The challenge:__ Given a number, print the factorial of this number
+__The challenge:__ Given a number, print the factorial of this number
```js
answer = factorial(n) // will return the factorial.
From 5aa9b94300f5d934df62abcd9bb3a70836bb5e9d Mon Sep 17 00:00:00 2001
From: Swapnil Tripathi
Date: Tue, 6 Oct 2020 07:39:53 +0530
Subject: [PATCH 5/5] Added new Algorithm
---
{Factorial_Of_Number_6 => Factorial_6}/factorial.js | 0
README.md | 1 +
2 files changed, 1 insertion(+)
rename {Factorial_Of_Number_6 => Factorial_6}/factorial.js (100%)
diff --git a/Factorial_Of_Number_6/factorial.js b/Factorial_6/factorial.js
similarity index 100%
rename from Factorial_Of_Number_6/factorial.js
rename to Factorial_6/factorial.js
diff --git a/README.md b/README.md
index 3f32012..f1c669f 100644
--- a/README.md
+++ b/README.md
@@ -77,6 +77,7 @@ __Algorithms practiced using JS__
3. Finding the Most Recurring Character
4. Sentence Capitalization
5. Palindromes
+6. Factorial of a number
## Explanation
1. String reversing