0

The answer returned by the following Java code is 0. Can anyone help me find the error?

public class ComplexityOrder {    
    public static void main(String[] args) {    
        ComplexityOrder co = new ComplexityOrder();
        co.Order(1000);   
    }

    public double Order(int n) {
        int[] a = new int[10];
        a[0] = Fact(n);
        System.out.println("Factorial " + a[0]);
        return a[0];    
    }

    public static int Fact(int n) {
        if (n == 0 || n ==1) {
            return 1;
        } else {
            return n * Fact(n - 1);
        }
    }    
}
pjs
  • 18,696
  • 4
  • 27
  • 56

3 Answers3

1

The max value int can contain is 2^32 and 1000! is too big for int to contain it. You can use java.math.BigInteger for the purpose. The BigInteger class allocates as much memory as it needs to hold all the bits of data it is asked to hold. There are, however, some practical limits, dictated by the memory available.

Using BigInteger your code will somewhat look like:

import java.math.BigInteger;

public class ComplexityOrder {  

    public static void main(String[] args) {    
        ComplexityOrder co = new ComplexityOrder();
        co.Order(1000);   
    }

    public BigInteger Order(int n) {
        BigInteger[] a = new BigInteger[10];
        a[0] = fact(n);
        System.out.println("Factorial " + a[0]);
        return a[0];    
    }

    public static BigInteger fact(int n) {
        if (n == 0 || n ==1) {
            return BigInteger.ONE;
        } else {
            return fact(n-1).multiply(BigInteger.valueOf(n));
        }
    }    
}

Also, I don't see any point using the array.

Karan Khanna
  • 1,947
  • 3
  • 21
  • 49
0

that is because of the overflow of int variable that maximum contain number = 2^32 , and Fact(1000) is more than Max int, if you don't acquire numbers leas than 100 you can use BigInteger class instead of int , if you acquire big numbers you have to implement your string addition function to avoid overflow .

mhassan
  • 11
  • 3
0

To be more specific ...

You are using standard integers, an n-bit signed binary number. You then compute 1000! This is a very large number compared to any standard integer representation. The prime factorization includes 2^994. This means that the resulting number, in binary, ends with a string of 994 zeroes.

When integer overflow isn't handled as an exception, the condition is a highly informal way of reducing your result mod 2^n, where n is the length of the internal representation, usually 32 or 64 bits, and then mapping the higher half of the range to negative numbers. A number that ends in at least n zeroes will get reduced to 0 (mod 2^n). That's what happened in your case, as your computer does not have 1024-bit integers. :-)

As others have already suggested, you can handle this capacity by switching to BigInteger and adjusting your class to deal with the expanded range. Do note that it will be much slower, as you are beyond the hardware's native integer range, and the processing resembles doing all operations by hand in base 2^n. "Write down the 00110111001010010110110001010110, carry the 1, and on to the next column." :-)

Prune
  • 76,765
  • 14
  • 60
  • 81