BigInt

public struct BigInt:
    SignedNumeric, // Implies Numeric, Equatable, ExpressibleByIntegerLiteral
    BinaryInteger, // Implies Hashable, CustomStringConvertible, Strideable, Comparable
    ExpressibleByFloatLiteral

BigNumber is an arbitrary precision integer value type. It stores a number in base 2^64 notation as an array.

Each element of the array is called a limb, which is of type UInt64, the whole array is called limbs and has the type [UInt64]. A boolean sign variable determines if the number is positive or negative. If sign == true, then the number is smaller than 0, otherwise it is greater or equal to 0. It stores the 64 bit digits in little endian, that is, the least significant digit is stored in the array index 0:

limbs == [] := undefined, should throw an error
limbs == [0], sign == false := 0, defined as positive
limbs == [0], sign == true := undefined, should throw an error
limbs == [n] := n if sign == false, otherwise -n, given 0 <= n < 2^64

limbs == [l0, l1, l2, ..., ln] :=
(l0 * 2^(0*64)) +
(11 * 2^(1*64)) +
(12 * 2^(2*64)) +
... +
(ln * 2^(n*64))

Internal data

Initializers

  • Create an instance initialized to an integer value.

    Declaration

    Swift

    public init(_ z: Int)
  • Create an instance initialized to an unsigned integer value.

    Declaration

    Swift

    public init(_ n: UInt)
  • Create an instance initialized to a string value.

    Declaration

    Swift

    public init?(_ str: String)
  • Create an instance initialized to a string with the value of mathematical numerical system of the specified radix (base). So for example, to get the value of hexadecimal string radix value must be set to 16.

    Declaration

    Swift

    public init?(_ number: String, radix: Int)
  • Create an instance initialized to a string with the value of mathematical numerical system of the specified radix (base). You have to specify the base as a prefix, so for example, “0b100101010101110” is a vaild input for a binary number. Currently, hexadecimal (0x), octal (0o) and binary (0b) are supported.

    Declaration

    Swift

    public init?(prefixedNumber number: String)
  • Declaration

    Swift

    public init(floatLiteral value: Double)
  • Declaration

    Swift

    public init(integerLiteral value: Int)
  • Declaration

    Swift

    public init?<T>(exactly source: T) where T : BinaryInteger
  • Creates an integer from the given floating-point value, rounding toward zero.

    Declaration

    Swift

    public init<T>(_ source: T) where T : BinaryFloatingPoint
  • Creates a new instance from the given integer.

    Declaration

    Swift

    public init<T>(_ source: T) where T : BinaryInteger
  • Creates a new instance with the representable value that’s closest to the given integer.

    Declaration

    Swift

    public init<T>(clamping source: T) where T : BinaryInteger
  • Creates an integer from the given floating-point value, if it can be represented exactly.

    Declaration

    Swift

    public init?<T>(exactly source: T) where T : BinaryFloatingPoint
  • Creates a new instance from the bit pattern of the given instance by sign-extending or truncating to fit this type.

    Declaration

    Swift

    public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger

Struct functions

BigNumber Shifts

  • Undocumented

    Declaration

    Swift

    public static func << <T>(lhs: BigInt, rhs: T) -> BigInt where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    public static func <<= <T>(lhs: inout BigInt, rhs: T) where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    public static func >> <T>(lhs: BigInt, rhs: T) -> BigInt where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    public static func >>= <T>(lhs: inout BigInt, rhs: T) where T : BinaryInteger

BigNumber Bitwise AND

  • Returns the result of performing a bitwise AND operation on the two given values.

    Declaration

    Swift

    public static func & (lhs: BigInt, rhs: BigInt) -> BigInt
  • Stores the result of performing a bitwise AND operation on the two given values in the left-hand-side variable.

    Declaration

    Swift

    public static func &= (lhs: inout BigInt, rhs: BigInt)

