Counting trailing zeros of numbers resulted from factorial

后端 未结 10 1585
北荒
北荒 2020-11-30 07:48

I\'m trying to count trailing zeros of numbers that are resulted from factorials (meaning that the numbers get quite large). Following code takes a number, compute the facto

相关标签:
10条回答
  • 2020-11-30 08:29

    The double type has limited precision, so if the numbers you are working with get too big the double will be only an approximation. To work around this you can use something like BigInteger to make it work for arbitrarily large integers.

    0 讨论(0)
  • 2020-11-30 08:31

    My 2 cents: avoid to work with double since they are error-prone. A better datatype in this case is BigInteger, and here there is a small method that will help you:

    public class CountTrailingZeroes {
    
        public int countTrailingZeroes(double number) {
            return countTrailingZeroes(String.format("%.0f", number));
        }
    
        public int countTrailingZeroes(String number) {
            int c = 0;
            int i = number.length() - 1;
    
            while (number.charAt(i) == '0') {
                i--;
                c++;
            }
    
            return c;
    
        }
    
        @Test
        public void $128() {
            assertEquals(0, countTrailingZeroes("128"));
        }
    
        @Test
        public void $120() {
            assertEquals(1, countTrailingZeroes("120"));
        }
    
        @Test
        public void $1200() {
            assertEquals(2, countTrailingZeroes("1200"));
        }
    
        @Test
        public void $12000() {
            assertEquals(3, countTrailingZeroes("12000"));
        }
    
        @Test
        public void $120000() {
            assertEquals(4, countTrailingZeroes("120000"));
        }
    
        @Test
        public void $102350000() {
            assertEquals(4, countTrailingZeroes("102350000"));
        }
    
        @Test
        public void $1023500000() {
            assertEquals(5, countTrailingZeroes(1023500000.0));
        }
    }
    
    0 讨论(0)
  • 2020-11-30 08:32

    Java's doubles max out at a bit over 9 * 10 ^ 18 where as 25! is 1.5 * 10 ^ 25. If you want to be able to have factorials that high you might want to use BigInteger (similar to BigDecimal but doesn't do decimals).

    0 讨论(0)
  • 2020-11-30 08:33

    I had the same issue to solve in Javascript, and I solved it like:

    var number = 1000010000;
    var str = (number + '').split(''); //convert to string
    var i = str.length - 1; // start from the right side of the array
    var count = 0; //var where to leave result
    for (;i>0 && str[i] === '0';i--){
        count++;
    }
    console.log(count) // console shows 4
    

    This solution gives you the number of trailing zeros.

    var number = 1000010000;
    var str = (number + '').split(''); //convert to string
    var i = str.length - 1; // start from the right side of the	array
    var count = 0; //var where to leave result
    for (;i>0 && str[i] === '0';i--){
    	count++;
    }
    console.log(count)

    0 讨论(0)
  • 2020-11-30 08:35

    You only really need to know how many 2s and 5s there are in the product. If you're counting trailing zeroes, then you're actually counting "How many times does ten divide this number?". if you represent n! as q*(2^a)*(5^b) where q is not divisible by 2 or 5. Then just taking the minimum of a and b in the second expression will give you how many times 10 divides the number. Actually doing the multiplication is overkill.

    Edit: Counting the twos is also overkill, so you only really need the fives.

    And for some python, I think this should work:

    def countFives(n):
        fives = 0   
        m = 5
        while m <= n:
            fives = fives + (n/m)
            m = m*5
        return fives
    
    0 讨论(0)
  • 2020-11-30 08:38

    This is how I made it, but with bigger > 25 factorial the long capacity is not enough and should be used the class Biginteger, with witch I am not familiar yet:)

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner in = new Scanner(System.in);
        System.out.print("Please enter a number : ");
        long number = in.nextLong();
        long numFactorial = 1;
    
        for(long i = 1; i <= number; i++) {
            numFactorial *= i;
        }
        long result = 0;
        int divider = 5;
        for( divider =5; (numFactorial % divider) == 0; divider*=5) {
             result += 1;
        }
    
        System.out.println("Factorial of n is: " + numFactorial);
        System.out.println("The number contains " + result + " zeroes at its end.");
    
        in.close();
    
     }
    
    }
    
    0 讨论(0)
提交回复
热议问题