详解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 相关文章推荐
js制作的鼠标悬浮时产生的下拉框效果
Oct 27 Javascript
js中生成map对象的方法
Jan 09 Javascript
Node调试工具JSHint的安装及配置教程
May 27 Javascript
javascript 将共享属性迁移到原型中去的实现方法
Aug 31 Javascript
详解原生js实现offset方法
Jun 15 Javascript
详解vue express启动数据服务
Jul 05 Javascript
vue+node+webpack环境搭建教程
Nov 05 Javascript
React props和state属性的具体使用方法
Apr 12 Javascript
详解javascript中var与ES6规范中let、const区别与用法
Jan 11 Javascript
jQuery事件模型默认行为执行顺序及trigger()与 triggerHandler()比较实例分析
Apr 30 jQuery
微信小程序基于高德地图API实现天气组件(动态效果)
Oct 22 Javascript
jquery实现淡入淡出轮播图效果
Dec 13 jQuery
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
基于mysql的论坛(5)
2006/10/09 PHP
Linux下将excel数据导入到mssql数据库中的方法
2010/02/08 PHP
页面乱码问题的根源及其分析
2013/08/09 PHP
PHP中模拟链表和链表的基本操作示例
2016/02/27 PHP
心扬JS分页函数代码
2010/09/10 Javascript
script标签的 charset 属性使用说明
2010/12/04 Javascript
web开发人员学习jQuery的6大理由及jQuery的优势介绍
2013/01/03 Javascript
jquery禁止回车触发表单提交
2014/12/12 Javascript
Jquery日历插件制作简单日历
2015/10/28 Javascript
浅析JavaScript回调函数应用
2016/05/22 Javascript
javaScript嗅探执行神器-sniffer.js
2017/02/14 Javascript
react性能优化达到最大化的方法 immutable.js使用的必要性
2017/03/09 Javascript
parabola.js抛物线与加入购物车效果的示例代码
2017/10/25 Javascript
详解Chart.js轻量级图表库的使用经验
2018/05/22 Javascript
JavaScript模拟实现自由落体效果
2018/08/28 Javascript
vee-validate vue 2.0自定义表单验证的实例
2018/08/28 Javascript
jQuery实现根据身份证号获取生日、年龄、性别等信息的方法
2019/01/09 jQuery
python+selenium开发环境搭建图文教程
2017/08/11 Python
Python实现将doc转化pdf格式文档的方法
2018/01/19 Python
替换python字典中的key值方法
2018/07/06 Python
Python闭包函数定义与用法分析
2018/07/20 Python
Python3.7实现中控考勤机自动连接
2018/08/28 Python
pycharm new project变成灰色的解决方法
2019/06/27 Python
CSS3的新特性介绍
2008/10/31 HTML / CSS
HTML5之HTML元素扩展(上)—新增加的元素及使用概述
2013/01/31 HTML / CSS
HTML5 Notification(桌面提醒)功能使用实例
2014/03/17 HTML / CSS
瑜伽灵感珠宝:Satya Jewelry
2018/01/06 全球购物
播音主持专业个人自我评价
2014/01/09 职场文书
英文留学推荐信范文
2014/01/25 职场文书
党员干部公开承诺书
2014/03/26 职场文书
含预算的公司户外活动方案
2014/08/16 职场文书
保密工作整改报告
2014/11/06 职场文书
2014年体育教学工作总结
2014/12/09 职场文书
护士求职自荐信范文
2015/03/04 职场文书
教师个人自我评价
2015/03/04 职场文书
十个Python自动化常用操作,即拿即用
2021/05/10 Python