Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,181 questions

56,073 answers

573 users

How to compute the factorial of a number greater than 20 in Python

1 Answer

0 votes
"""
    This program computes the factorial of numbers greater than 20.
    Python's built‑in integer type automatically expands to arbitrary size,
    making it ideal for very large factorials.
"""

import math

def factorial_big(n: int) -> int:
    """
    Compute factorial using Python's built‑in math.factorial,
    which is highly optimized in C and handles very large integers.
    """
    return math.factorial(n)


def factorial_manual(n: int) -> int:
    """
    Manual factorial implementation using iterative multiplication.
    Demonstrates how Python naturally handles huge integers.
    """
    result = 1
    for i in range(2, n + 1):
        result *= i
    return result


def main() -> None:
    """
    Main entry point: read input, compute factorial, print result.
    """
    n = int(input("Enter a number greater than 20: "))

    # Use the optimized built‑in version
    result = factorial_big(n)

    print(f"\nFactorial of {n} is:\n")
    print(result)


if __name__ == "__main__":
    main()


"""
run:

Enter a number greater than 20: 25

Factorial of 25 is:

15511210043330985984000000

"""

 



answered 3 days ago by avibootz

Related questions

...