# BigInt

``public struct BigInt: SignedInteger``

An arbitary precision signed integer type, also known as a big integer.

Operations on big integers never overflow, but they might take a long time to execute. The amount of memory (and address space) available is the only constraint to the magnitude of these numbers.

This particular big integer type uses base-2^64 digits to represent integers.

`BigInt` is essentially a tiny wrapper that extends `BigUInt` with a sign bit and provides signed integer operations. Both the underlying absolute value and the negative/positive flag are available as read-write properties.

Not all algorithms of `BigUInt` are available for `BigInt` values; for example, there is no square root or primality test for signed integers. When you need to call one of these, just extract the absolute value:

``````BigInt(255).abs.isPrime()   // Returns false
``````
• ``` Sign ```

Undocumented

See more

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` Word ```

The type representing a digit in `BigInt`‘s underlying number system.

#### Declaration

Swift

``public typealias Word = BigUInt.Word``
• ``` isSigned ```

#### Declaration

Swift

``public static var isSigned: Bool``
• ``` magnitude ```

The absolute value of this integer.

#### Declaration

Swift

``public var magnitude: BigUInt``
• ``` sign ```

True iff the value of this integer is negative.

#### Declaration

Swift

``public var sign: Sign``
• ``` init(sign:magnitude:) ```

Initializes a new big integer with the provided absolute number and sign flag.

#### Declaration

Swift

``public init(sign: Sign, magnitude: BigUInt)``
• ``` isZero ```

Return true iff this integer is zero.

Complexity

O(1)

#### Declaration

Swift

``public var isZero: Bool``
• ``` signum() ```

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

#### Declaration

Swift

``public func signum() -> BigInt``

#### Return Value

The sign of this number, expressed as an integer of the same type.

• ``` ~(_:) ```

#### Declaration

Swift

