Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Naive factorial implementation #6

Closed
CommanderTvis opened this issue Jan 30, 2022 · 6 comments
Closed

Naive factorial implementation #6

CommanderTvis opened this issue Jan 30, 2022 · 6 comments

Comments

@CommanderTvis
Copy link

The current implementation of factorial has two problems:

  1. It demonstrates literally nothing except the factorial's definition. To be meaningful it should contain some optimizations like prime factorization.
  2. It uses Int, hence it is correct only in [ 0 .. 12 ] range.
    image
    Even using ULong 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 ...)
@DmitryTsyvtsyn
Copy link
Owner

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

@DmitryTsyvtsyn
Copy link
Owner

Can you elaborate on the first point please?

@CommanderTvis
Copy link
Author

Can you elaborate on the first point please?

Just google for "efficient factorial algorithm".

@DmitryTsyvtsyn
Copy link
Owner

Thanks) I'll do it

@DmitryTsyvtsyn
Copy link
Owner

I found more efficient algorithms for factorial, but I couldn't implement it.
Could you make a pull request?

@DmitryTsyvtsyn
Copy link
Owner

I implemented an efficient algorithm (from google guava library)
FactorialAdvanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants