为什么我的递归程序会打印一个负数?

时间:2021-11-29 01:42:36

My code for whatever reason is printing out a negative number when i run it with certain numbers(17). It is supposed to find the factorial of a number and print it out however clearly that isn't happening.

我的代码无论出于什么原因,当我使用某些数字运行它时会打印出一个负数(17)。它应该找到一个数字的阶乘,然后将其打印出来然后清楚地表明它没有发生。

package recursion;

public class recursion_1 {

public static void main(String[] args) {
int x = factorial(17);
System.out.println(x);
}
public static int factorial(int N) { 
       if (N == 1) return 1; 
       return N * factorial(N-1); 
    }   
}

3 个解决方案

#1


2  

You're encountering integer overflow.

你遇到了整数溢出。

factorial(17) is 3.5568743e+14, which is well beyond the bounds of int. When an integer operation overflows, it can end up negative. For example:

factorial(17)是3.5568743e + 14,远远超出了int的界限。当整数运算溢出时,它可能最终为负数。例如:

int x = Integer.MAX_VALUE;
x++;
System.out.println(x); // Very large negative number

In your case, you'll have overflowed several times - even if the result were positive, it still wouldn't be right.

在你的情况下,你会溢出几次 - 即使结果是积极的,它仍然是不对的。

If you need integers in the range of [-263, 263-1] you can use long instead of int. If you want arbitrarily large integers, use BigInteger instead. For example:

如果需要[-263,263-1]范围内的整数,可以使用long而不是int。如果您想要任意大整数,请改用BigInteger。例如:

// Note rename of parameter to follow Java conventions
public static BigInteger factorial(int n) {
    return factorial(BigInteger.valueOf(n));
}

public static BigInteger factorial(BigInteger n) {
    if (n.equals(BigInteger.ONE)) {
        return BigInteger.ONE;
    }
    return n.multiply(n.subtract(BigInteger.ONE));
}

#2


1  

Factorials grow quickly in value, such that 17! (355687428096000) too large to fit in an int, causing overflow and the negative number.

因子的价值迅速增长,这样17! (355687428096000)太大而无法放入int,导致溢出和负数。

Return a long from factorial, so that when the multiplication occurs, it won't overflow (yet). You'll need to declare x as a long also. Note that this will only postpone the problem, because sufficiently high values of N will overflow a long too. If necessary, use BigIntegers.

从阶乘返回一个long,这样当乘法发生时,它不会溢出(还)。你还需要将x声明为long。请注意,这只会推迟问题,因为足够高的N值也会溢出很长时间。如有必要,请使用BigIntegers。

#3


0  

This is because the maximum value an int can have is 2,147,483,647. 17! exceeds this number. If an integer is assigned a number bigger than its maximum size, it starts counting up from -2,147,483,647.

这是因为int可以具有的最大值是2,147,483,647。 17!超过这个数字。如果为整数分配的数字大于其最大大小,则从-2,147,483,647开始计数。

2,147,483,647 + 1 = -12,147,483,647

Try a long or BigDecimal instead =)

尝试使用long或BigDecimal =)

#1


2  

You're encountering integer overflow.

你遇到了整数溢出。

factorial(17) is 3.5568743e+14, which is well beyond the bounds of int. When an integer operation overflows, it can end up negative. For example:

factorial(17)是3.5568743e + 14,远远超出了int的界限。当整数运算溢出时,它可能最终为负数。例如:

int x = Integer.MAX_VALUE;
x++;
System.out.println(x); // Very large negative number

In your case, you'll have overflowed several times - even if the result were positive, it still wouldn't be right.

在你的情况下,你会溢出几次 - 即使结果是积极的,它仍然是不对的。

If you need integers in the range of [-263, 263-1] you can use long instead of int. If you want arbitrarily large integers, use BigInteger instead. For example:

如果需要[-263,263-1]范围内的整数,可以使用long而不是int。如果您想要任意大整数,请改用BigInteger。例如:

// Note rename of parameter to follow Java conventions
public static BigInteger factorial(int n) {
    return factorial(BigInteger.valueOf(n));
}

public static BigInteger factorial(BigInteger n) {
    if (n.equals(BigInteger.ONE)) {
        return BigInteger.ONE;
    }
    return n.multiply(n.subtract(BigInteger.ONE));
}

#2


1  

Factorials grow quickly in value, such that 17! (355687428096000) too large to fit in an int, causing overflow and the negative number.

因子的价值迅速增长,这样17! (355687428096000)太大而无法放入int,导致溢出和负数。

Return a long from factorial, so that when the multiplication occurs, it won't overflow (yet). You'll need to declare x as a long also. Note that this will only postpone the problem, because sufficiently high values of N will overflow a long too. If necessary, use BigIntegers.

从阶乘返回一个long,这样当乘法发生时,它不会溢出(还)。你还需要将x声明为long。请注意,这只会推迟问题,因为足够高的N值也会溢出很长时间。如有必要,请使用BigIntegers。

#3


0  

This is because the maximum value an int can have is 2,147,483,647. 17! exceeds this number. If an integer is assigned a number bigger than its maximum size, it starts counting up from -2,147,483,647.

这是因为int可以具有的最大值是2,147,483,647。 17!超过这个数字。如果为整数分配的数字大于其最大大小,则从-2,147,483,647开始计数。

2,147,483,647 + 1 = -12,147,483,647

Try a long or BigDecimal instead =)

尝试使用long或BigDecimal =)