DmitryTsyvtsyn / Kotlin-Algorithms-and-Design-Patterns

This repository contains the most common algorithms and data structures written in the Kotlin language with simple and concise code.

Repository from Github https://github.comDmitryTsyvtsyn/Kotlin-Algorithms-and-Design-PatternsRepository from Github https://github.comDmitryTsyvtsyn/Kotlin-Algorithms-and-Design-Patterns

Naive factorial implementation

CommanderTvis opened this issue · comments

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 ...)

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