求阶乘的第一个非零数字

Problem statement:

问题陈述:

Find the number of trailing zeros in n! (Where, n is the given input).

n中找到尾随零的数目 (其中, n是给定的输入)。

Solution:

解:

Computing a factorial is of course expansive. Though using dynamic programming the computing expanse can be managed, for the large value of n, the factorial value is going exceed normal data size. Needless to say, computing the whole factorial is not the way to find the number of trailing zeros. There must be some advanced algorithm to find the no of trailing zeros.

计算阶乘当然是可扩展的。 尽管使用动态编程可以管理计算范围,但是对于较大的n值,阶乘值将超过正常数据大小。 不用说,计算整个阶乘不是找到尾随零的数量的方法 。 必须有一些高级算法来找到尾随零

Firstly, we need to understand what causes trailing zeroes. A pair of 2 & 5 is the reason behind a trailing zero. Thus a pair of 2 & 5 in the factorial expression leads to a trailing zero. Thus we simply need to check how many pairs (different) of 2 & 5 are there.

首先,我们需要了解导致尾随零的原因。 一对25是尾随零后面的原因。 因此,阶乘表达式中的25对导致尾随零。 因此,我们只需要检查25有多少对(不同)。

Let's say 5!
5!= 5*4*3*2*1 (thus only one pair)
5!=120 ( only one trailing zero)

假设5!
5!= 5 * 4 * 3 * 2 * 1 (因此只有一对)
5!= 120 (仅一个尾随零)

Intuition says that we don’t even need to find the number of pairs as the occurrence of 2 as a factor is obvious if a 5 is present as a factor. Thus we only need to check how many 5 is there as a factor in the factorial.

直觉说,我们甚至不需要找到对的数量,因为如果5作为一个因素,则2作为一个因素的出现就很明显。 因此,我们仅需要检查阶乘中有5个因素。

Algorithm:

算法:

    Set count to 0
For(i=5;n/i>0;i=i*5)
count=count+ n/i;
Return count
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

C ++代码查找数字阶乘中的尾随零 (C++ code to find trailing zeros in factorial of a number)

#include<bits/stdc++.h>
using namespace std;
int trailingZeros(int n){int count=0;
if(n<0)
return -1;
for(int i=5;n/i>0;i*=5){count+=n/i;
}
return count;
}
int main(){int n;
cout<<"enter input,n"<<endl;
cin>>n;
if(trailingZeros(n))
cout<<"no of trailing zero in "<<n<<"! is "<<trailingZeros(n)<<endl;
else
cout<<"input is negative, can't proceed"<<endl;
return 0;
}

Output

输出量

First run:
enter input,n
15
no of trailing zero in 15! is 3
Second run:
enter input,n
100
no of trailing zero in 100! is 24

翻译自: https://www.includehelp.com/algorithms/find-trailing-zeros-in-factorial-of-a-number.aspx

求阶乘的第一个非零数字

求阶乘的第一个非零数字_查找数字阶乘中的尾随零相关推荐

  1. 求单链表的最大值与原地逆转_数据结构:单链表中求最大值的算法。

    可以参考下面的代码: public static int FindMax(Node head) { if (head == null) return 0; int Max = head.value; ...

  2. Python如何判断一个数据的小数点后面首个非零数字位于小数点后面第几位

    文章目录 前言 一.背景介绍 二.实现方法 1.引入库 2.算法实现 总结 前言 本文主要介绍一种判断一个浮点数的小数点后面首个非零数字位于小数点后面第几位的方法.该方法有时能帮助我们找到划分坐标轴的 ...

  3. python1到20数字阶乘_Python 程序求数字的阶乘

    Python 程序求数字的阶乘 在本文中,您将学习查找数字的阶乘并显示它. 要理解此示例,您应该了解以下Python编程主题: 一个数字的阶乘是从1到该数字的所有整数的乘积. 例如,阶乘6为1 * 2 ...

  4. [全文索引]非索引字表

    非索引字表 在 SQL Server 2008 中,使用称为"非索引字表"的对象在数据库中管理非索引字."非索引字表" 是一个由非索引字组成的列表,这些非索引字 ...

  5. kotlin 尾递归阶乘_Kotlin程序查找数字的阶乘

    kotlin 尾递归阶乘 Factorial of number is the product of all positive numbers less or equal to the number. ...

  6. 数组第一个值_Excel公式技巧69:查找第一个非空值

    学习Excel技术,关注微信公众号: excelperfect 在<Excel公式技巧63:查找最后一行>中,我们使用LOOKUP函数的公式获取最后一个值或该值所在的行号.如果列表中的前面 ...

  7. C语言socket accept()函数(提取出所监听套接字的等待连接队列中第一个连接请求,创建一个新的套接字,并返回指向该套接字的文件描述符)

    文章目录 名称 使用格式 功能参数描述 参数 sockfd addr addrlen 返回值 示例 man 2 文档中的accept解释 错误处理 名称 accept() 接收一个套接字中已建立的连接 ...

  8. 求n的阶乘的算法框图_算法|从阶乘计算看递归算法

    欢迎点击「算法与编程之美」↑关注我们! 本文首发于微信公众号:"算法与编程之美",欢迎关注,及时了解更多此系列文章. 1 理解递归 "程序设计是实践计算机思维的重要手段& ...

  9. 求出首地址为DATA的字数组中的最小偶数,并将它放在AX中

    ;求出首地址为DATA的字数组中的最小偶数,并将它放在AX中 DATAS SEGMENT DATA DW 1 DUP(1,1,1,1,1,2)   ;随便设置的数据,可换为其他数据 COUNT DW ...

最新文章

  1. lazada食品类目如何做好运营,来提升店铺销量?
  2. 1.httpClient和ScrollView
  3. Linux umask 文件默认权限
  4. Android.View.InflateException: Binary XML File Line #异常的解决
  5. Hibernate 学习(一)
  6. 5000并发的qps是多少_高并发架构设计
  7. Python面向对象基础一
  8. angular4获得焦点事件_深究AngularJS——如何获取input的焦点(自定义指令)
  9. 求向量的垂线_高考数学填空题如何快速求直线关于直线对称的直线方程
  10. 3dmax 对模型做bool运算
  11. 税控接口 - 模拟录入
  12. python离线安装环境 解决 ERROR: Could not find a version that satisfies the requirement xxx 以及winError[10061]
  13. 计算机网络安全的圣经-《Computer Network Security》
  14. ubuntu安装maya2011的方法
  15. 图形学常见概念与算法-常用初等数学公式
  16. html 按钮变成椭圆,HTML 渐变椭圆按钮
  17. 18-基于STM32的室内可见光通信系统设计
  18. 行车百科系列之(二): 被多数人忽略的行车安全大忌(静物篇)
  19. oracle autovue是什么软件,AutoVue
  20. 分布式事务之基础理论(CAID、CAP与BASE)

热门文章

  1. java ==和=_Java ==和equals()的区别
  2. html 实现列表组并排,列表组--自定义列表组
  3. java final static_Java基础之final、static关键字
  4. python 如何快速判断列表是否相同_Python-检查列表中的所有元素是否相同
  5. html 图片墙效果,基于html5实现的图片墙效果
  6. 在服务器上远程使用tensorboard查看训练loss和准确率
  7. C#委托、类和事件的验证【C#】
  8. Keepalived 做负载均衡(简单实例)
  9. js 判断日期时间差
  10. zabbix的agent端的主动模式关键三个参数