# BigInt

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

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

•  Magnitude 

#### Declaration

Swift

public typealias Magnitude = UInt64
•  magnitude 

#### Declaration

Swift

public var magnitude: UInt64 { get }
•  Words 

#### Declaration

Swift

public typealias Words = [UInt]
•  words 

#### Declaration

Swift

public var words: BigInt.Words { get }
•  size 

Returns the size of the BigNumber in bits.

#### Declaration

Swift

public var size: Int { get }
•  sizeDescription 

Returns a formated human readable string that says how much space (in bytes, kilobytes, megabytes, or gigabytes) the BigNumber occupies.

#### Declaration

Swift

public var sizeDescription: String { get }

### Initializers

•  init(_:) 

Create an instance initialized to an integer value.

#### Declaration

Swift

public init(_ z: Int)
•  init(_:) 

Create an instance initialized to an unsigned integer value.

#### Declaration

Swift

public init(_ n: UInt)
•  init(_:) 

Create an instance initialized to a string value.

#### Declaration

Swift

public init?(_ str: String)
•  init(_:radix:) 

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)
•  init(prefixedNumber:) 

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)
•  init(floatLiteral:) 

#### Declaration

Swift

public init(floatLiteral value: Double)
•  init(integerLiteral:) 

#### Declaration

Swift

public init(integerLiteral value: Int)
•  init(exactly:) 

#### Declaration

Swift

public init?<T>(exactly source: T) where T : BinaryInteger
•  init(_:) 

Creates an integer from the given floating-point value, rounding toward zero.

#### Declaration

Swift

public init<T>(_ source: T) where T : BinaryFloatingPoint
•  init(_:) 

Creates a new instance from the given integer.

#### Declaration

Swift

public init<T>(_ source: T) where T : BinaryInteger
•  init(clamping:) 

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
•  init(exactly:) 

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
•  init(truncatingIfNeeded:) 

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

•  description 

#### Declaration

Swift

public var description: String { get }
•  locale 

Undocumented

#### Declaration

Swift

public var locale: Locale
•  scientificDescription 

returns the current value in scientific notation

#### Declaration

Swift

public var scientificDescription: String { get }
•  asString(radix:) 

Returns the BigNumber’s value in the given base (radix) as a string.

#### Declaration

Swift

public func asString(radix: Int) -> String
•  hash(into:) 

#### Declaration

Swift

public func hash(into hasher: inout Hasher)
•  isSigned 

#### Declaration

Swift

public static var isSigned: Bool { get }
•  bitWidth 

#### Declaration

Swift

public var bitWidth: Int { get }
•  signum() 

Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.

#### Declaration

Swift

public func signum() -> BigInt
•  trailingZeroBitCount 

The number of trailing zeros in this value’s binary representation.

#### Declaration

Swift

public var trailingZeroBitCount: Int { get }

### 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

•  +(_:) 

Undocumented

#### Declaration

Swift

prefix static func + (x: BigInt) -> BigInt
•  +=(_:_:) 

Undocumented

#### Declaration

Swift

static func += (lhs: inout BigInt, rhs: BigInt)
•  +(_:_:) 

Undocumented

#### Declaration

Swift

static func + (lhs: BigInt, rhs: BigInt) -> BigInt
•  +(_:_:) 

Undocumented

#### Declaration

Swift

static func + (lhs: Int, rhs: BigInt) -> BigInt
•  +(_:_:) 

Undocumented

#### Declaration

Swift

static func + (lhs: BigInt, rhs: Int) -> BigInt
•  +=(_:_:) 

Undocumented

#### Declaration

Swift

static func += (lhs: inout Int, rhs: BigInt)
•  +=(_:_:) 

Undocumented

#### Declaration

Swift

static func += (lhs: inout BigInt, rhs: Int)

### BigNumber Negation

•  negate() 

#### Declaration

Swift

mutating func negate()
•  -(_:) 

Undocumented

