Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a new Javascript Algorithm for Factorial of a number. #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Factorial_6/factorial.js
Original file line number Diff line number Diff line change
@@ -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);
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<b>1. String reversing</b>
Expand Down Expand Up @@ -447,15 +448,65 @@ method tests whether all elements pass the test or not which is implemented by p
<hr>
<hr>

<b>6. Name </b>
<b>6. Factorial of a number </b>

__The challenge:__ <p> </p>
<p>The Factorial of a non-negative number ,n , is computed as the product of all integers between 1 and n (both inclusive).<br><br>
* factorial(n) = n * (n-1) * ..... * 1<br>
It can be thought of as:<br>
* factorial(n) = n * factorial(n-1).</p> <br>

__The challenge:__ <p>Given a number, print the factorial of this number </p>

__Algorithmic Thinking:__ <p> </p>
```js
answer = factorial(n) // will return the factorial.
```

__Algorithmic Thinking:__ <p>Approaches <br>
There are two ways to compute the factorial of a number in JavaScript.<br><br>

__code Implementation:__ <p> </p>
1. Iterative<br>
2. Recursive<br><br>
Both of these approaches will be explored below. </p>


__code Implementation:__ <p><strong>1. The Iterative approach</strong><br><br>
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. </p><br>

```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);
```
<br>
<p><strong>2. The Recursive approach</strong><br><br>
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. </p><br>

```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);
```
<br>

<hr>
<hr>
Expand Down