Advertisement
Guest User

tt

a guest
Jan 23rd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.81 KB | None | 0 0
  1. int lastDigitDiffZero(long n) {
  2.     long a = fact(n);
  3.     if(a < 10000) {
  4.         long last= a % 10;
  5.         while (a % 10 == 0) {
  6.             a /= 10;
  7.             last =a%10;
  8.         }
  9.         return (int)last;
  10.     }
  11.     BigInteger f = factorial(n);
  12.     BigInteger b2 = new BigInteger("10");
  13.     BigInteger b3 = BigInteger.ZERO;
  14.    
  15.    while (f.mod(new BigInteger("10")).equals(BigInteger.ZERO)) {
  16.         f = f.divide(b2);
  17.         b3 = f.mod(new BigInteger("10"));
  18.     }
  19.     return b3.intValue();
  20. }
  21.  
  22. BigInteger factorial(long n) {
  23.     BigInteger result = BigInteger.ONE;
  24.     for (; n > 1; n--) {
  25.         result = result.multiply(BigInteger.valueOf(n));
  26.     }
  27.     return result;
  28. }
  29.  
  30. int fact(long n) {
  31.     int f = 0;
  32.     for (int c = 1; c <= n; c++) {
  33.             f *= c;
  34.     }
  35.     return f;
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement