function factorial(n) { let output = 1; for (let i = n; i >= 1; i--) { output *= i; } return output; } console.log(factorial(0)); //1 console.log(factorial(1)); //1 console.log(factorial(4)); //24 console.log(factorial(6)); //720
@YakovFn7 ай бұрын
I did it the same
@paulbird27722 жыл бұрын
I really like recursive approach for this: const factorial = (n) => { return n
@isaacdruin2 жыл бұрын
For this, you still have to write the factorial function as well, no? Same O and this would be less intuitive imo.
@paulbird27722 жыл бұрын
@@isaacdruin Nothing extra needed, that is the function. If you never seen recursion before, then I agree it is less intuitive than a for loop. I highly recommend pasting my one liner into a browser console and trying it out. It is the best example to learn what recursion is because it is so simple.
@manjunatha5a2 жыл бұрын
I think he is keeping recursion approach for later.
@YakovFn7 ай бұрын
Yea, me too! In maths you can say 5! = 5*4! = 5*4*3!...
@SuperYoman1006 ай бұрын
That'd be O(2^n) TC. Much worse.
@mohamedothman36372 жыл бұрын
function fib(n){ let num = 1 for(let i =0; i
@miladcodes2 жыл бұрын
Here is another solution const factorial = (n) => { let factorial = 1 for (let i = n; i > 1; i--) { factorial *= i } return factorial }
@norineoo6417 Жыл бұрын
This is my solution: function factorial(n) { let result = 1 for (let i=0; i
@akaris80422 жыл бұрын
Using recursion makes more sense to me function factorial(nnum){ //base case if(num == 0 || num == 1){ return 1; //recursive case }else{ return num * factorial(num - 1); } } / console.log(factorial(5)) //120 console.log(factorial(4)) //24 console.log(factorial(6)) //720
@natasdabsi11386 ай бұрын
fixed, function factorial(num){ //base case if(num == 0 || num == 1){ return 1; //recursive case }else{ return num * factorial(num - 1); } } console.log(factorial(5)) //120 console.log(factorial(4)) //24 console.log(factorial(6)) //720
@srudsalam6970 Жыл бұрын
// using loop function factorial(n) { let fac = 1; for(let i=2; i < n; i++) { fac *= i; } return fac; } // using recursive function factorial(n) { if (n === 0) return 1; return n * factorial(--n); } console.log('0: ', factorial(0)); console.log('1: ', factorial(1)); console.log('2: ', factorial(2)); console.log('3: ', factorial(3)); console.log('4: ', factorial(4)); console.log('5: ', factorial(5));
@DutchmanRadio Жыл бұрын
I also wanted to write a for loop despite the comments here, I enjoyed this exercise and the explanation a lot
@prachi_aroraАй бұрын
Thank you so much🙏
@DantesSagan2 жыл бұрын
Thanks, I've got a simple solution with for loop: function factorial(n){ let result = 1; for(let i = n; i > 1; i--){ if(n === 0){ return result; } else { result *= i } } return result } let result = factorial(0); let resultTwo = factorial(4); let resultThree = factorial(5); console.log(result, resultTwo, resultThree);
@narasimhakamath38082 жыл бұрын
Why do you want to check for n === 0 in every iteration? You can return 1 if n === 0 || n === 1 at the top of the function itself.
@podcasttakes6 ай бұрын
function factorial(n) { if (n === 0 || n === 1) { return 1; } let result = 1; // Initialize result to 1 for (let i = 2; i
@sadhruva3848 Жыл бұрын
function fact(n) { let result = 1; while (n >= 1) { res *= n; n - - } return result }
@bayasahamed18157 ай бұрын
function factorial(num) { let factor = 1; for (let i = num; i > 1; i--) { factor = factor * i; } return factor; }
@AdarshSingh-mm9lh2 жыл бұрын
I gone through this way... function factorial(n) { let result = n; if (!result) return 1; for (let i = n; i > 1; i--) { result *= (i - 1); } return result; }
@natasdabsi11386 ай бұрын
function factorial(n) { var y = []; var x = 1; for (var i = 1; i
@waleedcodes14222 жыл бұрын
I did this solution for the algorithm: function factorial(number) { let result = 1 while(number > 1) { result *= number number-- } return result } console.log(factorial(5))
@armoredchimp Жыл бұрын
I have zero experience with data structures/algorithms after the past 9 months of self-studying programming and cloud computing. Just started learning these and I am not good at them! Managed to get an almost-correct answer for this and fibonnaci on my own, but both were not quite correct. I'm going to do 1-2 of these at minimum every day until I get much better.
@rohitrout64502 жыл бұрын
solve it when contraints are n
@premsingh6967 Жыл бұрын
@dsuppal90599 ай бұрын
In your case i think if you pass 0 or 1 then code won't goes into the loop. It directly goes to the return statement; So Big O notation for the same will be O(1)
@YakovFn7 ай бұрын
Big O means the worst case scenario, you are talking about the best
@Ayoubmajid-uu9yv10 ай бұрын
// solve the problem of factorial : function factorial(n) { if (n == 0 || n == 0) return 1; let fact = 1; for (let i = 2; i
@NadirAli-kn7sl2 жыл бұрын
function factorial(n) { let result = 1 if(n!==0){ for(let i=n;i>=1;i--){ result*=i } } return result }
@vaibhavsharma4743 Жыл бұрын
let factorial = (n)=>{ let result = 1; for(let i=2;i
@simdanofficial4 ай бұрын
function factorial(n: number) { let result = 1; while (n > 0) { result = result * n; n--; } return result; }
@alcidesJoyableАй бұрын
loved it! I got lost at last part - Big-O?
@vaibhavsharma4743 Жыл бұрын
const factorial = (n) => { return n
@ronaldomaia7 ай бұрын
function Factorial(n) { if (n === 0) return 1; return n * Factorial(n - 1); }
// Normal approach function factorial(number){ let fact = 1; for(let i =number;i>0;i--){ fact= fact*=i; } return fact; } console.log(factorial(4)) // Recursive approach function factorial(number){ if(number === 0){ return 1; } return number * factorial(number-1); } console.log(factorial(5)) 🙂
@legdayenjoyer6000 Жыл бұрын
const factorial = (n) => { let fact = 1 for (let i = 0; i
@badbrother_20232 жыл бұрын
If you replace the multiplication sign with an addition sign and n becomes 3 you will get 6 but the math begs to differ as you will get 7
@shadowmixer2 жыл бұрын
const factorial = n => { fac = 1; for (let i = n; i > 0; i--) { fac *= i; } return fac; }
@grishaghlijyan233310 ай бұрын
@Codevolution - as we are starting the iteration from 2, isn't the complexity of this algorithm O(n-1) instead of O(n)?
@abhayjeetsingh42116 ай бұрын
Constants can be neglected
@jerbparagas392410 ай бұрын
Here is my implementation with while & recursion // recursion const fact = ( n : number ) : number => { return n === 0 ? 1 : n * fact(n - 1) } // while loop - i found while loop much simpler const factLoop = ( n : number ) : number => { let result = 1; while(n > 1) result *= n-- return result; }
@jmeahra2 жыл бұрын
amazing keep uploading, one question are you going to use recusion again after completion of this math algos?
@shujaatali841410 ай бұрын
I have start from 1 what is the disadvantage of i = 1 instead of 2?
@wedho_lifeInquisitor3 ай бұрын
why is i always equal to 2? what is i's relationship with n?
@projectdevelopers9639 Жыл бұрын
var a =0 var factorial =1 while (a > 1) { factorial= factorial * a a-- }
@jericmoreno7088 Жыл бұрын
my answer const Factorial = (n) => { let total = 1; for(let i = n; i > 0; i--){ total *= i } return total } console.log(Factorial(4)) console.log(Factorial(5))
@Mark-iv5dl2 жыл бұрын
function factorialNumber(n) { let sum = n; if (n === 0) { return 1; } for (i = n - 1; i >= 1; i--) { sum *= i; } return sum; }
@MythicEcho2 жыл бұрын
My solution function getFactorial(n){ let result=1; if(n===0){ return 1; }else if(n1; i--){ result = result*i; } } return result; }
@Lafsha Жыл бұрын
const fact=num=>num==0?1:num*fact(num-1)
@kamilturek37882 жыл бұрын
Hello. If I add if checking if n === 0 and else for other values of n's, and if I set the argument to 0, is the complexity O(1)? let factorial = function (n) { let total = 1; if (n === 0) { return 1; } else { for (let i = n; i > 0; i--) { total *= i; } } return total; };
@ishantkushwaha5342 жыл бұрын
Yes
@c.5879 Жыл бұрын
function getFactorial(n:number){ let factorial = 1 for(let i=1; i
@dsuppal90599 ай бұрын
Here is my code function factorial(n){ let i=n; let value = 1; while(i!=0){ value *= i i-- } return value } console.log(factorial(1)) // Bog O = O(n)