{"id":1998,"date":"2018-03-06T21:06:27","date_gmt":"2018-03-07T05:06:27","guid":{"rendered":"http:\/\/zxi.mytechroad.com\/blog\/?p=1998"},"modified":"2018-03-15T23:47:52","modified_gmt":"2018-03-16T06:47:52","slug":"leetcode-172-factorial-trailing-zeroes","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-172-factorial-trailing-zeroes\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 172. Factorial Trailing Zeroes"},"content":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42n!\u672b\u5c3e0\u7684\u4e2a\u6570\u3002<\/p>\n<h1>Problem:<\/h1>\n<p><a href=\"https:\/\/leetcode.com\/problems\/factorial-trailing-zeroes\/description\/\">https:\/\/leetcode.com\/problems\/factorial-trailing-zeroes\/description\/<\/a><\/p>\n<p>Given an integer\u00a0<i>n<\/i>, return the number of trailing zeroes in\u00a0<i>n<\/i>!.<\/p>\n<p><b>Note:\u00a0<\/b>Your solution should be in logarithmic time complexity.<\/p>\n<h1><strong>Idea:<\/strong><\/h1>\n<p>All trailing zeros are come from even_num x 5, we have more even_num than 5, so only count factor 5.<\/p>\n<p>4! = 1x2x3x4 = 24, we haven&#8217;t encountered any 5 yet, so we don&#8217;t have any trailing zero.<\/p>\n<p>5! = 1x2x3x4x5 = 120, we have one trailing zero. either 2&#215;5, or 4&#215;5 can contribute to that zero.<\/p>\n<p>9! =\u00a0362880, we only encountered 5 once, so 1 trailing zero as expected.<\/p>\n<p>10! =\u00a03628800, 2 trailing zeros, since we have two numbers that have factor 5, one is 5 and the other is 10 (2&#215;5)<\/p>\n<p>What about 100! then?<\/p>\n<p>100\/5 = 20, we have 20 numbers have factor 5: 5, 10, 15, 20, 25, &#8230;, 95, 100.<\/p>\n<p>Is the number of trailing zero 20? No, it&#8217;s 24, why?<\/p>\n<p>Within that 20 numbers, we have 4 of them: 25 (5&#215;5), 50 (2x5x5), 75 (3x5x5), 100 (4x5x5) that have an extra factor of 5.<\/p>\n<p>So, for a given number n, we are looking how many numbers &lt;=n have factor 5, 5&#215;5, 5x5x5, &#8230;<\/p>\n<p>Summing those numbers up we got the answer.<\/p>\n<p>e.g. 1000! has 249 trailing zeros:<\/p>\n<p>1000\/5 = 200<\/p>\n<p>1000\/25 = 40<\/p>\n<p>1000\/125 = 8<\/p>\n<p>1000\/625 = 1<\/p>\n<p>200 + 40 + 8 + 1 = 249<\/p>\n<p>alternatively, we can do the following<\/p>\n<p>1000\/5 = 200<\/p>\n<p>200\/5 = 40<\/p>\n<p>40\/5 = 8<\/p>\n<p>8\/5 = 1<\/p>\n<p>1\/5 = 0<\/p>\n<p>200 + 40 + 8 + 1 + 0 = 249<\/p>\n<h1><strong>Solution 1: Recursion<\/strong><\/h1>\n<p>Time complexity: O(log5(n))<\/p>\n<p>Space complexity:\u00a0O(log5(n))<\/p>\n<h2>C++<\/h2>\n<pre class=\"lang:c++ decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 4 ms\r\nclass Solution {\r\npublic:\r\n  int trailingZeroes(int n) {\r\n    return n &lt; 5 ? 0 : n \/5 + trailingZeroes(n \/ 5);\r\n  }\r\n};<\/pre>\n<h2>Java<\/h2>\n<pre class=\"lang:java decode:true \">\/\/ Author: Huahua\r\n\/\/ Running time: 1 ms\r\nclass Solution {\r\n  public int trailingZeroes(int n) {\r\n    return n &lt; 5 ? 0 : n \/5 + trailingZeroes(n \/ 5);\r\n  }\r\n}<\/pre>\n<h3>Python3<\/h3>\n<pre class=\"lang:python decode:true \">\"\"\"\r\nAuthor: Huahua\r\nRunning time: 68 ms\r\n\"\"\"\r\nclass Solution:\r\n  def trailingZeroes(self, n):\r\n    return 0 if n &lt; 5 else n \/\/ 5 + self.trailingZeroes(n \/\/ 5)<\/pre>\n<h1><strong>Related Problems:<\/strong><\/h1>\n<ul>\n<li><a href=\"http:\/\/zxi.mytechroad.com\/blog\/math\/leetcode-793-preimage-size-of-factorial-zeroes-function\/\">\u82b1\u82b1\u9171 LeetCode 793. Preimage Size of Factorial Zeroes Function<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>\u9898\u76ee\u5927\u610f\uff1a\u6c42n!\u672b\u5c3e0\u7684\u4e2a\u6570\u3002 Problem: https:\/\/leetcode.com\/problems\/factorial-trailing-zeroes\/description\/ Given an integer\u00a0n, return the number of trailing zeroes in\u00a0n!. Note:\u00a0Your solution should be in logarithmic time complexity. Idea: All trailing zeros&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[49],"tags":[222,230,31],"class_list":["post-1998","post","type-post","status-publish","format-standard","hentry","category-math","tag-easy","tag-factorial","tag-math","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=1998"}],"version-history":[{"count":6,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions"}],"predecessor-version":[{"id":2119,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/1998\/revisions\/2119"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=1998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=1998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=1998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}