|
Oktatás * Programozás 1 * Szkriptnyelvek Teaching • Programming 1 (BI) Félévek Linkek * kalendárium |
Nim2 /
mapm — a faster bigints alternativeWe saw in the previous post that bigints becomes very slow when you start working with really huge numbers. mapm is a Nim wrapper around Matt's Arbitrary Precision Math (MAPM) library. And it's fast! $ nimble install mapm ExerciseLet's solve this exercise. In short: determine the value of 2136,279,841-1 . We know that this number consists of 41,024,320 digits. For the sake of simplicity, our programs will just print the number of digits, not the entire number. All tests were executed on my desktop computer that has an Intel Core i7-8700 CPU @ 3.20GHz. (1) Pure Python solutionimport sys sys.set_int_max_str_digits(41_100_000) PRIME = 2**136_279_841 - 1 print(len(str(PRIME))) # 41024320 Runtime: 27.64 seconds. (2) Nim solution with mapmimport pkg/mapm let a = initMapm(2) let result = a.pow(136_279_841) - initMapm(1) echo len($result) # 41024320 Runtime (release build): 24.97 seconds. Considering that bigints didn't finish (I had to stop it after a few hours), this result is very good. And it's even faster than Python :) However, as I noticed, Python's big int implementation is really good and surprisingly fast. |
![]() Blogjaim, hobbi projektjeim * The Ubuntu Incident [ edit ] |