title: Least significant non-zero digit of a factorial
body: <p>I am trying to calculate the least significant non-zero digit in a factorial.</p>
  
  <hr>
  
  <p><strong>I have the following snippet :</strong> </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>$(document).ready(function() {
    $('#submit').click(function() {
      var n = $('#number').val();
      get_result(n);
    });
  });
  
  function get_result(n) {
    var factorial = 1;
    var factorial2 = 1;
    for (i = 1; i &lt;= n; i++) {
      factorial = factorial * i;
    }
    var count_5 = 0;
    for (j = 1; j &lt;= n; j++) {
      if (j % 5 != 0) {
        factorial2 = factorial2 * (j % 10);
        factorial2 = factorial2 % 10;
      } else if (j % 5 == 0) {
        count_5 = 1;
      }
    }
    if (count_5 == 1) {
      factorial2 = factorial2 * 5;
    }
    console.log(factorial2);
    factorial2 = factorial2.toString();
    var digit = 0;
    for (i = 0; i &lt; factorial2.length; i++) {
      if (factorial2[i] != '0') {
        digit = factorial2[i];
      }
    }
    $('#display').text("Factorial of " + n + " is " + factorial);
    $('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;div id="display"&gt;
  
  &lt;/div&gt;
  &lt;div id="display2"&gt;
  
  &lt;/div&gt;
  &lt;input type="text" value="" id="number"&gt;
  &lt;input type="submit" id="submit"&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>As part of the above code, to calculate the least significant non-zero digit, I am firstly ignoring all multiples of 5, and secondly, at each step of the factorial calculation,I am taking the remainder of factorial2 from 10 so as to only retain the non-zero digit at each step of the computation.Eventually, I multiply the final value of <code>factorial2</code> with 5 and then convert it to a string and find the last occurrence of a non-zero digit in the string.</p>
  
  <p>The above code seems to work fine for values of n=1,2........,8. But at n=9, the code returns the least significant non-zero digit as 3 whereas it should be returning 8.</p>
  
  <blockquote>
    <p>For example : Factorial(9) = 362880 , hence least significant non-zero digit = 8.</p>
  </blockquote>
  
  <p>What could the error be and how should I go about correcting it ? And is there another better performing method to compute this result ?</p>
  
  <blockquote>
    <p><strong>Note :</strong> I've included the code to calculate the factorial just for verification purposes, my ultimate aim is to just calculate the least significant non-zero digit and not the factorial for a worst possible case when n is a billion(when actually computing and reading the factorial is not feasible or advisable).</p>
  </blockquote>