Python Code Improvements


(Ikki23) #1

Hi, this is my solution to ECOO (2005) Fractorial using Python 3. Here is the problem,

Is there any improvements that I can make? I’d appreciate any advice. Thanks! :slight_smile:
Here’s my Python 3 code:

prime_list = [2]
fractorial = {}


def prime_factor(n):
    prime_factorization = {}
    for j in range(len(prime_list)):
        while n % prime_list[j] == 0:
            n = n // prime_list[j]
            update_dict(prime_factorization, prime_list[j])
    if len(prime_factorization) == 0:
        update_dict(prime_factorization, n)
        prime_list.append(n)

    return prime_factorization


def update_dict(dict, n):
    if n in dict:
        dict[n] += 1
    else:
        dict[n] = 1


def main():
    number = int(input())
    if number == 1:
        print("Fractorial(1) = 1")
    else:
        for i in range(2, number + 1):
            prime_factorization = prime_factor(i)
            for key in prime_factorization:
                if key in fractorial:
                    if prime_factorization[key] > fractorial[key]:
                        fractorial[key] = prime_factorization[key]
                else:
                    fractorial[key] = prime_factorization[key]

        fractorial_value = 1
        for key in fractorial:
            fractorial_value *= key ** fractorial[key]
        print(f"Fractorial({number}) = {fractorial_value}")


main()