factorial : Java Glossary

*0-9ABCDEFGHIJKLMNOPQRSTUVWXYZ (all)

factorial
Factorial is product of an integer and all smaller integers. It is denoted with an exclamation mark, e. g. 4! is 4 * 3 * 2 * 1 = 24.

Factorial methods are often written recursively just to show off. Iterative coding with a pedestrian for loop is much faster. Mathematicians like to use a powerful theorem proving technique called induction that works much like recursion, so they are enamoured of it and tend to recursion in Java where it is not warranted.

Recursive Calculation

Iterative Calculation

Table Lookup

Since you can’t fit n! into a long for n>20, you might as well use table lookup for a very fast factorial:

Stirling Approximation

Unfortunately, you can only calculate factorial of integers 20 or smaller, even with long arithmetic. To handle bigger ones you must use Stirling’s approximation or a Gamma function. For hints on faster algorithms you might use to calculate large factorials see Peter’s Math Pages.

Factorials 1 to 30

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5_040
8! = 40_320
9! = 362_880
10! = 3_628_800
11! = 39_916_800
12! = 479_001_600
13! = 6_227_020_800
14! = 87_178_291_200
15! = 1_307_674_368_000
16! = 20_922_789_888_000
17! = 355_687_428_096_000
18! = 6_402_373_705_728_000
19! = 121_645_100_408_832_000
20! = 2_432_902_008_176_640_000
21! = 51_090_942_171_709_440_000
22! = 1_124_000_727_777_607_680_000
23! = 2_585_2016_738_884_976_640_000
24! = 620_448_401_733_239_439_360_000
25! = 15_511_210_043_330_985_984_000_000
26! = 403_291_461_126_605_635_584_000_000
27! = 10_888_869_450_418_352_160_768_000_000
28! = 304_888_344_611_713_860_501_504_000_000
29! = 8_841_761_993_739_701_954_543_616_000_000
30! = 265_252_859_812_191_058_636_308_480_000_000

This page is posted
on the web at:

http://mindprod.com/jgloss/factorial.html

Optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\factorial.html
Canadian Mind Products
Please the feedback from other visitors, or your own feedback about the site.
Contact Roedy. Please feel free to link to this page without explicit permission.

IP:[65.110.21.43]
Your face IP:[3.17.162.140]
You are visitor number