Recent Changes - Search:

Oktatás

* Programozás 1
  + feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

Teaching

Programming 1 (BI)
  ▸ exercises
  ▸ quick link

teaching assets


Félévek

* 2025/26/2
* archívum


Linkek

* kalendárium
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C#
* D lang.
* Java
* Nim
* Nim2
  + exercises
* XC=BASIC
* old
  ✦C++, ✦Clojure, ✦Scala


[ edit | logout ]
[ sandbox | passwd ]

mapm — a faster bigints alternative

We 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

Exercise

Let'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 solution

import 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 mapm

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

Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2026 June 03, 20:40