distance(to:)
Returns the (potentially negative) difference between self
and other
as a BigInt
. Never traps.
func distance(to other: BigUInt) -> BigInt
Returns the (potentially negative) difference between self
and other
as a BigInt
. Never traps.
func distance(to other: BigUInt) -> BigInt
import BigInt
struct BigUInt
An arbitary precision unsigned integer type, also known as a “big integer”.
struct BigInt
An arbitary precision signed integer type, also known as a “big integer”.
init()
Initializes a new BigUInt with value 0.
init(_ data: Data)
Initializes an integer from the bits stored inside a piece of Data
. The data is assumed to be in network (big-endian) byte order.
init(_ buffer: UnsafeRawBufferPointer)
Initialize a BigInt from bytes accessed from an UnsafeRawBufferPointer
init<T>(_ source: T) where T : BinaryFloatingPoint
init<T>(_ source: T) where T : BinaryInteger
init?<S>(_ text: S, radix: Int = 10) where S : StringProtocol
Initialize a big integer from an ASCII representation in a given radix. Numerals above 9
are represented by letters from the English alphabet.
init<T>(clamping source: T) where T : BinaryInteger
init?(exactly source: Decimal)
init?<T>(exactly source: T) where T : BinaryFloatingPoint
init?<T>(exactly source: T) where T : BinaryInteger
init(extendedGraphemeClusterLiteral value: String)
Initialize a new big integer from an extended grapheme cluster. The cluster must consist of a decimal digit.
init(from decoder: Decoder) throws
init(integerLiteral value: UInt64)
Initialize a new big integer from an integer literal.
init(stringLiteral value: StringLiteralType)
Initialize a new big integer from a decimal number represented by a string literal of arbitrary length. The string must contain only decimal digits.
init?(truncating source: Decimal)
init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
init(unicodeScalarLiteral value: UnicodeScalar)
Initialize a new big integer from a Unicode scalar. The scalar must represent a decimal digit.
init(words: [Word])
Initializes a new BigUInt with the specified digits. The digits are ordered from least to most significant.
init<Words>(words: Words) where Words : Sequence, Words.Element == UInt
static let directMultiplicationLimit: Int
Multiplication switches to an asymptotically better recursive algorithm when arguments have more words than this limit.
static var isSigned: Bool { get }
static func compare(_ a: BigUInt, _ b: BigUInt) -> ComparisonResult
Compare a
to b
and return an NSComparisonResult
indicating their order.
static func randomInteger(lessThan limit: BigUInt) -> BigUInt
Create a uniformly distributed random unsigned integer that’s less than the specified limit.
static func randomInteger<RNG>(lessThan limit: BigUInt, using generator: inout RNG) -> BigUInt where RNG : RandomNumberGenerator
Create a uniformly distributed random unsigned integer that’s less than the specified limit.
static func randomInteger(withExactWidth width: Int) -> BigUInt
Create a big unsigned integer consisting of width-1
uniformly distributed random bits followed by a one bit.
static func randomInteger<RNG>(withExactWidth width: Int, using generator: inout RNG) -> BigUInt where RNG : RandomNumberGenerator
Create a big unsigned integer consisting of width-1
uniformly distributed random bits followed by a one bit.
static func randomInteger(withMaximumWidth width: Int) -> BigUInt
Create a big unsigned integer consisting of width
uniformly distributed random bits.
static func randomInteger<RNG>(withMaximumWidth width: Int, using generator: inout RNG) -> BigUInt where RNG : RandomNumberGenerator
Create a big unsigned integer consisting of width
uniformly distributed random bits.
var bitWidth: Int { get }
The minimum number of bits required to represent this integer in binary.
var debugDescription: String { get }
Return the decimal representation of this integer.
var description: String { get }
Return the decimal representation of this integer.
var isZero: Bool { get }
Return true iff this integer is zero.
var leadingZeroBitCount: Int { get }
The number of leading zero bits in the binary representation of this integer in base 2^(Word.bitWidth)
. This is useful when you need to normalize a BigUInt
such that the top bit of its most significant word is 1.
var playgroundDescription: Any { get }
Return the playground quick look representation of this integer.
var trailingZeroBitCount: Int { get }
The number of trailing zero bits in the binary representation of this integer.
var words: Words { get }
subscript(bitAt index: Int) -> Bool { get set }
static func % (x: BigUInt, y: BigUInt) -> BigUInt
Divide x
by y
and return the remainder.
static func %= (x: inout BigUInt, y: BigUInt)
Divide x
by y
and store the remainder in x
.
static func &= (a: inout BigUInt, b: BigUInt)
Calculate the bitwise AND of a
and b
and return the result.
static func * (x: BigUInt, y: BigUInt) -> BigUInt
Multiply a
by b
and return the result.
static func *= (a: inout BigUInt, b: BigUInt)
Multiply a
by b
and store the result in a
.
static func + (a: BigUInt, b: BigUInt) -> BigUInt
Add a
and b
together and return the result.
static func += (a: inout BigUInt, b: BigUInt)
Add a
and b
together, and store the sum in a
.
static func - (a: BigUInt, b: BigUInt) -> BigUInt
Subtract b
from a
and return the result.
static func -= (a: inout BigUInt, b: BigUInt)
Subtract b
from a
and store the result in a
.
static func / (x: BigUInt, y: BigUInt) -> BigUInt
Divide x
by y
and return the quotient.
static func /= (x: inout BigUInt, y: BigUInt)
Divide x
by y
and store the quotient in x
.
static func < (a: BigUInt, b: BigUInt) -> Bool
Return true iff a
is less than b
.
static func << <Other>(lhs: BigUInt, rhs: Other) -> BigUInt where Other : BinaryInteger
static func <<= <Other>(lhs: inout BigUInt, rhs: Other) where Other : BinaryInteger
static func == (a: BigUInt, b: BigUInt) -> Bool
Return true iff a
is equal to b
.
static func >> <Other>(lhs: BigUInt, rhs: Other) -> BigUInt where Other : BinaryInteger
static func >>= <Other>(lhs: inout BigUInt, rhs: Other) where Other : BinaryInteger
static func ^= (a: inout BigUInt, b: BigUInt)
Calculate the bitwise XOR of a
and b
and return the result.
static func |= (a: inout BigUInt, b: BigUInt)
Calculate the bitwise OR of a
and b
, and store the result in a
.
static func ~ (a: BigUInt) -> BigUInt
Return the ones’ complement of a
.
func advanced(by n: BigInt) -> BigUInt
Adds n
to self
and returns the result. Traps if the result would be less than zero.
mutating func decrement(shiftedBy shift: Int = 0)
Decrement this integer by one.
func encode(to encoder: Encoder) throws
func greatestCommonDivisor(with b: BigUInt) -> BigUInt
Returns the greatest common divisor of self
and b
.
func hash(into hasher: inout Hasher)
Append this BigUInt
to the specified hasher.
func inverse(_ modulus: BigUInt) -> BigUInt?
Returns the multiplicative inverse of this integer in modulo modulus
arithmetic, or nil
if there is no such number.
func isPrime(rounds: Int = 10) -> Bool
Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.
func isStrongProbablePrime(_ base: BigUInt) -> Bool
Returns true iff this integer passes the strong probable prime test for the specified base.
func multiplied(by y: BigUInt) -> BigUInt
Multiply this integer by y
and return the result.
func multiplied(byWord y: Word) -> BigUInt
Multiply this big integer by a single Word, and return the result.
mutating func multiply(byWord y: Word)
Multiply this big integer by a single word, and store the result in place of the original big integer.
mutating func multiplyAndAdd(_ x: BigUInt, _ y: Word, shiftedBy shift: Int = 0)
Multiply x
by y
, and add the result to this integer, optionally shifted shift
words to the left.
func power(_ exponent: Int) -> BigUInt
Returns this integer raised to the power exponent
.
func power(_ exponent: BigUInt, modulus: BigUInt) -> BigUInt
Returns the remainder of this integer raised to the power exponent
in modulo arithmetic under modulus
.
func quotientAndRemainder(dividingBy y: BigUInt) -> (quotient: BigUInt, remainder: BigUInt)
Divide this integer by y
and return the resulting quotient and remainder.
func serialize() -> Data
Return a Data
value that contains the base-256 representation of this integer, in network (big-endian) byte order.
func serializeToBuffer() -> UnsafeRawBufferPointer
Return a UnsafeRawBufferPointer
buffer that contains the base-256 representation of this integer, in network (big-endian) byte order.
func signum() -> BigUInt
Returns 1
if this value is, positive; otherwise, 0
.
func squareRoot() -> BigUInt
Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than value
.
mutating func subtract(_ other: BigUInt, shiftedBy shift: Int = 0)
Subtract other
from this integer in place. other
is shifted shift
digits to the left before being subtracted.
mutating func subtractReportingOverflow(_ b: BigUInt, shiftedBy shift: Int = 0) -> Bool
Subtract other
from this integer in place, and return a flag indicating if the operation caused an arithmetic overflow. other
is shifted shift
digits to the left before being subtracted.
func subtracting(_ other: BigUInt, shiftedBy shift: Int = 0) -> BigUInt
Subtract b
from this integer, and return the difference. b
is shifted shift
digits to the left before being subtracted.
func subtractingReportingOverflow(_ other: BigUInt) -> (partialValue: BigUInt, overflow: Bool)
Subtracts other
from self
, returning the result and a flag indicating arithmetic overflow.
func subtractingReportingOverflow(_ other: BigUInt, shiftedBy shift: Int) -> (partialValue: BigUInt, overflow: Bool)
Subtract other
from this integer, returning the difference and a flag indicating arithmetic overflow. other
is shifted shift
digits to the left before being subtracted.
struct Words
typealias Stride = BigInt
A type that can represent the distance between two values ofa BigUInt
.
typealias Word = UInt
The type representing a digit in BigUInt
’s underlying number system.