#### Declaration

Swift

prefix static func - (n: BigInt) -> BigInt

### BigNumber Subtraction

•  -(_:_:) 

Undocumented

#### Declaration

Swift

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

Undocumented

#### Declaration

Swift

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

Undocumented

#### Declaration

Swift

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

Undocumented

#### Declaration

Swift

static func -= (lhs: inout BigInt, rhs: BigInt)
•  -=(_:_:) 

Undocumented

#### Declaration

Swift

static func -= (lhs: inout Int, rhs: BigInt)
•  -=(_:_:) 

Undocumented

#### Declaration

Swift

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

### BigNumber Multiplication

•  *(_:_:) 

Undocumented

#### Declaration

Swift

static func * (lhs: BigInt, rhs: BigInt) -> BigInt
•  *(_:_:) 

Undocumented

#### Declaration

Swift

static func * (lhs: Int, rhs: BigInt) -> BigInt
•  *(_:_:) 

Undocumented

#### Declaration

Swift

static func * (lhs: BigInt, rhs: Int) -> BigInt
•  *=(_:_:) 

Undocumented

#### Declaration

Swift

static func *= (lhs: inout BigInt, rhs: BigInt)
•  *=(_:_:) 

Undocumented

#### Declaration

Swift

static func *= (lhs: inout Int, rhs: BigInt)
•  *=(_:_:) 

Undocumented

#### Declaration

Swift

static func *= (lhs: inout BigInt, rhs: Int)

### BigNumber Exponentiation

•  **(_:_:) 

Undocumented

#### Declaration

Swift

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

### BigNumber Division

•  quotientAndRemainder(dividingBy:) 

Returns the quotient and remainder of this value divided by the given value.

#### Declaration

Swift

func quotientAndRemainder(dividingBy rhs: BigInt) -> (quotient: BigInt, remainder: BigInt)
•  /(_:_:) 

Undocumented

#### Declaration

Swift

static func / (lhs: BigInt, rhs: BigInt) -> BigInt
•  /(_:_:) 

Undocumented

#### Declaration

Swift

static func / (lhs: Int, rhs: BigInt) -> BigInt
•  /(_:_:) 

Undocumented

#### Declaration

Swift

static func / (lhs: BigInt, rhs: Int) -> BigInt
•  /=(_:_:) 

Undocumented

#### Declaration

Swift

static func /= (lhs: inout BigInt, rhs: BigInt)
•  /=(_:_:) 

Undocumented

#### Declaration

Swift

static func /= (lhs: inout BigInt, rhs: Int)

### BigNumber Modulus

•  %(_:_:) 

Undocumented

#### Declaration

Swift

static func % (lhs: BigInt, rhs: BigInt) -> BigInt
•  %(_:_:) 

Undocumented

#### Declaration

Swift

static func % (lhs: Int, rhs: BigInt) -> BigInt
•  %(_:_:) 

Undocumented

#### Declaration

Swift

static func % (lhs: BigInt, rhs: Int) -> BigInt
•  %=(_:_:) 

Undocumented

#### Declaration

Swift

static func %= (lhs: inout BigInt, rhs: BigInt)
•  %=(_:_:) 

Undocumented

#### Declaration

Swift

static func %= (lhs: inout BigInt, rhs: Int)

### 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

•  fibonacci 

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 }
•  isPrime 

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 }
•  isMersenne 

Returns true if (2 ** exp) - 1 is a mersenne prime.

#### Declaration

Swift

var isMersenne: Bool { get }
•  primeFactors 

“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 }
•  millerRabin(accuracy:) 

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

•  primePi 

Counts the numbr of prime number before itself

#### Declaration

Swift

var primePi: Int { get }
•  randomBigNumber(bits:) 

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)
•  random 

Alias of randomBigNumber

#### Declaration

Swift

static let random: (Int) -> BigInt
•  rand() 

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
•  randbetween(_:_:) 

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.