BigInt
public struct BigInt : SignedInteger
extension BigInt: Codable
extension BigInt: Comparable
extension BigInt: Hashable
extension BigInt: ExpressibleByIntegerLiteral
extension BigInt: Strideable
extension BigInt: ExpressibleByStringLiteral
extension BigInt: CustomStringConvertible
extension BigInt: CustomPlaygroundDisplayConvertible
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).magnitude.isPrime() // Returns false
-
Undocumented
See moreDeclaration
Swift
public enum Sign -
Declaration
Swift
public typealias Magnitude = BigUInt -
The type representing a digit in
BigInt‘s underlying number system.Declaration
Swift
public typealias Word = BigUInt.Word -
Declaration
Swift
public static var isSigned: Bool { get } -
The absolute value of this integer.
Declaration
Swift
public var magnitude: BigUInt -
True iff the value of this integer is negative.
Declaration
Swift
public var sign: Sign -
Initializes a new big integer with the provided absolute number and sign flag.
-
Return true iff this integer is zero.
Complexity
O(1)Declaration
Swift
public var isZero: Bool { get } -
Returns
-1if this value is negative and1if it’s positive; otherwise,0.Declaration
Swift
public func signum() -> BigIntReturn Value
The sign of this number, expressed as an integer of the same type.
-
Add
atoband return the result.Declaration
Swift
public static func + (a: BigInt, b: BigInt) -> BigInt -
Add
btoain place.Declaration
Swift
public static func += (a: inout BigInt, b: BigInt)
-
Undocumented
Declaration
Swift
public prefix static func ~ (x: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func & (lhs: inout BigInt, rhs: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func | (lhs: inout BigInt, rhs: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func ^ (lhs: inout BigInt, rhs: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func &= (lhs: inout BigInt, rhs: BigInt) -
Undocumented
Declaration
Swift
public static func |= (lhs: inout BigInt, rhs: BigInt) -
Undocumented
Declaration
Swift
public static func ^= (lhs: inout BigInt, rhs: BigInt) -
Declaration
Swift
public init(from decoder: Decoder) throws -
Declaration
Swift
public func encode(to encoder: Encoder) throws -
Return true iff
ais equal tob.Declaration
Swift
public static func == (a: BigInt, b: BigInt) -> Bool -
Return true iff
ais less thanb.Declaration
Swift
public static func < (a: BigInt, b: BigInt) -> Bool -
Initialize a BigInt from bytes accessed from an UnsafeRawBufferPointer, where the first byte indicates sign (0 for positive, 1 for negative)
Declaration
Swift
public init(_ buffer: UnsafeRawBufferPointer) -
Initializes an integer from the bits stored inside a piece of
Data. The data is assumed to be in network (big-endian) byte order with a first byte to represent the sign (0 for positive, 1 for negative)Declaration
Swift
public init(_ data: Data) -
Return a
Datavalue that contains the base-256 representation of this integer, in network (big-endian) byte order and a prepended byte to indicate the sign (0 for positive, 1 for negative)Declaration
Swift
public func serialize() -> Data
-
Divide this integer by
yand return the resulting quotient and remainder.Requires
y > 0Complexity
O(count^2)Declaration
Swift
public func quotientAndRemainder(dividingBy y: BigInt) -> (quotient: BigInt, remainder: BigInt)Return Value
(quotient, remainder)wherequotient = floor(self/y),remainder = self - quotient * y -
Divide
abyband return the quotient. Traps ifbis zero.Declaration
Swift
public static func / (a: BigInt, b: BigInt) -> BigInt -
Divide
abyband return the remainder. The result has the same sign asa.Declaration
Swift
public static func % (a: BigInt, b: BigInt) -> BigInt -
Return the result of
amodb. The result is always a nonnegative integer that is less than the absolute value ofb.Declaration
Swift
public func modulus(_ mod: BigInt) -> BigInt -
Divide
abybstoring the quotient ina.Declaration
Swift
public static func /= (a: inout BigInt, b: BigInt) -
Divide
abybstoring the remainder ina.Declaration
Swift
public static func %= (a: inout BigInt, b: BigInt) -
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 whyexponentis a simple integer value. If you want to calculate big exponents, you’ll probably need to use the modulo arithmetic variant.See also
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) -> BigIntReturn Value
1 if
exponent == 0, otherwiseselfraised toexponent. (This implies that0.power(0) == 1.) -
Returns the remainder of this integer raised to the power
exponentin modulo arithmetic undermodulus.Uses the right-to-left binary method.
Complexity
O(exponent.count * modulus.count^log2(3)) or somesuchDeclaration
Swift
public func power(_ exponent: BigInt, modulus: BigInt) -> BigInt -
Declaration
Swift
public init?<T>(exactly source: T) where T : BinaryFloatingPoint -
Declaration
Swift
public init<T>(_ source: T) where T : BinaryFloatingPoint -
Returns the greatest common divisor of
aandb.Complexity
O(count^2) where count = max(a.count, b.count)Declaration
Swift
public func greatestCommonDivisor(with b: BigInt) -> BigInt -
Returns the multiplicative inverse of this integer in modulo
modulusarithmetic, ornilif there is no such number.Requires
modulus.magnitude > 1Complexity
O(count^3)Declaration
Swift
public func inverse(_ modulus: BigInt) -> BigInt?Return Value
If
gcd(self, modulus) == 1, the value returned is an integera < modulussuch that(a * self) % modulus == 1. Ifselfandmodulusaren’t coprime, the return value isnil. -
Append this
BigIntto the specified hasher.Declaration
Swift
public func hash(into hasher: inout Hasher) -
Undocumented
Declaration
Swift
public init() -
Initializes a new signed big integer with the same value as the specified unsigned big integer.
Declaration
Swift
public init(_ integer: BigUInt) -
Declaration
Swift
public init<T>(_ source: T) where T : BinaryInteger -
Declaration
Swift
public init?<T>(exactly source: T) where T : BinaryInteger -
Declaration
Swift
public init<T>(clamping source: T) where T : BinaryInteger -
Declaration
Swift
public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger -
Initialize a new big integer from an integer literal.
Declaration
Swift
public init(integerLiteral value: Int64) -
Multiply
awithband return the result.Declaration
Swift
public static func * (a: BigInt, b: BigInt) -> BigInt -
Multiply
awithbin place.Declaration
Swift
public static func *= (a: inout BigInt, b: BigInt)
-
Returns true iff this integer passes the strong probable prime test for the specified base.
Declaration
Swift
public func isStrongProbablePrime(_ base: BigInt) -> Bool -
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
roundsiterations, 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
selfis divisible by the first few prime numbers before diving into (slower) Miller-Rabin testing.Also, when
selfis less than 82 bits wide,isPrimedoes a deterministic test that is guaranteed to return a correct result.Declaration
Swift
public func isPrime(rounds: Int = 10) -> Bool -
Undocumented
Declaration
Swift
public static func &<< (left: BigInt, right: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func &<<= (left: inout BigInt, right: BigInt) -
Undocumented
Declaration
Swift
public static func &>> (left: BigInt, right: BigInt) -> BigInt -
Undocumented
Declaration
Swift
public static func &>>= (left: inout BigInt, right: BigInt) -
Undocumented
Declaration
Swift
public static func << <Other>(lhs: BigInt, rhs: Other) -> BigInt where Other : BinaryInteger -
Undocumented
Declaration
Swift
public static func <<= <Other>(lhs: inout BigInt, rhs: Other) where Other : BinaryInteger -
Undocumented
Declaration
Swift
public static func >> <Other>(lhs: BigInt, rhs: Other) -> BigInt where Other : BinaryInteger -
Undocumented
Declaration
Swift
public static func >>= <Other>(lhs: inout BigInt, rhs: Other) where Other : BinaryInteger
-
Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than
value.Requires
self >= 0Declaration
Swift
public func squareRoot() -> BigIntReturn Value
floor(sqrt(self))
-
Declaration
Swift
public typealias Stride = BigInt -
Returns
self + n.Declaration
Swift
public func advanced(by n: Stride) -> BigInt -
Returns
other - self.Declaration
Swift
public func distance(to other: BigInt) -> Stride -
Initialize a big integer from an ASCII representation in a given radix. Numerals above
9are represented by letters from the English alphabet.Requires
radix > 1 && radix < 36Declaration
Swift
public init?<S>(_ text: S, radix: Int = 10) where S : StringProtocolReturn Value
The integer represented by
text, or nil iftextcontains a character that does not represent a numeral inradix. -
Initialize a new big integer from a Unicode scalar. The scalar must represent a decimal digit.
Declaration
Swift
public init(unicodeScalarLiteral value: UnicodeScalar) -
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) -
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) -
Return the decimal representation of this integer.
Declaration
Swift
public var description: String { get } -
Return the playground quick look representation of this integer.
Declaration
Swift
public var playgroundDescription: Any { get } -
Undocumented
Declaration
Swift
public mutating func negate() -
Subtract
bfromaand return the result.Declaration
Swift
public static func - (a: BigInt, b: BigInt) -> BigInt -
Subtract
bfromain place.Declaration
Swift
public static func -= (a: inout BigInt, b: BigInt) -
Undocumented
Declaration
Swift
public var bitWidth: Int { get } -
Undocumented
Declaration
Swift
public var trailingZeroBitCount: Int { get } -
Declaration
Swift
public struct Words : RandomAccessCollection -
Undocumented
Declaration
Swift
public var words: Words { get } -
Undocumented
Declaration
Swift
public init<S>(words: S) where S : Sequence, S.Element == BigInt.Word
View on GitHub
Install in Dash
BigInt Structure Reference