详解JavaScript中分解数字的三种方法


Posted in Javascript onJanuary 05, 2021

本文基于免费代码营基本算法脚本“分解数字”

在数学中,非负整数n的阶乘可能是一个棘手的算法。在本文中,我将解释这种方法,首先使用递归函数,第二种使用而循环,第三种使用以循环。

算法挑战

返回提供的整体的阶乘。

如果整体用字母n表示,则阶乘是所有小于或等于n的正整数的乘积。

阶乘经常用简写符号n!表示!

例如:5!= 1 * 2 * 3 * 4 * 5 = 120

function factorialize(num) {
 return num;
}
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.递归分解一个数字

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循环分解一个数字

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循环分解数字

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

到此这篇关于详解JavaScript中分解数字的三种方法的文章就介绍到这了,更多相关js分解数字内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!

Javascript 相关文章推荐
JavaScript 关键字屏蔽实现函数
Aug 02 Javascript
JS 的应用开发初探(mootools)
Dec 19 Javascript
JQuery live函数
Dec 24 Javascript
jquery attr 设定src中含有&amp;(宏)符号问题的解决方法
Jul 26 Javascript
javascript实现单击和双击并存的方法
Dec 13 Javascript
javascript入门之string对象【新手必看】
Nov 22 Javascript
浅谈Vue.js
Mar 02 Javascript
15分钟深入了解JS继承分类、原理与用法
Jan 19 Javascript
elementUI select组件使用及注意事项详解
May 29 Javascript
开源一个微信小程序仪表盘组件过程解析
Jul 30 Javascript
利用PHP实现递归删除链表元素的方法示例
Oct 23 Javascript
js实现Element中input组件的部分功能并封装成组件(实例代码)
Mar 02 Javascript
Vue+scss白天和夜间模式切换功能的实现方法
Jan 05 #Vue.js
mapboxgl实现带箭头轨迹线的代码
Jan 04 #Javascript
jenkins自动构建发布vue项目的方法步骤
Jan 04 #Vue.js
vue3弹出层V3Popup实例详解
Jan 04 #Vue.js
vue3自定义dialog、modal组件的方法
Jan 04 #Vue.js
three.js显示中文字体与tween应用详析
Jan 04 #Javascript
js用正则表达式筛选年月日的实例方法
Jan 04 #Javascript
You might like
写php分页时出现的Fatal error的解决方法
2011/04/18 PHP
PHP封装的字符串加密解密函数
2015/12/18 PHP
phpstorm 正则匹配删除空行、注释行(替换注释行为空行)
2018/01/21 PHP
用javascript实现点击链接弹出&quot;图片另存为&quot;而不是直接打开
2007/08/15 Javascript
jquery click([data],fn)使用方法实例介绍
2013/07/08 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
兼容主流浏览器的JS复制内容到剪贴板
2014/12/12 Javascript
js使用Array.prototype.sort()对数组对象排序的方法
2015/01/28 Javascript
js简单实现点击左右运动的方法
2015/04/10 Javascript
js实现Select列表各项上移和下移的方法
2015/08/14 Javascript
基于JavaScript操作DOM常用的API小结
2015/12/01 Javascript
JS数组去掉重复数据只保留一条的实现代码
2016/08/11 Javascript
jQuery实现鼠标经过时高亮,同时其他同级元素变暗的效果
2016/09/18 Javascript
javascript笔记之匿名函数和闭包
2017/02/06 Javascript
js实现自定义进度条效果
2017/03/15 Javascript
js中getBoundingClientRect的作用及兼容方案详解
2018/02/01 Javascript
使用vux实现上拉刷新功能遇到的坑
2018/02/08 Javascript
javascript数据结构之多叉树经典操作示例【创建、添加、遍历、移除等】
2018/08/01 Javascript
vue基础之使用get、post、jsonp实现交互功能示例
2019/03/12 Javascript
详解关于JSON.parse()和JSON.stringify()的性能小测试
2019/03/14 Javascript
小程序实现搜索框功能
2020/03/26 Javascript
关于vue属性使用和不使用冒号的区别说明
2020/10/22 Javascript
[48:12]Secret vs Optic Supermajor 胜者组 BO3 第三场 6.4
2018/06/05 DOTA
一步步教你用Python实现2048小游戏
2017/01/19 Python
详解Python图像处理库Pillow常用使用方法
2019/09/02 Python
Scrapy中如何向Spider传入参数的方法实现
2020/09/28 Python
python更新数据库中某个字段的数据(方法详解)
2020/11/18 Python
《要下雨了》教学反思
2014/02/17 职场文书
保险内勤岗位职责
2014/04/05 职场文书
幼儿园大班开学寄语
2014/08/02 职场文书
俞敏洪一分钟演讲稿
2014/08/26 职场文书
2014年机关后勤工作总结
2014/12/16 职场文书
求职简历自我评价范文
2015/03/10 职场文书
欠款起诉书范文
2015/05/19 职场文书
帮你提高开发效率的JavaScript20个技巧
2021/06/18 Javascript
浅谈 JavaScript 沙箱Sandbox
2021/11/02 Javascript