详解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类和继承 this属性使用说明
Sep 03 Javascript
JS自动缩小超出大小的图片
Oct 12 Javascript
js中parseFloat(参数1,参数2)定义和用法及注意事项
Jan 27 Javascript
让checkbox不选中即将选中的checkbox不选中
Jul 11 Javascript
输入框过滤非数字的js代码
Sep 18 Javascript
JavaScript解析json格式数据简单示例
Dec 09 Javascript
jQuery选择器源码解读(七):elementMatcher函数
Mar 31 Javascript
js实现下拉列表选中某个值的方法(3种方法)
Dec 17 Javascript
AngularJS下对数组的对比分析
Aug 24 Javascript
微信小程序 教程之事件
Oct 18 Javascript
JavaScript中最常用的10种代码简写技巧总结
Jun 28 Javascript
Vue.js样式动态绑定实现小结
Jan 24 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连接MSSQL2008/2005数据库(SQLSRV)配置实例
2014/10/22 PHP
CI框架Session.php源码分析
2014/11/03 PHP
两款万能的php分页类
2015/11/12 PHP
PHP使用文件锁解决高并发问题示例
2018/03/29 PHP
php实现文件上传基本验证
2020/03/04 PHP
JavaScript DOM学习第一章 W3C DOM简介
2010/02/19 Javascript
flexigrid 参数说明
2010/11/23 Javascript
利用JQuery的load函数动态加载其它页面的内容的实现代码
2010/12/14 Javascript
基于Jquery与WebMethod投票功能实现代码
2011/01/19 Javascript
JavaScript如何自定义trim方法
2015/07/28 Javascript
延时加载JavaScript代码提高速度
2015/12/27 Javascript
Canvas 制作动态进度加载水球详解及实例代码
2016/12/09 Javascript
easyUI实现类似搜索框关键词自动提示功能示例代码
2016/12/27 Javascript
Html5 js实现手风琴效果
2020/04/17 Javascript
Bootstrap页面标题Page Header的实现方法
2017/03/22 Javascript
vue-router实现tab标签页(单页面)详解
2017/10/17 Javascript
浅谈es6 javascript的map数据结构
2017/12/14 Javascript
解决vue2.0 element-ui中el-upload的before-upload方法返回false时submit()不生效问题
2018/08/24 Javascript
vue中前进刷新、后退缓存用户浏览数据和浏览位置的实例讲解
2018/09/21 Javascript
微信小程序时间选择插件使用详解
2018/12/28 Javascript
react 中父组件与子组件双向绑定问题
2019/05/20 Javascript
详解Vue数据驱动原理
2020/11/17 Javascript
python通过定义一个类实例作为ftp回调方法
2015/05/04 Python
一个基于flask的web应用诞生 使用模板引擎和表单插件(2)
2017/04/11 Python
python实现kmp算法的实例代码
2019/04/03 Python
Django CBV与FBV原理及实例详解
2019/08/12 Python
Pytorch之卷积层的使用详解
2019/12/31 Python
在django中查询获取数据,get, filter,all(),values()操作
2020/08/09 Python
css3 flex布局 justify-content:space-between 最后一行左对齐
2020/01/02 HTML / CSS
德国家具、照明、家居用品网上商店:Wayfair.de
2020/02/13 全球购物
大四学生思想汇报
2014/01/13 职场文书
服务员自我评价
2014/01/25 职场文书
工作自我评价怎么写
2014/01/29 职场文书
师德培训心得体会2016
2016/01/09 职场文书
2019秋季运动会口号
2019/06/25 职场文书
详解Nginx的超时keeplive_timeout配置步骤
2022/05/25 Servers