dwayne/elm-natural

The natural numbers, ℕ = { 0, 1, 2, ... }


Keywords
arbitrary-precision, elm, natural-numbers
License
BSD-3-Clause
Install
elm-package install dwayne/elm-natural 1.0.0

Documentation

Elm Natural

An Elm library for computing with the natural numbers, ℕ = { 0, 1, 2, ... }.

What's available?

The natural numbers from 0 to 10

  • zero
  • one
  • two
  • three
  • four
  • five
  • six
  • seven
  • eight
  • nine
  • ten

Ways to create natural numbers from Int values

  • fromInt
  • fromSafeInt

Ways to create natural numbers from String values

  • fromBinaryString
  • fromOctalString
  • fromHexString
  • fromString (supports binary, octal, hexadecimal, and decimal input formats)
  • fromSafeString
  • fromBaseBString

Comparison operators

  • ==
  • compare
  • isLessThan
  • isLessThanOrEqual
  • isGreaterThan
  • isGreaterThanOrEqual
  • max
  • min

Predicates for classification

  • isZero
  • isOne
  • isPositive
  • isEven
  • isOdd

Arithmetic

  • add
  • sub (saturating subtraction)
  • mul
  • divModBy (Euclidean division)
  • divBy
  • modBy
  • exp

A way to convert to an Int

  • toInt (use with caution)

Ways to convert to a String representation

  • toBinaryString
  • toOctalString
  • toHexString
  • toString (decimal representation)
  • toBaseBString

Examples

As a simple example we can implement the factorial function.

import Natural as N exposing (Natural)

fact : Natural -> Natural
fact n =
    if N.isZero n then
      N.one

    else
      N.mul n <| fact (N.sub n N.one)

And then we can use it to compute 100!.

N.toString (fact (N.fromSafeInt 100)) == "93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000"

Many more examples can be found in the examples/src directory.

  • Fibonacci.elm - Computes up to the 1000th Fibonacci number.
  • Pi.elm - Computes the first 100 digits of π.
  • E.elm - Computes the first 100 digits of e.
  • BaseConversion.elm - Converts decimal numbers to their binary, octal, and hexadecimal representations.

References