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
  New to Python - Not sure why this code isn't working - Any help appreciated TheGreatNinx 4 952 Jul-22-2023, 10:21 PM
Last Post: Pedroski55
  code not working when executed from flask app ThomasDC 1 872 Jul-18-2023, 07:16 AM
Last Post: ThomasDC
  extracting x from '(x)' as an integer number Hwolff1962 3 882 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,001 Dec-19-2022, 05:24 PM
Last Post: kartheekdas
  help me simple code result min and max number abrahimusmaximus 2 900 Nov-12-2022, 07:52 AM
Last Post: buran
Exclamation My code is not working as I expected and I don't know why! Marinho 4 1,058 Oct-13-2022, 08:09 PM
Last Post: deanhystad
  Does this code need to be so long? duckredbeard 4 882 Sep-27-2022, 09:36 AM
Last Post: ibreeden
  float("{:.2f}" return long number? korenron 2 1,018 Jul-13-2022, 05:35 AM
Last Post: korenron
  My Code isn't working... End3r 4 1,914 Mar-21-2022, 10:12 AM
Last Post: End3r
  I don't undestand why my code isn't working. RuyCab 2 1,977 Jun-17-2021, 03:06 PM
Last Post: RuyCab

Forum Jump:

User Panel Messages

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