<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Factorial Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/factorial/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/factorial/</link>
	<description></description>
	<lastBuildDate>Fri, 16 Mar 2018 06:47:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>Factorial Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/factorial/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 172. Factorial Trailing Zeroes</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 07 Mar 2018 05:06:27 +0000</pubDate>
				<category><![CDATA[Math]]></category>
		<category><![CDATA[easy]]></category>
		<category><![CDATA[Factorial]]></category>
		<category><![CDATA[math]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1998</guid>

					<description><![CDATA[<p>题目大意：求n!末尾0的个数。 Problem: https://leetcode.com/problems/factorial-trailing-zeroes/description/ Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in logarithmic time complexity. Idea: All trailing zeros&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/">花花酱 LeetCode 172. Factorial Trailing Zeroes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>题目大意：求n!末尾0的个数。</p>
<h1>Problem:</h1>
<p><a href="https://leetcode.com/problems/factorial-trailing-zeroes/description/">https://leetcode.com/problems/factorial-trailing-zeroes/description/</a></p>
<p>Given an integer <i>n</i>, return the number of trailing zeroes in <i>n</i>!.</p>
<p><b>Note: </b>Your solution should be in logarithmic time complexity.</p>
<h1><strong>Idea:</strong></h1>
<p>All trailing zeros are come from even_num x 5, we have more even_num than 5, so only count factor 5.</p>
<p>4! = 1x2x3x4 = 24, we haven&#8217;t encountered any 5 yet, so we don&#8217;t have any trailing zero.</p>
<p>5! = 1x2x3x4x5 = 120, we have one trailing zero. either 2&#215;5, or 4&#215;5 can contribute to that zero.</p>
<p>9! = 362880, we only encountered 5 once, so 1 trailing zero as expected.</p>
<p>10! = 3628800, 2 trailing zeros, since we have two numbers that have factor 5, one is 5 and the other is 10 (2&#215;5)</p>
<p>What about 100! then?</p>
<p>100/5 = 20, we have 20 numbers have factor 5: 5, 10, 15, 20, 25, &#8230;, 95, 100.</p>
<p>Is the number of trailing zero 20? No, it&#8217;s 24, why?</p>
<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>
<p>So, for a given number n, we are looking how many numbers &lt;=n have factor 5, 5&#215;5, 5x5x5, &#8230;</p>
<p>Summing those numbers up we got the answer.</p>
<p>e.g. 1000! has 249 trailing zeros:</p>
<p>1000/5 = 200</p>
<p>1000/25 = 40</p>
<p>1000/125 = 8</p>
<p>1000/625 = 1</p>
<p>200 + 40 + 8 + 1 = 249</p>
<p>alternatively, we can do the following</p>
<p>1000/5 = 200</p>
<p>200/5 = 40</p>
<p>40/5 = 8</p>
<p>8/5 = 1</p>
<p>1/5 = 0</p>
<p>200 + 40 + 8 + 1 + 0 = 249</p>
<h1><strong>Solution 1: Recursion</strong></h1>
<p>Time complexity: O(log5(n))</p>
<p>Space complexity: O(log5(n))</p>
<h2>C++</h2>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 4 ms
class Solution {
public:
  int trailingZeroes(int n) {
    return n &lt; 5 ? 0 : n /5 + trailingZeroes(n / 5);
  }
};</pre><p></p>
<h2>Java</h2>
<p></p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 1 ms
class Solution {
  public int trailingZeroes(int n) {
    return n &lt; 5 ? 0 : n /5 + trailingZeroes(n / 5);
  }
}</pre><p></p>
<h3>Python3</h3>
<p></p><pre class="crayon-plain-tag">"""
Author: Huahua
Running time: 68 ms
"""
class Solution:
  def trailingZeroes(self, n):
    return 0 if n &lt; 5 else n // 5 + self.trailingZeroes(n // 5)</pre><p></p>
<h1><strong>Related Problems:</strong></h1>
<ul>
<li><a href="http://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/">花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</a></li>
</ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/">花花酱 LeetCode 172. Factorial Trailing Zeroes</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</title>
		<link>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/</link>
					<comments>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/#comments</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 04 Mar 2018 08:01:23 +0000</pubDate>
				<category><![CDATA[Binary Search]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Factorial]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[zeros]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=1944</guid>

					<description><![CDATA[<p>Let f(x) be the number of zeroes at the end of x!. (Recall that x! = 1 * 2 * 3 * ... * x, and by convention, 0! =&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/">花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Let <code>f(x)</code> be the number of zeroes at the end of <code>x!</code>. (Recall that <code>x! = 1 * 2 * 3 * ... * x</code>, and by convention, <code>0! = 1</code>.)</p>
<p>For example, <code>f(3) = 0</code> because 3! = 6 has no zeroes at the end, while <code>f(11) = 2</code> because 11! = 39916800 has 2 zeroes at the end. Given <code>K</code>, find how many non-negative integers <code>x</code> have the property that <code>f(x) = K</code>.</p><pre class="crayon-plain-tag">Example 1:
Input: K = 0
Output: 5
Explanation: 0!, 1!, 2!, 3!, and 4! end with K = 0 zeroes.

Example 2:
Input: K = 5
Output: 0
Explanation: There is no x such that x! ends in K = 5 zeroes.</pre><p><strong>Note:</strong></p>
<ul>
<li><code>K</code> will be an integer in the range <code>[0, 10^9]</code>.</li>
</ul>
<p><strong>Idea:</strong></p>
<p>First we need to compute how many trailing zeros n! has.</p>
<p>See  <a href="http://zxi.mytechroad.com/blog/math/leetcode-172-factorial-trailing-zeroes/">花花酱 LeetCode 172. Factorial Trailing Zeroes</a> for details</p>
<p>It&#8217;s hard to say how many numbers have trailing zeros equals to K, but we can find the largest number p whose trailing zeros is K using binary search. (p+1)! has more than K trailing zeros. And do the same thing to find the largest number q whose trailing zeros is K &#8211; 1 using binary search.</p>
<p>Then we know that are exact p numbers 1,2,&#8230;,p whose trailing zeros are less or equal to K.</p>
<p>And exact q numbers 1, 2, &#8230;, q whose trailing zeros are less or equal to K &#8211; 1.</p>
<p>q + 1, q + 2, &#8230;, m (m &#8211; q numbers in total) the numbers with trailing zeros equal to K.</p>
<p><strong>Solution 1: Math + Binary Search</strong></p>
<p>Time complexity: O(log2(INT_MAX)*log5(INT_MAX))</p>
<p>Space complexity: O(1)</p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 3 ms
class Solution {
public:
  int preimageSizeFZF(int K) {
    return g(K) - g(K - 1);
  }
private:
  // Find the largest number l, that numZeros(l!) == K and numZeros((l+1)!) &gt; K
  int g(int K) {    
    int l = 0;
    int r = INT_MAX;
    while (l &lt; r) {
      int m = l + (r - l) / 2;
      int zeros = numZeros(m);
      if (zeros &lt;= K)
        l = m + 1;
      else
        r = m;
    }    
    return l;
  }
  
  int numZeros(int n) {
    return n &lt; 5 ? 0 : n / 5 + numZeros(n / 5);
  }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/">花花酱 LeetCode 793. Preimage Size of Factorial Zeroes Function</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/math/leetcode-793-preimage-size-of-factorial-zeroes-function/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
	</channel>
</rss>