BigNumber Bitwise OR

  • Undocumented

    Declaration

    Swift

    public static func | (lhs: BigInt, rhs: BigInt) -> BigInt
  • Undocumented

    Declaration

    Swift

    public static func |= (lhs: inout BigInt, rhs: BigInt)
  • Undocumented

    Declaration

    Swift

    public static func ^ (lhs: BigInt, rhs: BigInt) -> BigInt
  • Undocumented

    Declaration

    Swift

    public static func ^= (lhs: inout BigInt, rhs: BigInt)

BigNumber Bitwise NOT

  • Undocumented

    Declaration

    Swift

    public prefix static func ~ (x: BigInt) -> BigInt

BigNumber Addition

BigNumber Negation

BigNumber Subtraction

BigNumber Multiplication

BigNumber Exponentiation

BigNumber Division

BigNumber Modulus

BigNumber Comparing

  • Undocumented

    Declaration

    Swift

    static func == (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func == <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    static func == <T>(lhs: T, rhs: BigInt) -> Bool where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    static func != (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func != <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    static func != <T>(lhs: T, rhs: BigInt) -> Bool where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    static func < (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func < <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
  • Undocumented

    Declaration

    Swift

    static func < (lhs: Int, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func < (lhs: BigInt, rhs: Int) -> Bool
  • Undocumented

    Declaration

    Swift

    static func > (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func > (lhs: Int, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func > (lhs: BigInt, rhs: Int) -> Bool
  • Undocumented

    Declaration

    Swift

    static func <= (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func <= (lhs: Int, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func <= (lhs: BigInt, rhs: Int) -> Bool
  • Undocumented

    Declaration

    Swift

    static func >= (lhs: BigInt, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func >= (lhs: Int, rhs: BigInt) -> Bool
  • Undocumented

    Declaration

    Swift

    static func >= (lhs: BigInt, rhs: Int) -> Bool

BigNumber Utility Functions

  • Returns the corresponding Fibonacci number, commonly denoted F_n, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.

    Declaration

    Swift

    var fibonacci: BigInt { get }
  • Check if the BigInt is a prime.

    Uses the multiple of 6 method (which is fairly quick and 100% safe) for relatively small integers. For bigger ones, we switch to Miller-Rabin algorithm with 30 precision.

    Declaration

    Swift

    var isPrime: Bool { get }
  • Returns true if (2 ** exp) - 1 is a mersenne prime.

    Declaration

    Swift

    var isMersenne: Bool { get }
  • “Prime Factorization” is finding which prime numbers multiply together to make the original number.

    Here are some examples:

    BigInt(12).primeFactors //=> [2, 2, 3] because 12 = 2 * 2 * 3
    

    Declaration

    Swift

    var primeFactors: [BigInt] { get }
  • The Miller–Rabin test relies on an equality or set of equalities that hold true for prime values, then checks whether or not they hold for a number that we want to test for primality.

    Declaration

    Swift

    func millerRabin(accuracy k: Int = 30) -> Bool

    Parameters

    k

    a parameter that determines the accuracy of the test

    Return Value

    composite if self is composite, otherwise probably prime

  • Counts the numbr of prime number before itself

    Declaration

    Swift

    var primePi: Int { get }
  • Generate a random BigInt

    ⚠️ This isn’t crypto secure

    Declaration

    Swift

    static func randomBigNumber(bits n: Int) -> BigInt

    Parameters

    n

    Length of random number (in terms of bits)

  • Alias of randomBigNumber

    Declaration

    Swift

    static let random: (Int) -> BigInt
  • RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1.

    A new random real number is returned every time the worksheet is calculated.

    Declaration

    Swift

    static func rand() -> BigDouble
  • Returns a random integer number between the numbers you specify. A new random integer number is returned every time the worksheet is calculated.

    Declaration

    Swift

    static func randbetween(_ a: BigDouble, _ b: BigDouble) -> BigDouble

    Parameters

    a

    The smallest integer RANDBETWEEN will return.

    b

    The largest integer RANDBETWEEN will return.