Skip to content

Latest commit

 

History

History
91 lines (61 loc) · 1.98 KB

README.md

File metadata and controls

91 lines (61 loc) · 1.98 KB

Bignum API for Scala.js

bignum - An arbitrary-precision integer arithmetic using OpenSSL.

Description

This library is based on node-bigint by substack, but instead of using libgmp, it uses the builtin bignum functionality provided by OpenSSL. The advantage is that OpenSSL is already part of Node.js, so this library does not add any external dependency whatsoever.

Build Requirements

Build/publish the SDK locally

 $ sbt clean publish-local

Running the tests

Before running the tests the first time, you must ensure the npm packages are installed:

$ npm install

Then you can run the tests:

$ sbt test

Examples

You can perform math functions via built-in methods:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

val b = new BigNum(v1).add(500).sub(v2).div(8)
// b.toString == "75067108192986261319312244199638"

Alternatively, you can perform math functions via overloaded operators:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

val b = (new BigNum(v1) + 500 - v2) / 8
// b.toString == "75067108192986261319312244199638"

Prime number detection:

import io.scalajs.npm.bignum._

val v1 = "782910138827292261791972728324982"
val v2 = "182373273283402171237474774728373"

for {
    n <- 0 to 100
    p = BigNum.pow(2, n) - 1 if p.probPrime(50)
} {
    val perfect = p.mul(BigNum.pow(2, n - 1))
    println(perfect.toString)
}

Artifacts and Resolvers

To add the BigNum binding to your project, add the following to your build.sbt:

libraryDependencies += "io.scalajs.npm" %%% "bignum" % "0.5.0"

Optionally, you may add the Sonatype Repository resolver:

resolvers += Resolver.sonatypeRepo("releases")