Why does Java think that the product of all numbers from 10 to 99 is 0?

前端 未结 9 1490
悲哀的现实
悲哀的现实 2020-12-02 05:56

The following block of codes gives the output as 0.

public class HelloWorld{

    public static void main(String []args){
        int product = 1;
        fo         


        
相关标签:
9条回答
  • 2020-12-02 06:59

    Somewhere in the middle you get 0 as the product. So, your entire product will be 0.

    In your case :

    for (int i = 10; i < 99; i++) {
        if (product < Integer.MAX_VALUE)
            System.out.println(product);
        product *= i;
    }
    // System.out.println(product);
    
    System.out.println(-2147483648 * EvenValueOfi); // --> this is the culprit (Credits : Kocko's answer )
    
    O/P :
    1
    10
    110
    1320
    17160
    240240
    3603600
    57657600
    980179200
    463356416
    213837312
    -18221056
    -382642176
    171806720
    -343412736
    348028928
    110788608
    -1414463488
    464191488
    112459776
    -1033633792
    -944242688
    793247744
    -385875968
    150994944
    838860800
    -704643072
    402653184
    2013265920
    -805306368
    -1342177280  --> Multiplying this and the current value of `i` will also give -2147483648 (INT overflow)
    -2147483648  --> Multiplying this and the current value of `i` will also give -2147483648 (INT overflow)
    
    -2147483648  ->  Multiplying this and the current value of 'i' will give 0 (INT overflow)
    0
    0
    0
    

    Every time you multiply the current value of i with the number you get 0 as output.

    0 讨论(0)
  • 2020-12-02 07:02

    It's because of integer overflow. When you multiply many even numbers together, the binary number gets a lot of trailing zeroes. When you have over 32 trailing zeroes for an int, it rolls over to 0.

    To help you visualize this, here are the multiplications in hex calculated on a number type that won't overflow. See how the trailing zeroes slowly grow, and note that an int is made up of the last 8 hex-digits. After multiplying by 42 (0x2A), all 32 bits of an int are zeroes!

                                         1 (int: 00000001) * 0A =
                                         A (int: 0000000A) * 0B =
                                        6E (int: 0000006E) * 0C =
                                       528 (int: 00000528) * 0D =
                                      4308 (int: 00004308) * 0E =
                                     3AA70 (int: 0003AA70) * 0F =
                                    36FC90 (int: 0036FC90) * 10 =
                                   36FC900 (int: 036FC900) * 11 =
                                  3A6C5900 (int: 3A6C5900) * 12 =
                                 41B9E4200 (int: 1B9E4200) * 13 =
                                4E0CBEE600 (int: 0CBEE600) * 14 =
                               618FEE9F800 (int: FEE9F800) * 15 =
                              800CE9315800 (int: E9315800) * 16 =
                             B011C0A3D9000 (int: 0A3D9000) * 17 =
                            FD1984EB87F000 (int: EB87F000) * 18 =
                          17BA647614BE8000 (int: 14BE8000) * 19 =
                         25133CF88069A8000 (int: 069A8000) * 1A =
                        3C3F4313D0ABB10000 (int: ABB10000) * 1B =
                       65AAC1317021BAB0000 (int: 1BAB0000) * 1C =
                      B1EAD216843B06B40000 (int: 06B40000) * 1D =
                    142799CC8CFAAFC2640000 (int: C2640000) * 1E =
                   25CA405F8856098C7B80000 (int: C7B80000) * 1F =
                  4937DCB91826B2802F480000 (int: 2F480000) * 20 =
                 926FB972304D65005E9000000 (int: E9000000) * 21 =
               12E066E7B839FA050C309000000 (int: 09000000) * 22 =
              281CDAAC677B334AB9E732000000 (int: 32000000) * 23 =
             57BF1E59225D803376A9BD6000000 (int: D6000000) * 24 =
            C56E04488D526073CAFDEA18000000 (int: 18000000) * 25 =
          1C88E69E7C6CE7F0BC56B2D578000000 (int: 78000000) * 26 =
         43C523B86782A6DBBF4DE8BAFD0000000 (int: D0000000) * 27 =
        A53087117C4E76B7A24DE747C8B0000000 (int: B0000000) * 28 =
      19CF951ABB6C428CB15C2C23375B80000000 (int: 80000000) * 29 =
     4223EE1480456A88867C311A3DDA780000000 (int: 80000000) * 2A =
    AD9E50F5D0B637A6610600E4E25D7B00000000 (int: 00000000)
    
    0 讨论(0)
  • 2020-12-02 07:02

    Eventually, the calculation overflows, and eventually that overflow leads to a product of zero; that happens when product == -2147483648 and i == 42. Try this code out to verify it for yourself (or run the code here):

    import java.math.BigInteger;
    
    class Ideone {
        public static void main (String[] args) throws java.lang.Exception {
            System.out.println("Result: " + (-2147483648 * 42));
        }
    }
    

    Once it's zero, it of course stays zero. Here's some code that will produce a more accurate result (you can run the code here):

    import java.math.BigInteger;
    
    class Ideone {
        public static void main (String[] args) throws java.lang.Exception {
            BigInteger p = BigInteger.valueOf(1);
            BigInteger start = BigInteger.valueOf(10);
            BigInteger end = BigInteger.valueOf(99);
            for(BigInteger i = start; i.compareTo(end) < 0; i = i.add(BigInteger.ONE)){
                p = p.multiply(i);
                System.out.println("p: " + p);
            }
            System.out.println("\nProduct: " + p);
        }
    }
    
    0 讨论(0)
提交回复
热议问题