{"id":2376,"date":"2025-03-04T23:27:19","date_gmt":"2025-03-04T14:27:19","guid":{"rendered":"https:\/\/remoooo.com\/?p=2376"},"modified":"2025-03-04T23:27:21","modified_gmt":"2025-03-04T14:27:21","slug":"leetcode4-mar-2025-count-trailing-zeros-in-factorial","status":"publish","type":"post","link":"https:\/\/remoooo.com\/en\/leetcode4-mar-2025-count-trailing-zeros-in-factorial\/","title":{"rendered":"[Leetcode]4 Mar. 2025. Count Trailing Zeros in Factorial"},"content":{"rendered":"<h2 class=\"wp-block-heading\">Description:<\/h2>\n\n\n\n<p>Given an integer $n$ , return the number of trailing zeros in $n!$ (n factorial).<\/p>\n\n\n\n<p>Factorial is defined as:<br>$$<br>n! = n \\times (n \u2013 1) \\times (n \u2013 2) \\times \\dots \\times 3 \\times 2 \\times 1<br>$$<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Examples:<\/h2>\n\n\n\n<p>Example 1:<br>Input: <code>n = 3<\/code><br>Output: <code>0<\/code><br>Explanation: <code>3! = 6<\/code>, which has no trailing zeros.<\/p>\n\n\n\n<p>Example 2:<br>Input: <code>n = 5<\/code><br>Output: <code>1<\/code><br>Explanation: <code>5! = 120<\/code>, which has one trailing zero.<\/p>\n\n\n\n<p>Example 3:<br>Input: <code>n = 0<\/code><br>Output: <code>0<\/code><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Constraints:<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>$0 \\leq n \\leq 10^4$<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro padding-bottom-disabled\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:block;padding:16px 0 0 16px;margin-bottom:-1px;width:100%;text-align:left;background-color:#1E1E1E\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"54\" height=\"14\" viewbox=\"0 0 54 14\"><g fill=\"none\" fill-rule=\"evenodd\" transform=\"translate(1 1)\"><circle cx=\"6\" cy=\"6\" r=\"6\" fill=\"#d4d4d433\" stroke=\"#d4d4d44d\" stroke-width=\".5\"><\/circle><circle cx=\"26\" cy=\"6\" r=\"6\" fill=\"#d4d4d433\" stroke=\"#d4d4d44d\" stroke-width=\".5\"><\/circle><circle cx=\"46\" cy=\"6\" r=\"6\" fill=\"#d4d4d433\" stroke=\"#d4d4d44d\" stroke-width=\".5\"><\/circle><\/g><\/svg><\/span><span role=\"button\" tabindex=\"0\" data-code=\"class Solution {public:    int trailingZeroes(int n) {        int count;        while(n &gt;= 5){            n = n \/ 5;            count += n;        }        return count;    }};\/*## Counting 5 as a factor.We Need To Determine how many times &quot;10&quot; appears as a factor in the product.10 is formed by multiplying 2 &amp; 5, we can only count 5 as a factor.eg\uff1a- 3! = 3*2*1 = 6 -&gt; +0- 5! = 5*4*3*2*1 = 120 -&gt; +1- 10! = 10*9*...*3*2*1 = 3628800 -&gt; +2*\/\" style=\"color:#D4D4D4;display:none\" aria-label=\"copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewbox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2\"><\/path><\/svg><\/span><pre class=\"shiki dark-plus\" style=\"background-color: #1E1E1E\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #569CD6\">class<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #4EC9B0\">Solution<\/span><span style=\"color: #D4D4D4\"> {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #569CD6\">public:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #DCDCAA\">trailingZeroes<\/span><span style=\"color: #D4D4D4\">(<\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> <\/span><span style=\"color: #9CDCFE\">n<\/span><span style=\"color: #D4D4D4\">) {<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #569CD6\">int<\/span><span style=\"color: #D4D4D4\"> count;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">while<\/span><span style=\"color: #D4D4D4\">(n &gt;= <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">){<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            n = n \/ <\/span><span style=\"color: #B5CEA8\">5<\/span><span style=\"color: #D4D4D4\">;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">            count += n;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">        <\/span><span style=\"color: #C586C0\">return<\/span><span style=\"color: #D4D4D4\"> count;<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">    }<\/span><\/span>\n<span class=\"line\"><span style=\"color: #D4D4D4\">};<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">\/*<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">## Counting 5 as a factor.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">We Need To Determine how many times &quot;10&quot; appears as a factor in the product.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">10 is formed by multiplying 2 &amp; 5, we can only count 5 as a factor.<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">For example:<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">- 3! = 3*2*1 = 6 -&gt; +0<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">- 5! = 5*4*3*2*1 = 120 -&gt; +1<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">- 10! = 10*9*...*3*2*1 = 3628800 -&gt; +2<\/span><\/span>\n<span class=\"line\"><span style=\"color: #6A9955\">*\/<\/span><\/span><\/code><\/pre><span style=\"display:flex;align-items:flex-end;padding:10px;width:100%;justify-content:flex-end;background-color:#1E1E1E;color:#c7c7c7;font-size:12px;line-height:1;position:relative\">C++<\/span><\/div>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Description: Given an integer $n$ , return the number o [&hellip;]<\/p>","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2376","post","type-post","status-publish","format-standard","hentry","category-article"],"_links":{"self":[{"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/posts\/2376","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/comments?post=2376"}],"version-history":[{"count":1,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/posts\/2376\/revisions"}],"predecessor-version":[{"id":2377,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/posts\/2376\/revisions\/2377"}],"wp:attachment":[{"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/media?parent=2376"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/categories?post=2376"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/remoooo.com\/en\/wp-json\/wp\/v2\/tags?post=2376"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}