Python Forum
Help making this code not TLE
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help making this code not TLE
#1

Im trying to solve this in Python 2.7 but it keeps saying TLE after case 1: https://dmoj.ca/problem/factorial
This is my code:
N=int(input())
x=[]
import sys

total = 0
def factorial(n):
    total = n
    while n > 1:
        total *= (n - 1)
        n -= 1
    return total


for i in range(0,N):
  c=int(input())
  x.append(c)
for i in range(0,N):
  print(((factorial(x[i]))%(2**32)))
Reply
#2
Okay, so there is a trick here.  You can't purely brute force this.

The trick is, after a certain point any factorial mod 2**32 is 0.
So if the number is bigger than that you just return 0 without doing any calculation.

Take a shot at figuring out what that number is.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Star I was making code for collision for my character izmamonke 2 2,095 Aug-06-2021, 04:30 PM
Last Post: izmamonke
  Making a code.py file to function, does not run hobbyist 6 2,928 Jan-27-2021, 07:50 AM
Last Post: DeaD_EyE
  Making new lines of code AvioxyYT 1 1,818 Jan-22-2021, 07:02 PM
Last Post: buran
  making the code easier, list comprehension go127a 2 2,078 May-26-2019, 06:19 PM
Last Post: Gribouillis
  [split] I need help making a four digit code cracker with random tycpytyt 1 2,562 Mar-24-2019, 05:53 PM
Last Post: micseydel
  Making a Easy Password/code system nmsturcke 4 3,859 Jul-09-2018, 02:50 AM
Last Post: ichabod801
  Need help making code more efficient dhbergen 5 3,900 Apr-17-2018, 01:00 AM
Last Post: dhbergen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020