🏡 drafts\bigints_examples.nim

Bigints Examples

see https://github.com/def-/nim-bigints/tree/master/examples

import
  bigints, sugar

Rosetta code

Arbitrary-precision integers

$$5^{4^{3^2}}$$

var x = 5.pow 4.pow 3.pow 2
var s = $x
echo s[0 .. 19] & "..." & s[^20 .. ^1]
echo s.len, " digits"
62060698786608744707...92256259918212890625
183231 digits

Combinations and permutations

Solution for Combinations_and_permutations

proc perm(n, k: int32): BigInt =
  result = initBigInt 1      ## I could also use `one` (a const)
  var
    k = n - k
    n = n
  while n > k:
    result *= n
    dec n

proc comb(n, k: int32): BigInt =
  result = perm(n, k)
  var k = k
  while k > 0:
    result = result div k
    dec k

dump
  let combinations = comb(1000, 969)
  combinations
dump
  let permutations = perm(1000, 969)
  permutations

let combinations = comb(1000, 969)
combinations = 76023224077694604844267036884498328958811481548795886956000

let permutations = perm(1000, 969)
permutations = 48935322338186189966677615285264341307180008233417452886421014800950224875798629273926474557369035547211608939952425271857427907004912931382799980022535452823312144836508204188170562716219897527182757583012458103224469238431017264361357084179542551077339421218458525552210108412655253787858136317956338985503569604861639899701375118833411302155834718351270828287035400491383900493509499569168658161030348473708836044022937712580718634122326391120676298445158652944658449416153349920876602658760327928712841524541385291103617324363995364258875104593688468122090426867620668347962556488927982208536582639896200845855316536558023419168588932089763266007161186460812895165172222679328450735400027239293877695823413549738647246493410762890027858462874704445249505066856869158499901313801901794502504723615154109215428770227142898548411560306800701245360319610995782542728449804204425224900432803335713216610535443069526664397910819420296727391485660367677390609552098469805487995593855202735055224825799441869393117501033477508656170735343719235877766454673978280218812457637409594643934896448323038418194453204867489293698469149128577156374321013443310588967727652631387253936678004973103562714936132170761327107457911224297737840734888430796837809293767771189645281284263896331364944569254604339636706219595567045351283162761038686472674861442149619773743631737332109187120268922583036351767801107619263151558668321857486766620956921907456537417653856615083500356414390187097277419506999359315073694959823940713086453121285609743159030742719230270447287701392999208349056164483814630837742061990086746976557389586277861847668106772171150342474311451036661958197415704018677687188960376326732499270448198456552100604403852921697064424469405220701806194938420802201228657655411564226071892658400557397747212683882156188289009011021109998569430007126733885336598000799364737831050436837847478136562462851361574908821992088989991543967878103201480422357083732815018107934471809944740913159057087257139704509658342961719342049479649453934358082983195657763260664269985623046548957257032904564935980463560696215026021182421434772003716535651452340247847865573980699543205268737090407337836401364575647481349938920034362250943586347426438597011699691456364487594862697358331692914687612436827219584614400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

combinations has 59 digits
permutations has 2534 digits

import nimib, strformat
# add timings
nbInit
nbDoc.useLatex
nbText """
# Bigints Examples
see <https://github.com/def-/nim-bigints/tree/master/examples>"""

nbCode:
  import bigints, sugar

# todo: add toc
nbText "## Rosetta code"
nbText """### [Arbitrary-precision integers](http://rosettacode.org/wiki/Arbitrary-precision_integers_(included))

$$5^{4^{3^2}}$$
"""
nbCode:
# a way to keep this as a separate scope? go the nim way: wrap it in scope!
  var x = 5.pow 4.pow 3.pow 2
  var s = $x

  echo s[0 .. 19] & "..." & s[^20 .. ^1]
  echo s.len, " digits"

nbText """
### Combinations and permutations
Solution for [Combinations_and_permutations](http://rosettacode.org/wiki/Combinations_and_permutations)"""

nbCode:
  proc perm(n, k: int32): BigInt = # note the spacing on next documentation comment
    result = initBigInt 1  ## I could also use `one` (a const)
    var
      k = n - k
      n = n
    while n > k:
      result *= n
      dec n
  
  proc comb(n, k: int32): BigInt =
    result = perm(n, k)
    var k = k
    while k > 0:
      result = result div k
      dec k

  # implemented walrus-like, it will be rendered over 3 lines
  dump (let combinations = comb(1000, 969); combinations)
  dump (let permutations = perm(1000, 969); permutations)

nbText fmt"""
`combinations` has {($combinations).len} digits  
`permutations` has {($permutations).len} digits"""  # I used trailing spaces here

nbSave