``public static prefix func ~(x: BigInt) -> BigInt``
• ``` &(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` |(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` ^(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` &=(_:_:) ```

#### Declaration

Swift

``public static func &=(lhs: inout BigInt, rhs: BigInt)``
• ``` |=(_:_:) ```

#### Declaration

Swift

``public static func |=(lhs: inout BigInt, rhs: BigInt)``
• ``` ^=(_:_:) ```

#### Declaration

Swift

``public static func ^=(lhs: inout BigInt, rhs: BigInt)``
• ``` isStrongProbablePrime(_:) ```

Returns true iff this integer passes the strong probable prime test for the specified base.

#### Declaration

Swift

``public func isStrongProbablePrime(_ base: BigInt) -> Bool``
• ``` isPrime(rounds:) ```

Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.

This function performs a probabilistic Miller-Rabin Primality Test, consisting of `rounds` iterations, each calculating the strong probable prime test for a random base. The number of rounds is 10 by default, but you may specify your own choice.

To speed things up, the function checks if `self` is divisible by the first few prime numbers before diving into (slower) Miller-Rabin testing.

Also, when `self` is less than 82 bits wide, `isPrime` does a deterministic test that is guaranteed to return a correct result.

#### Declaration

Swift

``public func isPrime(rounds: Int = 10) -> Bool``
• ``` bitWidth ```

#### Declaration

Swift

``public var bitWidth: Int``
• ``` trailingZeroBitCount ```

#### Declaration

Swift

``public var trailingZeroBitCount: Int``
• ``` Words ```

#### Declaration

Swift

``public struct Words: RandomAccessCollection``
• ``` words ```

#### Declaration

Swift

``public var words: Words``
• ``` init(words:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` appendHashes(to:) ```

Append this `BigInt` to the specified hasher.

#### Declaration

Swift

``public func appendHashes(to hasher: inout SipHasher)``
• ``` init(exactly:) ```

#### Declaration

Swift

``public init?<T: BinaryFloatingPoint>(exactly source: T)``
• ``` init(_:) ```

#### Declaration

Swift

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

#### Declaration

Swift

``public mutating func negate()``
• ``` -(_:_:) ```

Subtract `b` from `a` and return the result.

#### Declaration

Swift

``public static func -(a: BigInt, b: BigInt) -> BigInt``
• ``` -=(_:_:) ```

Subtract `b` from `a` in place.

#### Declaration

Swift

``public static func -=(a: inout BigInt, b: BigInt)``
• ``` quotientAndRemainder(dividingBy:) ```

Divide this integer by `y` and return the resulting quotient and remainder.

Requires

`y > 0`

Complexity

O(count^2)

#### Declaration

Swift

``public func quotientAndRemainder(dividingBy y: BigInt) -> (quotient: BigInt, remainder: BigInt)``

#### Return Value

`(quotient, remainder)` where `quotient = floor(self/y)`, `remainder = self - quotient * y`

• ``` /(_:_:) ```

Divide `a` by `b` and return the quotient. Traps if `b` is zero.

#### Declaration

Swift

``public static func /(a: BigInt, b: BigInt) -> BigInt``
• ``` %(_:_:) ```

Divide `a` by `b` and return the remainder. The result has the same sign as `a`.

#### Declaration

Swift

``public static func %(a: BigInt, b: BigInt) -> BigInt``
• ``` modulus(_:) ```

Return the result of `a` mod `b`. The result is always a nonnegative integer that is less than the absolute value of `b`.

#### Declaration

Swift

``public func modulus(_ mod: BigInt) -> BigInt``
• ``` /=(_:_:) ```

Divide `a` by `b` storing the quotient in `a`.

#### Declaration

Swift

``public static func /=(a: inout BigInt, b: BigInt)``
• ``` %=(_:_:) ```

Divide `a` by `b` storing the remainder in `a`.

#### Declaration

Swift

``public static func %=(a: inout BigInt, b: BigInt)``
• ``` init() ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` init(_:) ```

Initializes a new signed big integer with the same value as the specified unsigned big integer.

#### Declaration

Swift

``public init(_ integer: BigUInt)``
• ``` init(_:) ```

#### Declaration

Swift

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

#### Declaration

Swift

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

#### Declaration

Swift

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

#### Declaration

Swift

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

Initialize a new big integer from an integer literal.

#### Declaration

Swift

``public init(integerLiteral value: Int64)``
• ``` ==(_:_:) ```

Return true iff `a` is equal to `b`.

#### Declaration

Swift

``public static func ==(a: BigInt, b: BigInt) -> Bool``
• ``` <(_:_:) ```

Return true iff `a` is less than `b`.

#### Declaration

Swift

``public static func <(a: BigInt, b: BigInt) -> Bool``
• ``` init(_:radix:) ```

Initialize a big integer from an ASCII representation in a given radix. Numerals above `9` are represented by letters from the English alphabet.

Requires

`radix > 1 && radix < 36`

#### Declaration

Swift

``public init?<S: StringProtocol>(_ text: S, radix: Int = 10)``

#### Return Value

The integer represented by `text`, or nil if `text` contains a character that does not represent a numeral in `radix`.

• ``` init(unicodeScalarLiteral:) ```

Initialize a new big integer from a Unicode scalar. The scalar must represent a decimal digit.

#### Declaration

Swift

``public init(unicodeScalarLiteral value: UnicodeScalar)``
• ``` init(extendedGraphemeClusterLiteral:) ```

Initialize a new big integer from an extended grapheme cluster. The cluster must consist of a decimal digit.

#### Declaration

Swift

``public init(extendedGraphemeClusterLiteral value: String)``
• ``` init(stringLiteral:) ```

Initialize a new big integer from a decimal number represented by a string literal of arbitrary length. The string must contain only decimal digits.

#### Declaration

Swift

``public init(stringLiteral value: StringLiteralType)``
• ``` description ```

Return the decimal representation of this integer.

#### Declaration

Swift

``public var description: String``
• ``` customPlaygroundQuickLook ```

Return the playground quick look representation of this integer.

#### Declaration

Swift

``public var customPlaygroundQuickLook: PlaygroundQuickLook``
• ``` *(_:_:) ```

Multiply `a` with `b` and return the result.

#### Declaration

Swift

``public static func *(a: BigInt, b: BigInt) -> BigInt``
• ``` *=(_:_:) ```

Multiply `a` with `b` in place.

#### Declaration

Swift

``public static func *=(a: inout BigInt, b: BigInt)``
• ``` advanced(by:) ```

Returns `self + n`.

#### Declaration

Swift

``public func advanced(by n: Stride) -> BigInt``
• ``` distance(to:) ```

Returns `other - self`.

#### Declaration

Swift

``public func distance(to other: BigInt) -> Stride``
• ``` &<<(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` &<<=(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` &>>(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` &>>=(_:_:) ```

Undocumented

#### Declaration

Swift

``public struct BigInt: SignedInteger``
• ``` <<(_:_:) ```

#### Declaration

Swift

``public static func <<<Other: BinaryInteger>(lhs: BigInt, rhs: Other) -> BigInt``
• ``` <<=(_:_:) ```

#### Declaration

Swift

``public static func <<=<Other: BinaryInteger>(lhs: inout BigInt, rhs: Other)``
• ``` >>(_:_:) ```

#### Declaration

Swift

``public static func >><Other: BinaryInteger>(lhs: BigInt, rhs: Other) -> BigInt``
• ``` >>=(_:_:) ```

#### Declaration

Swift

``public static func >>=<Other: BinaryInteger>(lhs: inout BigInt, rhs: Other)``
• ``` greatestCommonDivisor(with:) ```

Returns the greatest common divisor of `a` and `b`.

Complexity

O(count^2) where count = max(a.count, b.count)

#### Declaration

Swift

``public func greatestCommonDivisor(with b: BigInt) -> BigInt``
• ``` inverse(_:) ```

Returns the multiplicative inverse of this integer in modulo `modulus` arithmetic, or `nil` if there is no such number.

Requires

modulus.magnitude > 1

Complexity

O(count^3)

#### Declaration

Swift

``public func inverse(_ modulus: BigInt) -> BigInt?``

#### Return Value

If `gcd(self, modulus) == 1`, the value returned is an integer `a < modulus` such that `(a * self) % modulus == 1`. If `self` and `modulus` aren’t coprime, the return value is `nil`.

• ``` squareRoot() ```

Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than `value`.

Requires

self >= 0

#### Declaration

Swift

``public func squareRoot() -> BigInt``

#### Return Value

floor(sqrt(self))

• ``` init(from:) ```

#### Declaration

Swift

``public init(from decoder: Decoder) throws``
• ``` encode(to:) ```

#### Declaration

Swift

``public func encode(to encoder: Encoder) throws``
• ``` power(_:) ```

Returns this integer raised to the power `exponent`.

This function calculates the result by successively squaring the base while halving the exponent.

Note

This function can be unreasonably expensive for large exponents, which is why `exponent` is a simple integer value. If you want to calculate big exponents, you’ll probably need to use the modulo arithmetic variant.

`BigUInt.power(_:, modulus:)`

Complexity

O((exponent * self.count)^log2(3)) or somesuch. The result may require a large amount of memory, too.

#### Declaration

Swift

``public func power(_ exponent: Int) -> BigInt``

#### Return Value

1 if `exponent == 0`, otherwise `self` raised to `exponent`. (This implies that `0.power(0) == 1`.)

• ``` power(_:modulus:) ```

Returns the remainder of this integer raised to the power `exponent` in modulo arithmetic under `modulus`.

Uses the right-to-left binary method.

Complexity

O(exponent.count * modulus.count^log2(3)) or somesuch

#### Declaration

Swift

``public func power(_ exponent: BigInt, modulus: BigInt) -> BigInt``
• ``` +(_:_:) ```

Add `a` to `b` and return the result.

#### Declaration

Swift

``public static func +(a: BigInt, b: BigInt) -> BigInt``
• ``` +=(_:_:) ```

Add `b` to `a` in place.

#### Declaration

Swift

``public static func +=(a: inout BigInt, b: BigInt)``