<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Trailing_zeroes_in_factorial</id>
		<title>Trailing zeroes in factorial - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wcipeg.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Trailing_zeroes_in_factorial"/>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;action=history"/>
		<updated>2026-05-01T11:35:00Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.25.2</generator>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1623&amp;oldid=prev</id>
		<title>Brian: use pointfree style :D</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1623&amp;oldid=prev"/>
				<updated>2012-02-28T23:07:52Z</updated>
		
		<summary type="html">&lt;p&gt;use pointfree style :D&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 23:07, 28 February 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L45&quot; &gt;Line 45:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 45:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Functional style (Haskell):&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Functional style (Haskell):&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;zeroes &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;n &lt;/del&gt;= sum &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;$ &lt;/del&gt;tail &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;$ &lt;/del&gt;takeWhile (&amp;gt;0) &lt;del class=&quot;diffchange diffchange-inline&quot;&gt;(&lt;/del&gt;iterate (flip div 5&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;) n&lt;/del&gt;) -- note that 0 is an edge case&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;zeroes = sum &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. &lt;/ins&gt;tail &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. &lt;/ins&gt;takeWhile (&amp;gt;0) &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;. &lt;/ins&gt;iterate (flip div 5) -- note that 0 is an edge case&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Bases other than 10 may be trickier to handle. For example, consider the problem of finding the number of zeroes at the end of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; when expressed in base 12 (with prime factorization &amp;lt;math&amp;gt;2^2 \cdot 3&amp;lt;/math&amp;gt;). In this case we have to count factors of 2 and factors of 3, but it takes ''two'' factors of 2 to make one factor of 12, so we have to divide by two and then compare it with the number of factors of 3 (the smaller of the two results will be the answer). It's not obvious which factor will &amp;quot;run out&amp;quot; first, so it might be necessary to find the number of times ''each'' prime factor in the base appears in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The algorithm shown above still works for each individual prime factor, though (provided that &amp;quot;5&amp;quot; is replaced with the appropriate number).&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Bases other than 10 may be trickier to handle. For example, consider the problem of finding the number of zeroes at the end of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; when expressed in base 12 (with prime factorization &amp;lt;math&amp;gt;2^2 \cdot 3&amp;lt;/math&amp;gt;). In this case we have to count factors of 2 and factors of 3, but it takes ''two'' factors of 2 to make one factor of 12, so we have to divide by two and then compare it with the number of factors of 3 (the smaller of the two results will be the answer). It's not obvious which factor will &amp;quot;run out&amp;quot; first, so it might be necessary to find the number of times ''each'' prime factor in the base appears in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The algorithm shown above still works for each individual prime factor, though (provided that &amp;quot;5&amp;quot; is replaced with the appropriate number).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1622&amp;oldid=prev</id>
		<title>Brian at 23:05, 28 February 2012</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1622&amp;oldid=prev"/>
				<updated>2012-02-28T23:05:33Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class='diff diff-contentalign-left'&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;col class='diff-marker' /&gt;
				&lt;col class='diff-content' /&gt;
				&lt;tr style='vertical-align: top;'&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan='2' style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Revision as of 23:05, 28 February 2012&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;L45&quot; &gt;Line 45:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 45:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Functional style (Haskell):&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Functional style (Haskell):&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;zeroes n = sum $ tail $ takeWhile (&amp;gt;0) (iterate (flip div 5) n)&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color:black; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;zeroes n = sum $ tail $ takeWhile (&amp;gt;0) (iterate (flip div 5) n) &lt;ins class=&quot;diffchange diffchange-inline&quot;&gt;-- note that 0 is an edge case&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Bases other than 10 may be trickier to handle. For example, consider the problem of finding the number of zeroes at the end of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; when expressed in base 12 (with prime factorization &amp;lt;math&amp;gt;2^2 \cdot 3&amp;lt;/math&amp;gt;). In this case we have to count factors of 2 and factors of 3, but it takes ''two'' factors of 2 to make one factor of 12, so we have to divide by two and then compare it with the number of factors of 3 (the smaller of the two results will be the answer). It's not obvious which factor will &amp;quot;run out&amp;quot; first, so it might be necessary to find the number of times ''each'' prime factor in the base appears in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The algorithm shown above still works for each individual prime factor, though (provided that &amp;quot;5&amp;quot; is replaced with the appropriate number).&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f9f9f9; color: #333333; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #e6e6e6; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;Bases other than 10 may be trickier to handle. For example, consider the problem of finding the number of zeroes at the end of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; when expressed in base 12 (with prime factorization &amp;lt;math&amp;gt;2^2 \cdot 3&amp;lt;/math&amp;gt;). In this case we have to count factors of 2 and factors of 3, but it takes ''two'' factors of 2 to make one factor of 12, so we have to divide by two and then compare it with the number of factors of 3 (the smaller of the two results will be the answer). It's not obvious which factor will &amp;quot;run out&amp;quot; first, so it might be necessary to find the number of times ''each'' prime factor in the base appears in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The algorithm shown above still works for each individual prime factor, though (provided that &amp;quot;5&amp;quot; is replaced with the appropriate number).&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	<entry>
		<id>https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1621&amp;oldid=prev</id>
		<title>Brian: Created page with &quot;A well-known programming exercise involves finding the number of trailing zeroes at the end of the decimal representation of &lt;math&gt;n!&lt;/math&gt;, for some given value of &lt;math&gt;n&lt;/mat...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wcipeg.com/wiki/index.php?title=Trailing_zeroes_in_factorial&amp;diff=1621&amp;oldid=prev"/>
				<updated>2012-02-28T19:28:33Z</updated>
		
		<summary type="html">&lt;p&gt;Created page with &amp;quot;A well-known programming exercise involves finding the number of trailing zeroes at the end of the decimal representation of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;, for some given value of &amp;lt;math&amp;gt;n&amp;lt;/mat...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;A well-known programming exercise involves finding the number of trailing zeroes at the end of the decimal representation of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;, for some given value of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;.&amp;lt;ref&amp;gt;{{SPOJ|FCTRL|Factorial}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{Problem|acsl1p3|Task 3: Zeros}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One possible approach, of course, is to explicitly calculate the factorial. The only problem with this is that &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; may be very large for reasonably sized values of &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;. For example, &amp;lt;math&amp;gt;100! &amp;gt; 10^{100}&amp;lt;/math&amp;gt;, so [[bignum]] arithmetic is needed for this kind of approach. This tends to be slow and inexpedient.&lt;br /&gt;
&lt;br /&gt;
A much cleverer solution&amp;amp;mdash;one that does not involve calculating the factorial explicitly&amp;amp;mdash;can be obtained by noting that what is really desired is the largest &amp;lt;math&amp;gt;k&amp;lt;/math&amp;gt; such that &amp;lt;math&amp;gt;10^k&amp;lt;/math&amp;gt; divides &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The prime factorization of &amp;lt;math&amp;gt;10^k&amp;lt;/math&amp;gt; is &amp;lt;math&amp;gt;2^k 5^k&amp;lt;/math&amp;gt;, so really all we need to do is find out how many factors of 2 are in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;, and how many factors of 5 are in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;, and the smaller of the two gives the answer. For example, &amp;lt;math&amp;gt;10!&amp;lt;/math&amp;gt; has prime factorization &amp;lt;math&amp;gt;2^8 \cdot 3^4 \cdot 5^2 \cdot 7&amp;lt;/math&amp;gt;, so it contains 8 factors of 2 and 2 factors of 5. Consistent with these figures, there are 2 zeroes at the end of its decimal representation (3628800).&lt;br /&gt;
&lt;br /&gt;
How do we calculate this? A workable approach is to simply determine how many factors of 2 and 5 are in each of the numbers &amp;lt;math&amp;gt;1, 2, ..., n&amp;lt;/math&amp;gt;. When we multiply those numbers to get &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;, we of course ''add'' the exponents in their prime factorizations; so the sum of the numbers of factors of 2 in each of the integers &amp;lt;math&amp;gt;1, 2, ..., n&amp;lt;/math&amp;gt; gives us the number of factors of 2 in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;; and we do something similar for factors of 5. Determining how many times 2 or 5 divides a number is as easy as actually performing the division over and over again (until a non-integer result is obtained).&lt;br /&gt;
&lt;br /&gt;
There is a cleverer solution yet, though. When we multiply all the numbers from 1 to &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt;, exactly &amp;lt;math&amp;gt;\lfloor n/2 \rfloor&amp;lt;/math&amp;gt; of them will be even, so each one of those contributes a factor of 2. However, the ones that are divisible by 4 actually contribute ''two'' factors of 2, so we have to add &amp;lt;math&amp;gt;\lfloor n/4 \rfloor&amp;lt;/math&amp;gt; to the result (we counted those numbers once in &amp;lt;math&amp;gt;\lfloor n/2 \rfloor&amp;lt;/math&amp;gt;, and now we are counting them again in &amp;lt;math&amp;gt;\lfloor n/4 \rfloor&amp;lt;/math&amp;gt;). But then there may be some numbers that are divisible by 8, which contribute at least ''three'' factors of 2, so we have to add &amp;lt;math&amp;gt;\lfloor n/8 \rfloor&amp;lt;/math&amp;gt;; and so on until we reach some power of 2 that is greater than &amp;lt;math&amp;gt;n&amp;lt;/math&amp;gt; (so that it obviously divides none of &amp;lt;math&amp;gt;1, 2, ..., n&amp;lt;/math&amp;gt;). We can then do something similar for factors of 5. This gives us a formula:&lt;br /&gt;
:&amp;lt;math&amp;gt;k = \min\left(\left\lfloor \frac{n}{2} \right\rfloor + \left\lfloor \frac{n}{4} \right\rfloor + \left\lfloor \frac{n}{8} \right\rfloor + ..., \left\lfloor \frac{n}{5} \right\rfloor + \left\lfloor \frac{n}{25} \right\rfloor + \left\lfloor \frac{n}{125} \right\rfloor + ...\right)&amp;lt;/math&amp;gt;&lt;br /&gt;
It is now fairly obvious by inspection that the second argument&amp;amp;mdash;the number of factors of 5&amp;amp;mdash;is always the smaller of the two.&lt;br /&gt;
&lt;br /&gt;
This formula can be used as-is, hence:&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    int n;&lt;br /&gt;
    scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);&lt;br /&gt;
    int p = 5;&lt;br /&gt;
    int k = 0;&lt;br /&gt;
    while (p &amp;lt;= n)&lt;br /&gt;
    {&lt;br /&gt;
        k += n/p;&lt;br /&gt;
        p *= 5;&lt;br /&gt;
    }&lt;br /&gt;
    printf(&amp;quot;There are %d zeroes at the end of %d!\n&amp;quot;, k, n);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
