Reverse factorial

前端 未结 17 1453
南笙
南笙 2020-12-05 03:12

Well, we all know that if N is given it\'s easy to calculate N!. But what about the inverse?

N! is given and you are about to find N - Is that possible ? I\'m curio

17条回答
  •  無奈伤痛
    2020-12-05 03:48

    int inverse_factorial(int factorial){
        int current = 1;
        while (factorial > current) {
            if (factorial % current) {
                return -1; //not divisible
            }
            factorial /= current;
            ++current;
        }
        if (current == factorial) {
            return current;
        }
        return -1;
    }
    

提交回复
热议问题