Python Forum
Factorial Code is not working when the given number is very long integer
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Factorial Code is not working when the given number is very long integer
#1
Hi,

Below code is to find "Factorial of a given number"

n=int(input("enter a end range: "))
s=1
for i in range(1,n+1):
    s=s*i
print(s)
for smaller n values code returns correct output. for big numbers (ex: 99 / 999 / 9999) it gives wrong output. that means, The result in output ends with zeros.
Ex: n = 99
Output: (which is wrong output) 933262154439441526816992388562667004907159682643816214685929638952175999932299156089414639761565182862536979208272237582511852109168640000000000000000000000

If n is very long integer i.e. n=100000 (1 Lakh), then output will get struct. we never get output.

What my query is, If I use any long integer ("trillion" or more), I should get the correct result in a very short time with no trouble. Please share tips on this issue.
Reply
#2
You perhaps want to look up the term "dynamic programming", which is a way to solve problems where you can store and reuse solutions to subproblems.
Reply
#3
What makes you think this isn't working? The value you display for 99! is the value I get which is the value I see for 99 in factorial tables (ok, I only checked the first 10 digits, the last 10 non zero digits, and counted the zeros). Anything above 5! accumulates zero at the end because 4*5 is 20 and anything times zero is zero. Large numbers are going to take a long time not only because there is a lot of multiplication, but because the multiplication is beyond the native ability of the processor on your machine. Then there is the time it takes to print the output. The output for 100000! is 5700 lines long!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  code isnt working. nezercat33 1 581 Mar-14-2025, 03:45 AM
Last Post: deanhystad
  Simple code not working properly tmv 2 475 Feb-28-2025, 09:27 PM
Last Post: deanhystad
  I'm new to Python - can someone help with this code as it is not working? lminc123 1 483 Feb-13-2025, 06:13 PM
Last Post: lminc123
  my code is not working erTurko 1 626 Nov-11-2024, 08:43 AM
Last Post: buran
  Printing the code line number arbiel 6 1,561 Jun-30-2024, 08:01 AM
Last Post: arbiel
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 2,272 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 3,026 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  extracting x from '(x)' as an integer number Hwolff1962 3 1,658 Mar-23-2023, 02:50 PM
Last Post: Hwolff1962
  I am new to python and Could someone please explain how this below code is working? kartheekdas 2 1,758 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  help me simple code result min and max number abrahimusmaximus 2 1,658 Nov-12-2022, 07:52 AM
Last Post: buran

Forum Jump:

User Panel Messages

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