详解JavaScript中分解数字的三种方法
本文基于免费代码营基本算法脚本“分解数字”
在数学中,非负整数n的阶乘可能是一个棘手的算法。在本文中,我将解释这种方法,首先使用递归函数,第二种使用而循环,第三种使用以循环。
算法挑战
返回提供的整体的阶乘。
如果整体用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。
阶乘经常用简写符号n!表示!
例如:5!=1*2*3*4*5=120
functionfactorialize(num){ returnnum; } factorialize(5);
提供的测试用例
- factorialize(0)应该返回1
- factorialize(5)应该返回120
- factorialize(10)应该返回3628800
- factorialize(20)应该返回2432902008176640000
什么是因数分解?
当将一个因数分解时,就是称为数字乘以每个连续的数字减一个。
如果您的电话号码是5,则您将:
5!=5*4*3*2*1
该模式为:
0!=1
1!=1
2!=2*1
3!=3*2*1
4!=4*3*2*1
5!=5*4*3*2*1
1.递归分解一个数字
functionfactorialize(num){ //Ifthenumberislessthan0,rejectit. if(num<0) return-1; //Ifthenumberis0,itsfactorialis1. elseif(num==0) return1; //Otherwise,calltherecursiveprocedureagain else{ return(num*factorialize(num-1)); /* FirstPartoftherecursionmethod Youneedtorememberthatyouwon'thavejustonecall,you'llhaveseveralnestedcalls Eachcall:num==="?"num*factorialize(num-1) 1stcall–factorialize(5)willreturn5*factorialize(5-1)//factorialize(4) 2ndcall–factorialize(4)willreturn4*factorialize(4-1)//factorialize(3) 3rdcall–factorialize(3)willreturn3*factorialize(3-1)//factorialize(2) 4thcall–factorialize(2)willreturn2*factorialize(2-1)//factorialize(1) 5thcall–factorialize(1)willreturn1*factorialize(1-1)//factorialize(0) Secondpartoftherecursionmethod Themethodhitstheifcondition,itreturns1whichnumwillmultiplyitselfwith Thefunctionwillexitwiththetotalvalue 5thcallwillreturn(5*(5-1))//num=5*4 4thcallwillreturn(20*(4-1))//num=20*3 3rdcallwillreturn(60*(3-1))//num=60*2 2ndcallwillreturn(120*(2-1))//num=120*1 1stcallwillreturn(120)//num=120 Ifwesumupallthecallsinoneline,wehave (5*(5-1)*(4-1)*(3-1)*(2-1))=5*4*3*2*1=120 */ } } factorialize(5);
没有注释:
functionfactorialize(num){ if(num<0) return-1; elseif(num==0) return1; else{ return(num*factorialize(num-1)); } } factorialize(5);
2.用WHILE循环分解一个数字
functionfactorialize(num){ //Step1.Createavariableresulttostorenum varresult=num; //Ifnum=0ORnum=1,thefactorialwillreturn1 if(num===0||num===1) return1; //Step2.CreatetheWHILEloop while(num>1){ num--;//decrementationby1ateachiteration result=result*num;//orresult*=num; /* numnum--varresultresult*=num 1stiteration:54520=5*4 2nditeration:432060=20*3 3rditeration:3260120=60*2 4thiteration:21120120=120*1 5thiteration:10120 EndoftheWHILEloop */ } //Step3.Returnthefactorialoftheprovidedinteger returnresult;//120 } factorialize(5);
没有注释:
functionfactorialize(num){ varresult=num; if(num===0||num===1) return1; while(num>1){ num--; result*=num; } returnresult; } factorialize(5);
3.使用FOR循环分解数字
functionfactorialize(num){ //Ifnum=0ORnum=1,thefactorialwillreturn1 if(num===0||num===1) return1; //WestarttheFORloopwithi=4 //Wedecrementiaftereachiteration for(vari=num-1;i>=1;i--){ //Westorethevalueofnumateachiteration num=num*i;//ornum*=i; /* numvari=num-1num*=ii--i>=1? 1stiteration:54=5-120=5*43yes 2nditeration:203=4-160=20*32yes 3rditeration:602=3-1120=60*21yes 4thiteration:1201=2-1120=120*10no 5thiteration:1200120 EndoftheFORloop */ } returnnum;//120 } factorialize(5);
没有注释:
functionfactorialize(num){ if(num===0||num===1) return1; for(vari=num-1;i>=1;i--){ num*=i; } returnnum; } factorialize(5);
到此这篇关于详解JavaScript中分解数字的三种方法的文章就介绍到这了,更多相关js分解数字内容请搜索毛票票以前的文章或继续浏览下面的相关文章希望大家以后多多支持毛票票!