Carpet

Carpet

2M Downloads

Function `fact(x)` can potentially take a long time to execute for very large `x`

James103 opened this issue ยท 0 comments

commented

As of Carpet mod commit 47c8484, the function fact(x) can take a long time to execute for vary large values of x. This is because the function loops through all numbers from 1 to x, multiplying them through, even though fact(x) == Infinity for x > 170 due to the Java double limit.

for (int i = 1; i <= number; i++)
{
factorial = factorial * i;
}

To solve this, the function should check if its input exceeds the value 170, or alternatively if the factorial variable in the loop goes infinite, and if so, return Infinity immediately.