詳解JavaScript中分解數(shù)字的三種方法
本文基于免費(fèi)代碼營基本算法腳本“分解數(shù)字”
在數(shù)學(xué)中,非負(fù)整數(shù)n的階乘可能是一個(gè)棘手的算法。在本文中,我將解釋這種方法,首先使用遞歸函數(shù),第二種使用而循環(huán),第三種使用以循環(huán)。
算法挑戰(zhàn)
返回提供的整體的階乘。
如果整體用字母n表示,則階乘是所有小于或等于n的正整數(shù)的乘積。
階乘經(jīng)常用簡寫符號(hào)n!表示!
例如:5!= 1 * 2 * 3 * 4 * 5 = 120
function factorialize(num) { return num; } factorialize(5);
提供的測試用例
- factorialize(0)應(yīng)該返回1
- factorialize(5)應(yīng)該返回120
- factorialize(10)應(yīng)該返回3628800
- factorialize(20)應(yīng)該返回2432902008176640000
什么是因數(shù)分解?
當(dāng)將一個(gè)因數(shù)分解時(shí),就是稱為數(shù)字乘以每個(gè)連續(xù)的數(shù)字減一個(gè)。
如果您的電話號(hào)碼是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.遞歸分解一個(gè)數(shù)字
function factorialize(num) { // If the number is less than 0, reject it. if (num < 0) return -1; // If the number is 0, its factorial is 1. else if (num == 0) return 1; // Otherwise, call the recursive procedure again else { return (num * factorialize(num - 1)); /* First Part of the recursion method You need to remember that you won't have just one call, you'll have several nested calls Each call: num === "?" num * factorialize(num - 1) 1st call – factorialize(5) will return 5 * factorialize(5 - 1) // factorialize(4) 2nd call – factorialize(4) will return 4 * factorialize(4 - 1) // factorialize(3) 3rd call – factorialize(3) will return 3 * factorialize(3 - 1) // factorialize(2) 4th call – factorialize(2) will return 2 * factorialize(2 - 1) // factorialize(1) 5th call – factorialize(1) will return 1 * factorialize(1 - 1) // factorialize(0) Second part of the recursion method The method hits the if condition, it returns 1 which num will multiply itself with The function will exit with the total value 5th call will return (5 * (5 - 1)) // num = 5 * 4 4th call will return (20 * (4 - 1)) // num = 20 * 3 3rd call will return (60 * (3 - 1)) // num = 60 * 2 2nd call will return (120 * (2 - 1)) // num = 120 * 1 1st call will return (120) // num = 120 If we sum up all the calls in one line, we have (5 * (5 - 1) * (4 - 1) * (3 - 1) * (2 - 1)) = 5 * 4 * 3 * 2 * 1 = 120 */ } } factorialize(5);
沒有注釋:
function factorialize(num) { if (num < 0) return -1; else if (num == 0) return 1; else { return (num * factorialize(num - 1)); } } factorialize(5);
2.用WHILE循環(huán)分解一個(gè)數(shù)字
function factorialize(num) { // Step 1. Create a variable result to store num var result = num; // If num = 0 OR num = 1, the factorial will return 1 if (num === 0 || num === 1) return 1; // Step 2. Create the WHILE loop while (num > 1) { num--; // decrementation by 1 at each iteration result = result * num; // or result *= num; /* num num-- var result result *= num 1st iteration: 5 4 5 20 = 5 * 4 2nd iteration: 4 3 20 60 = 20 * 3 3rd iteration: 3 2 60 120 = 60 * 2 4th iteration: 2 1 120 120 = 120 * 1 5th iteration: 1 0 120 End of the WHILE loop */ } // Step 3. Return the factorial of the provided integer return result; // 120 } factorialize(5);
沒有注釋:
function factorialize(num) { var result = num; if (num === 0 || num === 1) return 1; while (num > 1) { num--; result *= num; } return result; } factorialize(5);
3.使用FOR循環(huán)分解數(shù)字
function factorialize(num) { // If num = 0 OR num = 1, the factorial will return 1 if (num === 0 || num === 1) return 1; // We start the FOR loop with i = 4 // We decrement i after each iteration for (var i = num - 1; i >= 1; i--) { // We store the value of num at each iteration num = num * i; // or num *= i; /* num var i = num - 1 num *= i i-- i >= 1? 1st iteration: 5 4 = 5 - 1 20 = 5 * 4 3 yes 2nd iteration: 20 3 = 4 - 1 60 = 20 * 3 2 yes 3rd iteration: 60 2 = 3 - 1 120 = 60 * 2 1 yes 4th iteration: 120 1 = 2 - 1 120 = 120 * 1 0 no 5th iteration: 120 0 120 End of the FOR loop */ } return num; //120 } factorialize(5);
沒有注釋:
function factorialize(num) { if (num === 0 || num === 1) return 1; for (var i = num - 1; i >= 1; i--) { num *= i; } return num; } factorialize(5);
到此這篇關(guān)于詳解JavaScript中分解數(shù)字的三種方法的文章就介紹到這了,更多相關(guān)js分解數(shù)字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
JavaScript實(shí)現(xiàn)的使用鍵盤控制人物走動(dòng)實(shí)例
這篇文章主要介紹了JavaScript實(shí)現(xiàn)的使用鍵盤控制人物走動(dòng)實(shí)例,也可說是一個(gè)JS實(shí)現(xiàn)的小人走動(dòng)小游戲,需要的朋友可以參考下2014-08-08element-ui 圖片壓縮上傳功能實(shí)現(xiàn)
這篇文章主要介紹了element-ui 圖片壓縮上傳功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10JavaScript樹形結(jié)構(gòu)數(shù)組處理之遞歸問題
這篇文章主要介紹了JavaScript樹形結(jié)構(gòu)數(shù)組處理之遞歸問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06JavaScript事件學(xué)習(xí)小結(jié)(三)js事件對(duì)象
這篇文章主要介紹了JavaScript事件學(xué)習(xí)小結(jié)(三)js事件對(duì)象的相關(guān)資料,非常不錯(cuò)具有參考借鑒價(jià)值,需要的朋友可以參考下2016-06-06省市聯(lián)動(dòng)效果的簡單實(shí)現(xiàn)代碼(推薦)
下面小編就為大家?guī)硪黄∈新?lián)動(dòng)效果的簡單實(shí)現(xiàn)代碼(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-06-06JavaScript實(shí)現(xiàn)隱藏省略文字效果的方法
這篇文章主要介紹了JavaScript實(shí)現(xiàn)隱藏省略文字效果的方法,涉及javascript基于事件響應(yīng)實(shí)現(xiàn)頁面字符串元素的獲取、截取、設(shè)置等相關(guān)操作技巧,需要的朋友可以參考下2017-04-04JS簡單編號(hào)生成器實(shí)現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了JS簡單編號(hào)生成器實(shí)現(xiàn)方法,涉及JavaScript針對(duì)表單與字符串操作的相關(guān)技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2016-04-04關(guān)于 byval 與 byref 的區(qū)別分析總結(jié)
關(guān)于 byval 與 byref 的區(qū)別分析總結(jié)...2007-10-10