how many does the factorial of n have zero?

how many does the n factorial end have zero?

个人信息:就读于燕大本科软件工程专业 目前大三;

本人博客:google搜索“cqs_2012”即可;

个人爱好:酷爱数据结构和算法,希望将来从事算法工作为人民作出自己的贡献;

博客内容:how many does the factorial of n have zero?

博客时间:2014-5-5;

编程语言:Java ;

编程坏境:Windows 7 专业版 x64;

编程工具:jdk,eclipse;

制图工具:office 2010 ppt;

硬件信息:7G-3 笔记本;

my words

nature is so beautiful.

problem( from beauty of programming )

how many do the factorial of n have zero?

my solution

first solution: get the n factorial, then get how many zero the end of it has.

time cost: O(n)

second solution: 1 * 2 * 3 * 4 * ···· * n. we can know zero form 2 * 5, and know that the number of 2 is more than that of 5 in these factors. So we get the number of 5.

1) we can computer how many 5 factor every number from 1 to n have.

2) we can use n to get all the sum of number of 5 factor.

my code

	int _Number_n_factorial(int n)
	{
		int i = n;
		int result = 0;
		while(i > 0)
		{
			result += i/5 ;
			i = i/5;
		}
		return result;
	}

program run out:

the line of 1 is 0
the line of 2 is 0
the line of 3 is 0
the line of 4 is 0
the line of 5 is 1
the line of 6 is 1
the line of 7 is 1
the line of 8 is 1
the line of 9 is 1
the line of 10 is 2
the line of 11 is 2
the line of 12 is 2
the line of 13 is 2
the line of 14 is 2
the line of 15 is 3
the line of 16 is 3
the line of 17 is 3
the line of 18 is 3
the line of 19 is 3
the line of 20 is 4
the line of 21 is 4
the line of 22 is 4
the line of 23 is 4
the line of 24 is 4
the line of 25 is 6
the line of 26 is 6
the line of 27 is 6
the line of 28 is 6
the line of 29 is 6
the line of 30 is 7
the line of 31 is 7
the line of 32 is 7
the line of 33 is 7
the line of 34 is 7
the line of 35 is 8
the line of 36 is 8
the line of 37 is 8
the line of 38 is 8
the line of 39 is 8
the line of 40 is 9
the line of 41 is 9
the line of 42 is 9
the line of 43 is 9
the line of 44 is 9
the line of 45 is 10
the line of 46 is 10
the line of 47 is 10
the line of 48 is 10
the line of 49 is 10
the line of 50 is 12
the line of 51 is 12
the line of 52 is 12
the line of 53 is 12
the line of 54 is 12
the line of 55 is 13
the line of 56 is 13
the line of 57 is 13
the line of 58 is 13
the line of 59 is 13
the line of 60 is 14
the line of 61 is 14
the line of 62 is 14
the line of 63 is 14
the line of 64 is 14
the line of 65 is 15
the line of 66 is 15
the line of 67 is 15
the line of 68 is 15
the line of 69 is 15
the line of 70 is 16
the line of 71 is 16
the line of 72 is 16
the line of 73 is 16
the line of 74 is 16
the line of 75 is 18
the line of 76 is 18
the line of 77 is 18
the line of 78 is 18
the line of 79 is 18
the line of 80 is 19
the line of 81 is 19
the line of 82 is 19
the line of 83 is 19
the line of 84 is 19
the line of 85 is 20
the line of 86 is 20
the line of 87 is 20
the line of 88 is 20
the line of 89 is 20
the line of 90 is 21
the line of 91 is 21
the line of 92 is 21
the line of 93 is 21
the line of 94 is 21
the line of 95 is 22
the line of 96 is 22
the line of 97 is 22
the line of 98 is 22
the line of 99 is 22

my code

test.java

package test;

public class test {

	public static void main(String[] args) {
		//System.in.read();
		int i = 1;
		int result;
		while( i<100 )
		{
			result = _Number_n_factorial(i);
			System.out.println("the line of "+i+" is "+result);
			i = i+1;
		}

	}

	public static int _Number_n_factorial(int n)
	{
		int i = n;
		int result = 0;
		while(i > 0)
		{
			result += i/5 ;
			i = i/5;
		}
		return result;
	}

}

how many does the factorial of n have zero?,布布扣,bubuko.com

时间: 2024-08-28 02:10:44

how many does the factorial of n have zero?的相关文章

LeetCode172 Factorial Trailing Zeroes. LeetCode258 Add Digits. LeetCode268 Missing Number

数学题 172. Factorial Trailing Zeroes Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (Easy) 分析:求n的阶乘中末位0的个数,也就是求n!中因数5的个数(2比5多),简单思路是遍历一遍,对于每个数,以此除以5求其因数5的个数,但会超时. 考虑到一个数n比他小

Project Euler:Problem 74 Digit factorial chains

The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145: 1! + 4! + 5! = 1 + 24 + 120 = 145 Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it tu

[LeetCode]172.Factorial Trailing Zeroes

题目 Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. 分析 朴素解法: 首先求出n!,然后计算末尾0的个数.(重复÷10,直到余数非0) 该解法在输入的数字稍大时就会导致阶乘得数溢出,不足取. O(logn)解法: 考虑n!的质数因子. 后缀0总是由质因子2和质因子5相乘得来的.如果我们可以计数

POJ 1775 sum of Factorial (数论)

链接:http://poj.org/problem?id=1775 Description John von Neumann, b. Dec. 28, 1903, d. Feb. 8, 1957, was a Hungarian-American mathematician who made important contributions to the foundations of mathematics, logic, quantum physics,meteorology, science,

SPOJ FCTRL - Factorial

题目链接:http://www.spoj.com/problems/FCTRL/ 题目大意:询问N的阶乘的结果尾数有几个0. 解题思路:考虑问题:N的阶乘的结果能被2m整除,这个m最大为多少. 我们对前N个数除以2,忽略奇数,会得到N/2个数字.那么相当于我们得到了2N/2 对之后的N/2个数字继续除以2,同样忽略奇数.我们会再得到2N/4 ... 所以m=N/2 + N/4 + N/8 +...+0. 那么对于这个问题,我们计算出N的记成被2m1整除的最大m1以及被5m2整除的最大m2,由于2

Factorial Trailing Zeroes

Factorial Trailing Zeroes Total Accepted: 44612 Total Submissions: 144778 Difficulty: Easy Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. (M) Number of Digit One n!的结果中0的个

Codeforces 515C. Drazil and Factorial

//codeforces 515C. Drazil and Factorial #include <iostream> #include <cstring> #include <cstdio> using namespace std; /** 4!=2!*2!*3! 6!=1*2*3*4*5*6=5!*3! 8!=1*2*3*4*5*6*7*8=7!*2!*2!*2! 9!=1*2*3*4*5*6*7*8*9=7!*2!*3!*3! */ int main() { ch

Hex Factorial 高精度

Hex Factorial 求n的阶乘结果的十六进制表示中,用多少个0.       java秒之! 1 import java.io.*; 2 import java.math.*; 3 import java.util.*; 4 5 public class Main 6 { 7 public static void main(String[] args) 8 { 9 Scanner cin=new Scanner(new BufferedInputStream(System.in)); 1

light oj 1045 - Digits of Factorial K进制下N!的位数

1045 - Digits of Factorial Factorial of an integer is defined by the following function f(0) = 1 f(n) = f(n - 1) * n, if(n > 0) So, factorial of 5 is 120. But in different bases, the factorial may be different. For example, factorial of 5 in base 8 i