Classic interview questions: the factorial in question 100 has a few ending zeros.

Source: Internet
Author: User

100! = 100*99*98*97 *... * 2*1 first, let's take a look at how the zero end is formed: the zero end is formed when a multiples of 5 and a multiple of 2 are multiplied. So we only need to calculate a few () pairs, and we can know that there are a few ending zeros. There are several 5 factors first: within 100, 5 as the factor has 5, 10, 15, 20, 25... a total of 20. However, we note that 25, 50, and 75,100 both contain 2 5 as the factor (25 = 5*5, 50 = 2*5*5). Therefore, we need to do this most once. Therefore, there are 24 5 factors in total. From the formula point of view: the number of 5 factors = 100/5 + 100/(5 ^ 2) + 100/(5 ^ 3) +... = 24 (must be an integer) Now there are several factors in the number 2: 2, 4, 6, 8, 10 ,... A total of 100/2 = 50 2 factors, 100/4 = 25 4 factors (count more once), 100/8 = 12 8 factors (count more once ),... so the number of 2 factors = 100/2 + 100/(2 ^ 2) + 100/(2 ^ 3) + 100/(2 ^ 4) + 100/(2 ^ 5) + 100/(2 ^ 6) + 100/(2 ^ 7) +... = 97 In summary, there are 24 5 factors and 97 2 factors in total, so 24 (2, 5) pairs can be merged. Therefore, it is easy to write a program after 24 factorial ends with zero knowledge of ideas and formulas. When is there any time to complete the program Update: 11.16 now complete the program, respectively on the two OJ test, no problem. The POJ http://poj.org/problem? Id = 1401

Import java. io. *; import java. util. *; public class Main {public static void main (String args []) {program cin = new program (System. in); int cnt = cin. nextInt (); int input; for (int I = 0; I <cnt; I ++) {input = cin. nextInt (); System. out. println (trailingZeros (input);} // you can only calculate the number of five factors, because the number of five factors must be less than the number of two factors public static int trailingZeros (int input) {int quotient; int fiveFactors = 0; quotient = input/ 5; int I = 1; while (quotient! = 0) {fiveFactors + = quotient; I ++; quotient = (int) (input/Math. pow (5, I);} return fiveFactors ;}}

 

Package Factorial; import java. io. *; import java. util. *; public class Main {public static void main (String args []) {program cin = new program (System. in); int input = cin. nextInt (); while (input! = 0) {System. out. println (trailingZeros (input); input = cin. nextInt ();} System. out. println () ;}// you only need to calculate the number of 5 factors, because the number of 5 factors must be less than the number of 2 factors. public static int trailingZeros (int input) {int quotient; int fiveFactors = 0; quotient = input/5; int I = 1; while (quotient! = 0) {fiveFactors + = quotient; I ++; quotient = (int) (input/Math. pow (5, I);} return fiveFactors ;}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.