昨天我试图弄清楚新梅森素数的大小 ( http://www.mersenne.org/primes/?press=M74207281 ) 在我的 SBCL 盒子上(v. 1.3.2 (x64),Windows 10,Dell Core i5 8GB RAM) 将近一个小时后,我放弃并中断了计算。 在结果屏幕下方:
This is SBCL 1.3.2, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
SBCL is free software, provided as is, with absolutely no warranty.
It is mostly in the public domain; some portions are provided under
BSD-style licenses. See the CREDITS and COPYING files in the
distribution for more information.
WARNING: the Windows port is fragile, particularly for multithreaded
code. Unfortunately, the development team currently lacks the time
and resources this platform demands.
* (- (expt 2 74207281) 1)
debugger invoked on a SB-SYS:INTERACTIVE-INTERRUPT in thread
'#<THREAD "main thread" RUNNING {1002A9BD03}>:'
Interactive interrupt at #x100008BD9E.
Type HELP for debugger help, or (SB-EXT:EXIT) to exit from SBCL.
restarts (invokable by number or by possibly-abbreviated name):
0: [CONTINUE] Return from SB-WIN32::SIGINT.
1: [ABORT ] Exit debugger, returning to top level.
1
(SB-BIGNUM:MULTIPLY-BIGNUMS #<unavailable argument> #<unavailable argument>)
0] 1
对我来说这很有趣,因为我在同一台机器上的 Racket 6.4 上尝试了相同的表达式, 并且(相对而言)只用了 1 分 08 秒就开始吐出数字。 在 Haskell 中,再次在同一台机器上,使用
GHCi, version 7.10.3: http://www.haskell.org/ghc/ :? for help
Prelude> 2^74207281 - 1
开始数字展示仅用了 8 秒。
尽管这可能是一个错误,但有人知道 SBCL 如何进行大数乘法吗?它的处理方式是否会导致延误?
提前致谢!
*编辑
在 Sylvester 的评论之后,也许正确的问题是:是什么阻止了大数字的显示?是的,它确实很大(Racket 和 Haskell 版本将它写入一个 21 MB 的文本文件),但似乎有超出其大小的东西阻止了任务的完成。
最佳答案
实际计算在我的机器上相当快,实际上不到一秒。
(defun make-prime ()
(declare (optimize (safety 0)(debug 0)(speed 3)))
(time (- (expt 2 74207281) 1)))
(defparameter *prime* (make-prime))
;Evaluation took:
; 0.000 seconds of real time
; 0.000017 seconds of total run time (0.000015 user, 0.000002 system)
; 100.00% CPU
; 1,292 processor cycles
; 0 bytes consed
; ==> *PRIME*
但是打印数字是另一回事。
关于lisp - SBCL 中的 bignum 乘法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35582932/