📜  FCTRL2 (1)

📅  最后修改于: 2023-12-03 15:15:02.978000             🧑  作者: Mango

FCTRL2

Introduction

FCTRL2 is a code solution to the SPOJ problem with the same name. The problem requires the implementation of a program that can calculate the factorial of a given number. The task seems simple, but the problem expects the programmers to solve it efficiently, without overflow and in O(n) time complexity.

Code Explanation

The FCTRL2 program takes an integer input as the number whose factorial needs to be calculated and returns the factorial as output. The code uses an array to store the intermediate value of the factorial, with each element in the array representing a digit in the factorial.

def factorial(n):
    if n < 0:
        return "Invalid input"
    if n == 0 or n == 1:
        return 1

    res = [0] * 200
    res[0] = 1
    res_size = 1

    for i in range(2, n + 1):
        res_size = multiply(i, res, res_size)

    return ''.join(map(str, res[:res_size][::-1]))

The function factorial(n) checks if the input is valid and returns 1 if the input is 0 or 1. If the input is valid, the function declares an array "res" to store the intermediate values of the factorial. The array has a fixed size of 200 elements which is enough to store the maximum number of digits in the factorial of 100.

def multiply(x, res, res_size):
    carry = 0

    for i in range(res_size):
        prod = res[i] * x + carry
        res[i] = prod % 10
        carry = prod // 10

    while carry != 0:
        res[res_size] = carry % 10
        carry //= 10
        res_size += 1

    return res_size

To calculate the factorial of the input number, the program uses a helper function multiply(x, res, res_size) that multiplies the intermediate value of the factorial with the next number in the loop. The helper function takes three arguments: the current number in the loop (x), the array that stores the current value of the factorial (res), and the size of the array (res_size).

The helper function performs the multiplication of the current number (x) with each digit in the "res" array, starting from the least significant digit. If the multiplication result is more than 10, the carry is added to the next multiplication. The size of the "res" array grows whenever there is a carry-out at the most significant digit.

if __name__ == "__main__":
    t = int(input())

    for _ in range(t):
        n = int(input())
        print(factorial(n))

The main function gets the input number "t" as the number of test cases and calls the factorial(n) function for each test case. The code outputs the result of the function as a string.

Conclusion

The FCTRL2 program is an efficient solution to the SPOJ problem for calculating the factorial of an integer. The program performs the calculation in O(n) time complexity and uses an array to store the intermediate value of the factorial. The program can be easily modified to compute the factorial of large numbers by increasing the size of the array.