Search code examples
javascriptjqueryhtmlperformancefactorial

Least significant non-zero digit of a factorial


I am trying to calculate the least significant non-zero digit in a factorial.


I have the following snippet :

$(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 <= n; i++) {
    factorial = factorial * i;
  }
  var count_5 = 0;
  for (j = 1; j <= 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 < 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);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="display">

</div>
<div id="display2">

</div>
<input type="text" value="" id="number">
<input type="submit" id="submit">

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 factorial2 with 5 and then convert it to a string and find the last occurrence of a non-zero digit in the string.

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.

For example : Factorial(9) = 362880 , hence least significant non-zero digit = 8.

What could the error be and how should I go about correcting it ? And is there another better performing method to compute this result ?

Note : 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).


Solution

  • The problem is that 5's don't simply disappear. They combine with a 2 to create a 0. So you'll have problems after multiples of 5 (like 15 or 35) or after numbers with a lot of powers of 2 (like 24). The best thing might be to keep count of the number of 2's, and decrease that for each multiple of 5 (there's always more 2's than 5's). (Also, once you've gone to the trouble of finding the number without a 0, you don't need to convert it to a string.)

    $(document).ready(function() {
      $('#submit').click(function() {
        var n = $('#number').val();
        get_result(n);
      });
    });
    
    function get_result(n) {
      var factorial = 1;
      var factorial2 = 1;
      for ( var i = 1; i <= n; i++ ) {
        factorial = factorial * i;
      }
      var extra2s = 0;
      for ( var j = 1; j <= n; j++ ) {
        var jcopy = j;
        while( jcopy%10 == 0 ) {
          jcopy /= 10;
        }
        while( jcopy%2==0 ) {
          extra2s++;
          jcopy /= 2;
        }
        while( jcopy%5==0 ) {
          extra2s--;
          jcopy /= 5;
        }
        jcopy %= 10;
        factorial2 = (factorial2 * jcopy)%10;
      }
      for ( var k = 0 ; k < extra2s ; k++ ) {
        factorial2 = (factorial2 * 2)%10;
      }
      var digit = factorial2;
      $('#display').text("Factorial of " + n + " is " + factorial);
      $('#display2').text("Least significant digit of Factorial of " + n + " is " + digit);
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <div id="display">
    
    </div>
    <div id="display2">
    
    </div>
    <input type="text" value="" id="number">
    <input type="submit" id="submit">