Number of Digits in n!: Coding a Program for 500! Factorial

  • Thread starter Gagan A
  • Start date
In summary, to find the correct number of digits in n!, one can use a for loop to calculate the value of log(n!) and then add 1 to the truncated sum. Using a program like Mathematica or Python, which supports arbitrary-precision arithmetic, allows for the accurate calculation of large factorials like 500!. Using Stirling's series or other mathematical techniques can also provide an alternative method for finding the number of digits in n!.
  • #1
Gagan A
20
0
I have to code a program in C that will give the correct number of digits of n! where n is upto 500.
I thought this way:
Get the value of log(n!) by using a for loop. (like log1 + log2 + log3... upto logn, log is to the base 10). Now the final answer will be (int)sum + 1. If I give 500! factorial the answer comes out to be 1135 acc. to my program, but the answer given is 1133. Am I right?
 
Technology news on Phys.org
  • #2
I also get 1135.
 
  • #3
500! =

Code:
122013682599111006870123878542304692625357434280319284219241358838584537315388\
199760549644750220328186301361647714820358416337872207817720048078520515932928\
547790757193933060377296085908627042917454788242491272634430567017327076946106\
280231045264421887878946575477714986349436778103764427403382736539747138647787\
849543848959553753799042324106127132698432774571554630997720278101456108118837\
370953101635632443298702956389662891165897476957208792692887128178007026517450\
776841071962439039432253642260523494585012991857150124870696156814162535905669\
342381300885624924689156412677565448188650659384795177536089400574523894033579\
847636394490531306232374906644504882466507594673586207463792518420045936969298\
102226397195259719094521782333175693458150855233282076282002340262690789834245\
171200620771464097945611612762914595123722991334016955236385094288559201872743\
379517301458635757082835578015873543276888868012039988238470215146760544540766\
353598417443048012893831389688163948746965881750450692636533817505547812864000\
000000000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000

This has 1135 digits.

- Warren
 
  • #4
I too verified with the teacher, the answer is 1135. But how in heaven did you calculate the value of 500!, chroot? What is the maximum precision of the calculator you used?
 
  • #5
Gagan A said:
I too verified with the teacher, the answer is 1135. But how in heaven did you calculate the value of 500!, chroot? What is the maximum precision of the calculator you used?
I think he used Methematica. It can handle most mathematical operations quite well.
It can give you the evaluation of 500!, say, in less than 1 second. :rolleyes:
------------------
The reason you got the wrong answer may be the rounding errors of the programme.
 
  • #6
Gagan A,

I did indeed use Mathematica (VietDao29 probably recognized the tell-tale "\" characters at the end of each line in my output).

It uses arbitrary-precision arithmetic, so the maximum precision is that afforded by the entire memory of my computer (2 GB on this one). 500! is computed, indeed, in the blink of an eye, and probably only uses a few kilobytes of memory.

- Warren
 
  • #7
Yes Mathemathica really wotks great for calculations like this. But there are other good way's of doing it as well. I have in the past also did stuff like this with the Smalltalk programming language (it isn't very good for doing mathemathics but it does enable arbitrary-precision arithmetic) which was also qouit fast. I once also made a programm in C that could work with large integers (it used strings) but it wasn't veary fast.
 
  • #8
It may be 'cheating' within the context of your course (if they want you to do this a certain way), but you might want to look up Stirling's series. You can calculate the log of gamma (and hence the factorial) to the necessary accuracy to find the number of digits without resorting to a summation.
 
  • #9
Python also supports arbitrary length integers:
Code:
>>> def fac(a):
    if a==1: return a
    return a * fac(a-1)
>>> len( str( fac( 500)))
1135
 

1. How do I calculate the number of digits in n! factorial for a specific number?

To calculate the number of digits in n! factorial, you can use the formula log(n!) = n*log(n) - n + 1. This will give you the number of digits in n! factorial without having to manually calculate the entire factorial.

2. What is the maximum number of digits in n! factorial?

The maximum number of digits in n! factorial is equal to n, where n is the number for which you are calculating the factorial. For example, the maximum number of digits in 500! factorial is 500.

3. How do I code a program to calculate the number of digits in n! factorial?

To code a program for calculating the number of digits in n! factorial, you can use a loop to multiply the numbers from 1 to n and then use the log formula mentioned above to determine the number of digits. You can also use built-in functions in programming languages such as Python or Java to calculate the factorial and then use the log function to determine the number of digits.

4. Can I use a calculator to find the number of digits in n! factorial?

Yes, you can use a calculator to find the number of digits in n! factorial. However, you will need to use the log formula mentioned above to calculate the number of digits as most calculators have a limit on the number of digits they can display.

5. Why is the number of digits in n! factorial important?

The number of digits in n! factorial can provide insights into the magnitude of the factorial and can be useful in various mathematical and scientific calculations. It can also be used to determine the number of trailing zeros in the factorial, which has applications in number theory and combinatorics. Additionally, the number of digits in the factorial can also be used to analyze the efficiency of algorithms in computer science.

Similar threads

  • Programming and Computer Science
Replies
2
Views
308
  • General Math
Replies
3
Views
1K
  • Programming and Computer Science
Replies
1
Views
750
  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
5
Views
22K
  • Programming and Computer Science
Replies
4
Views
735
  • Programming and Computer Science
Replies
10
Views
1K
Replies
11
Views
2K
Back
Top