Understanding Schönhage-Strassen algorithm (huge integer multiplication)

后端 未结 3 692
自闭症患者
自闭症患者 2021-02-08 13:56

I need to multiply several 1000s digits long integers as efficiently as possible in Python. The numbers are read from a file.

I am trying to implement the Schönhage-Stra

3条回答
  •  半阙折子戏
    2021-02-08 14:17

    Don't reinvent the wheel. GMP has an excellent high-performance implementation of this algorithm and any algorithm written in pure Python will be at least 100 times slower, simply because Python is an interpreted language. Use gmpy to call out to GMP from your Python application. I'm also curious what application you're working on that requires multiplication of such large numbers - there might be a simpler way to handle your problem.

    Also, as mentioned by other answers, "several 1000s digits long" is not nearly long enough to justify Schönhage-Strassen (you'd have to have at least 10000 decimal digits, probably more). Some variant of Toom-Cook like Toom-3 is normally used in this range. Again, don't write this yourself in Python - GMP's implementation is very carefully optimized.

提交回复
热议问题