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

后端 未结 3 645
自闭症患者
自闭症患者 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:15

    Chapter 4.3.3 of Knuth's TAOCP describes it and also has some FFT pseudocode in other chapters that could be used for this.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-08 14:26

    1000 digits is "small" for Schönhage-Strassen to be really worth using. You may have a look at the Toom Cook multiplication. gmpy is a Python wrapper to gmp providing these functions.

    0 讨论(0)
提交回复
热议问题