The formula can also be re-worked into the following form. The mathematical reasoning behind the correctness of this implementation is left as an exercise for the reader.&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
int main()&lt;br /&gt;
{&lt;br /&gt;
    int n;&lt;br /&gt;
    scanf(&amp;quot;%d&amp;quot;, &amp;amp;n);&lt;br /&gt;
    int k = 0;&lt;br /&gt;
    while (n &amp;gt; 0)&lt;br /&gt;
        k += n /= 5;&lt;br /&gt;
    printf(&amp;quot;There are %d zeroes at the end of %d!\n&amp;quot;, k, n);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Functional style (Haskell):&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;haskell&amp;quot;&amp;gt;&lt;br /&gt;
zeroes n = sum $ tail $ takeWhile (&amp;gt;0) (iterate (flip div 5) n)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Bases other than 10 may be trickier to handle. For example, consider the problem of finding the number of zeroes at the end of &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt; when expressed in base 12 (with prime factorization &amp;lt;math&amp;gt;2^2 \cdot 3&amp;lt;/math&amp;gt;). In this case we have to count factors of 2 and factors of 3, but it takes ''two'' factors of 2 to make one factor of 12, so we have to divide by two and then compare it with the number of factors of 3 (the smaller of the two results will be the answer). It's not obvious which factor will &amp;quot;run out&amp;quot; first, so it might be necessary to find the number of times ''each'' prime factor in the base appears in &amp;lt;math&amp;gt;n!&amp;lt;/math&amp;gt;. The algorithm shown above still works for each individual prime factor, though (provided that &amp;quot;5&amp;quot; is replaced with the appropriate number).&lt;br /&gt;
&lt;br /&gt;
==References==&lt;br /&gt;
&amp;lt;references/&amp;gt;&lt;/div&gt;</summary>
		<author><name>Brian</name></author>	</entry>

	</feed>