Naive factorial implementation
CommanderTvis opened this issue · comments
The current implementation of factorial has two problems:
- It demonstrates literally nothing except the factorial's definition. To be meaningful it should contain some optimizations like prime factorization.
- It uses
Int
, hence it is correct only in [ 0 .. 12 ] range.
Even usingULong
will allow values only from 0 to 20 inclusively, so only long arithmetic can have any practical value because for standard integer types the optimal algorithm is just to access array:
val factorials: ULongArray = ulongArrayOf(1uL, 1uL, 2uL ...)
The Second problem has already been solved)
Look at here - https://github.com/KiberneticWorm/Kotlin-Algorithms-and-Design-Patterns/blob/master/src/main/kotlin/other/FactorialBigWithCache.kt
Can you elaborate on the first point please?
Can you elaborate on the first point please?
Just google for "efficient factorial algorithm".
Thanks) I'll do it
I found more efficient algorithms for factorial, but I couldn't implement it.
Could you make a pull request?
I implemented an efficient algorithm (from google guava library)
FactorialAdvanced