详解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实现addClass,removeClass,hasClass的函数代码
Jul 13 Javascript
jquery获取css中的选择器(实例讲解)
Dec 02 Javascript
jQuery实现的数值范围range2dslider选取插件特效多款代码分享
Aug 27 Javascript
理解javascript定时器中的单线程
Feb 23 Javascript
判断输入的字符串是否是日期格式的简单方法
Jul 11 Javascript
详解jQuery中基本的动画方法
Dec 14 Javascript
Angular2 Service实现简单音乐播放器服务
Feb 24 Javascript
JS兼容所有浏览器的DOMContentLoaded事件
Jan 12 Javascript
微信小程序--特定区域滚动到顶部时固定的方法
Apr 28 Javascript
使用 vue 实现灭霸打响指英雄消失的效果附demo
May 06 Javascript
layui动态渲染生成左侧3级菜单的方法(根据后台返回数据)
Sep 23 Javascript
微信小程序实现签字功能
Dec 23 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
注意:php5.4删除了session_unregister函数
2013/08/05 PHP
PHP实现压缩图片尺寸并转为jpg格式的方法示例
2018/05/10 PHP
YII框架页面缓存操作示例
2019/04/29 PHP
正则表达式判断是否存在中文和全角字符和判断包含中文字符串长度
2008/09/27 Javascript
javascript Base类 包含基本的方法
2009/07/22 Javascript
js操作textarea方法集合封装(兼容IE,firefox)
2011/02/22 Javascript
使用Sticker.js实现贴纸效果
2015/01/28 Javascript
浅谈JS继承_寄生式继承 &amp; 寄生组合式继承
2016/08/16 Javascript
第一次接触Bootstrap框架
2016/10/24 Javascript
使用JS读取XML文件的方法
2016/11/25 Javascript
jQuery使用ajax传递json对象到服务端及contentType的用法示例
2020/03/12 jQuery
Vue作用域插槽实现方法及作用详解
2020/07/08 Javascript
node.js 基于 STMP 协议和 EWS 协议发送邮件
2021/02/14 Javascript
[02:37]2018DOTA2亚洲邀请赛赛前采访-EG篇
2018/04/03 DOTA
Python 返回汉字的汉语拼音
2009/02/27 Python
Django 浅谈根据配置生成SQL语句的问题
2018/05/29 Python
详解如何在cmd命令窗口中搭建简单的python开发环境
2019/08/29 Python
django 数据库 get_or_create函数返回值是tuple的问题
2020/05/15 Python
sqlalchemy实现时间列自动更新教程
2020/09/02 Python
浅谈matplotlib默认字体设置探索
2021/02/03 Python
css3 media 响应式布局的简单实例
2016/08/03 HTML / CSS
canvas需要在标签里直接定义宽高
2014/12/17 HTML / CSS
什么是跨站脚本攻击
2014/12/11 面试题
自我鉴定200字
2013/10/28 职场文书
电脑饰品店的创业计划书
2014/01/21 职场文书
顶撞老师检讨书
2014/02/07 职场文书
质量月口号
2014/06/20 职场文书
学习党代会心得体会
2014/09/05 职场文书
市场部岗位职责
2015/02/12 职场文书
中学生国庆节演讲稿2015
2015/07/30 职场文书
四年级数学教学反思
2016/02/16 职场文书
同学会演讲稿
2019/04/02 职场文书
pycharm无法导入lxml的解决办法
2021/03/31 Python
Goland使用Go Modules创建/管理项目的操作
2021/05/06 Golang
Lakehouse数据湖并发控制陷阱分析
2022/03/31 Oracle
阿里云国际版 使用Nginx作为HTTPS转发代理服务器
2022/05/11 Servers