Movatterモバイル変換


[0]ホーム

URL:


BigInt Docs (85% documented)

View on GitHub

Install in Dash

BigInt Reference BigUInt Structure Reference

BigUInt

publicstructBigUInt:UnsignedInteger
extensionBigUInt:Codable
extensionBigUInt:Comparable
extensionBigUInt:Hashable
extensionBigUInt:ExpressibleByIntegerLiteral
extensionBigUInt:Strideable
extensionBigUInt:ExpressibleByStringLiteral
extensionBigUInt:CustomStringConvertible
extensionBigUInt:CustomPlaygroundDisplayConvertible

An arbitary precision unsigned integer type, also known as a “big integer”.

Operations on big integers never overflow, but they may 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; you can think of it as a wrapperaroundArray<UInt64>. (In fact,BigUInt only uses an array if there are more than two digits.)

Show on GitHub
  • The type representing a digit inBigUInt‘s underlying number system.

    Declaration

    Swift

    publictypealiasWord=UInt
  • Initializes a new BigUInt with value 0.

    Declaration

    Swift

    publicinit()
  • Initializes a new BigUInt with the specified digits. The digits are ordered from least to most significant.

    Declaration

    Swift

    publicinit(words:[Word])

Addition

  • Adda andb together and return the result.

    Complexity

    O(max(a.count, b.count))

    Declaration

    Swift

    publicstaticfunc+(a:BigUInt,b:BigUInt)->BigUInt
  • Adda andb together, and store the sum ina.

    Complexity

    O(max(a.count, b.count))

    Declaration

    Swift

    publicstaticfunc+=(a:inoutBigUInt,b:BigUInt)
  • Declaration

    Swift

    publicstaticvarisSigned:Bool{get}
  • Return true iff this integer is zero.

    Complexity

    O(1)

    Declaration

    Swift

    publicvarisZero:Bool{get}
  • Returns1 if this value is, positive; otherwise,0.

    Declaration

    Swift

    publicfuncsignum()->BigUInt

    Return Value

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

Bitwise Operations

  • Return the ones’ complement ofa.

    Complexity

    O(a.count)

    Declaration

    Swift

    publicprefixstaticfunc~(a:BigUInt)->BigUInt
  • Calculate the bitwise OR ofa andb, and store the result ina.

    Complexity

    O(max(a.count, b.count))

    Declaration

    Swift

    publicstaticfunc|=(a:inoutBigUInt,b:BigUInt)
  • Calculate the bitwise AND ofa andb and return the result.

    Complexity

    O(max(a.count, b.count))

    Declaration

    Swift

    publicstaticfunc&=(a:inoutBigUInt,b:BigUInt)
  • Calculate the bitwise XOR ofa andb and return the result.

    Complexity

    O(max(a.count, b.count))

    Declaration

    Swift

    publicstaticfunc^=(a:inoutBigUInt,b:BigUInt)
  • Declaration

    Swift

    publicinit(fromdecoder:Decoder)throws
  • Declaration

    Swift

    publicfuncencode(toencoder:Encoder)throws

Comparison

  • Comparea tob and return anNSComparisonResult indicating their order.

    Complexity

    O(count)

    Declaration

    Swift

    publicstaticfunccompare(_a:BigUInt,_b:BigUInt)->ComparisonResult
  • Return true iffa is equal tob.

    Complexity

    O(count)

    Declaration

    Swift

    publicstaticfunc==(a:BigUInt,b:BigUInt)->Bool
  • Return true iffa is less thanb.

    Complexity

    O(count)

    Declaration

    Swift

    publicstaticfunc<(a:BigUInt,b:BigUInt)->Bool

NSData Conversion

  • Initialize a BigInt from bytes accessed from an UnsafeRawBufferPointer

    Declaration

    Swift

    publicinit(_buffer:UnsafeRawBufferPointer)
  • Initializes an integer from the bits stored inside a piece ofData.The data is assumed to be in network (big-endian) byte order.

    Declaration

    Swift

    publicinit(_data:Data)
  • Return aData value that contains the base-256 representation of this integer, in network (big-endian) byte order.

    Declaration

    Swift

    publicfuncserialize()->Data

Division

  • Divide this integer byy and return the resulting quotient and remainder.

    Requires

    y > 0

    Complexity

    O(count^2)

    Declaration

    Swift

    publicfuncquotientAndRemainder(dividingByy:BigUInt)->(quotient:BigUInt,remainder:BigUInt)

    Return Value

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

  • Dividex byy and return the quotient.

    Note

    Usedivided(by:) if you also need the remainder.

    Declaration

    Swift

    publicstaticfunc/(x:BigUInt,y:BigUInt)->BigUInt
  • Dividex byy and return the remainder.

    Note

    Usedivided(by:) if you also need the remainder.

    Declaration

    Swift

    publicstaticfunc%(x:BigUInt,y:BigUInt)->BigUInt
  • Dividex byy and store the quotient inx.

    Note

    Usedivided(by:) if you also need the remainder.

    Declaration

    Swift

    publicstaticfunc/=(x:inoutBigUInt,y:BigUInt)
  • Dividex byy and store the remainder inx.

    Note

    Usedivided(by:) if you also need the remainder.

    Declaration

    Swift

    publicstaticfunc%=(x:inoutBigUInt,y:BigUInt)

Exponentiation

  • Returns this integer raised to the powerexponent.

    This function calculates the result bysuccessively squaring the base while halving the exponent.

    Note

    This function can be unreasonably expensive for large exponents, which is whyexponent is 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

    publicfuncpower(_exponent:Int)->BigUInt

    Return Value

    1 ifexponent == 0, otherwiseself raised toexponent. (This implies that0.power(0) == 1.)

  • Returns the remainder of this integer raised to the powerexponent in modulo arithmetic undermodulus.

    Uses theright-to-left binary method.

    Complexity

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

    Declaration

    Swift

    publicfuncpower(_exponent:BigUInt,modulus:BigUInt)->BigUInt
  • Declaration

    Swift

    publicinit?<T>(exactlysource:T)whereT:BinaryFloatingPoint
  • Declaration

    Swift

    publicinit<T>(_source:T)whereT:BinaryFloatingPoint

Greatest Common Divisor

Hashing

Multiplication

  • Multiply this big integer by a single word, and store the result in place of the original big integer.

    Complexity

    O(count)

    Declaration

    Swift

    publicmutatingfuncmultiply(byWordy:Word)
  • Multiply this big integer by a single Word, and return the result.

    Complexity

    O(count)

    Declaration

    Swift

    publicfuncmultiplied(byWordy:Word)->BigUInt
  • Multiplyx byy, and add the result to this integer, optionally shiftedshift words to the left.

    Note

    This is the fused multiply/shift/add operation; it is more efficient than doing the componentsindividually. (The fused operation doesn’t need to allocate space for temporary big integers.)

    Complexity

    O(count)

    Declaration

    Swift

    publicmutatingfuncmultiplyAndAdd(_x:BigUInt,_y:Word,shiftedByshift:Int=0)

    Return Value

    self is set toself + (x * y) << (shift * 2^Word.bitWidth)

  • Multiply this integer byy and return the result.

    Note

    This uses the naive O(n^2) multiplication algorithm unless both arguments have more thanBigUInt.directMultiplicationLimit words.

    Complexity

    O(n^log2(3))

    Declaration

    Swift

    publicfuncmultiplied(byy:BigUInt)->BigUInt
  • Multiplication switches to an asymptotically better recursive algorithm when arguments have more words than this limit.

    Declaration

    Swift

    publicstaticvardirectMultiplicationLimit:Int
  • Multiplya byb and return the result.

    Note

    This uses the naive O(n^2) multiplication algorithm unless both arguments have more thanBigUInt.directMultiplicationLimit words.

    Complexity

    O(n^log2(3))

    Declaration

    Swift

    publicstaticfunc*(x:BigUInt,y:BigUInt)->BigUInt
  • Multiplya byb and store the result ina.

    Declaration

    Swift

    publicstaticfunc*=(a:inoutBigUInt,b:BigUInt)

Primality Testing

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

    Declaration

    Swift

    publicfuncisStrongProbablePrime(_base:BigUInt)->Bool
  • Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.

    This function performs a probabilisticMiller-Rabin Primality Test, consisting ofrounds 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 ifself is divisible by the first few prime numbers beforediving into (slower) Miller-Rabin testing.

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

    Declaration

    Swift

    publicfuncisPrime(rounds:Int=10)->Bool
  • Create a big unsigned integer consisting ofwidth uniformly distributed random bits.

    Declaration

    Swift

    publicstaticfuncrandomInteger<RNG>(withMaximumWidthwidth:Int,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGenerator

    Parameters

    width

    The maximum number of one bits in the result.

    generator

    The source of randomness.

    Return Value

    A big unsigned integer less than1 << width.

  • Create a big unsigned integer consisting ofwidth uniformly distributed random bits.

    Note

    I use aSystemRandomGeneratorGenerator as the source of randomness.

    Declaration

    Swift

    publicstaticfuncrandomInteger(withMaximumWidthwidth:Int)->BigUInt

    Parameters

    width

    The maximum number of one bits in the result.

    Return Value

    A big unsigned integer less than1 << width.

  • Create a big unsigned integer consisting ofwidth-1 uniformly distributed random bits followed by a one bit.

    Note

    Ifwidth is zero, the result is zero.

    Declaration

    Swift

    publicstaticfuncrandomInteger<RNG>(withExactWidthwidth:Int,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGenerator

    Parameters

    width

    The number of bits required to represent the answer.

    generator

    The source of randomness.

    Return Value

    A random big unsigned integer whose width iswidth.

  • Create a big unsigned integer consisting ofwidth-1 uniformly distributed random bits followed by a one bit.

    Note

    Ifwidth is zero, the result is zero.

    Note

    I use aSystemRandomGeneratorGenerator as the source of randomness.

    Declaration

    Swift

    publicstaticfuncrandomInteger(withExactWidthwidth:Int)->BigUInt

    Return Value

    A random big unsigned integer whose width iswidth.

  • Create a uniformly distributed random unsigned integer that’s less than the specified limit.

    Precondition

    limit > 0.

    Declaration

    Swift

    publicstaticfuncrandomInteger<RNG>(lessThanlimit:BigUInt,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGenerator

    Parameters

    limit

    The upper bound on the result.

    generator

    The source of randomness.

    Return Value

    A random big unsigned integer that is less thanlimit.

  • Create a uniformly distributed random unsigned integer that’s less than the specified limit.

    Precondition

    limit > 0.

    Note

    I use aSystemRandomGeneratorGenerator as the source of randomness.

    Declaration

    Swift

    publicstaticfuncrandomInteger(lessThanlimit:BigUInt)->BigUInt

    Parameters

    limit

    The upper bound on the result.

    Return Value

    A random big unsigned integer that is less thanlimit.

Shift Operators

  • Undocumented

    Declaration

    Swift

    publicstaticfunc>>=<Other>(lhs:inoutBigUInt,rhs:Other)whereOther:BinaryInteger
  • Undocumented

    Declaration

    Swift

    publicstaticfunc<<=<Other>(lhs:inoutBigUInt,rhs:Other)whereOther:BinaryInteger
  • Undocumented

    Declaration

    Swift

    publicstaticfunc>><Other>(lhs:BigUInt,rhs:Other)->BigUIntwhereOther:BinaryInteger
  • Undocumented

    Declaration

    Swift

    publicstaticfunc<<<Other>(lhs:BigUInt,rhs:Other)->BigUIntwhereOther:BinaryInteger

Square Root

  • Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater thanvalue.

    Declaration

    Swift

    publicfuncsquareRoot()->BigUInt

    Return Value

    floor(sqrt(self))

  • A type that can represent the distance between two values ofaBigUInt.

    Declaration

    Swift

    publictypealiasStride=BigInt
  • Addsn toself and returns the result. Traps if the result would be less than zero.

    Declaration

    Swift

    publicfuncadvanced(byn:BigInt)->BigUInt
  • Returns the (potentially negative) difference betweenself andother as aBigInt. Never traps.

    Declaration

    Swift

    publicfuncdistance(toother:BigUInt)->BigInt

String Conversion

  • Initialize a big integer from an ASCII representation in a given radix. Numerals above9 are represented byletters from the English alphabet.

    Requires

    radix > 1 && radix < 36

    Declaration

    Swift

    publicinit?<S>(_text:S,radix:Int=10)whereS:StringProtocol

    Return Value

    The integer represented bytext, or nil iftext contains 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

    publicinit(unicodeScalarLiteralvalue:UnicodeScalar)
  • Initialize a new big integer from an extended grapheme cluster.The cluster must consist of a decimal digit.

    Declaration

    Swift

    publicinit(extendedGraphemeClusterLiteralvalue: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

    publicinit(stringLiteralvalue:StringLiteralType)
  • Return the decimal representation of this integer.

    Declaration

    Swift

    publicvardescription:String{get}
  • Return the playground quick look representation of this integer.

    Declaration

    Swift

    publicvarplaygroundDescription:Any{get}

Subtraction

  • Subtractother from this integer in place, and return a flag indicating if the operation caused anarithmetic overflow.other is shiftedshift digits to the left before being subtracted.

    Note

    If the result indicates an overflow, thenself becomes the twos’ complement of the absolute difference.

    Complexity

    O(count)

    Declaration

    Swift

    publicmutatingfuncsubtractReportingOverflow(_b:BigUInt,shiftedByshift:Int=0)->Bool
  • Subtractother from this integer, returning the difference and a flag indicating arithmetic overflow.other is shiftedshift digits to the left before being subtracted.

    Note

    Ifoverflow is true, then the result value is the twos’ complement of the absolute value of the difference.

    Complexity

    O(count)

    Declaration

    Swift

    publicfuncsubtractingReportingOverflow(_other:BigUInt,shiftedByshift:Int)->(partialValue:BigUInt,overflow:Bool)
  • Subtractsother fromself, returning the result and a flag indicating arithmetic overflow.

    Note

    When the operation overflows, thenpartialValue is the twos’ complement of the absolute value of the difference.

    Complexity

    O(count)

    Declaration

    Swift

    publicfuncsubtractingReportingOverflow(_other:BigUInt)->(partialValue:BigUInt,overflow:Bool)
  • Subtractother from this integer in place.other is shiftedshift digits to the left before being subtracted.

    Requires

    self >= other * 2^shift

    Complexity

    O(count)

    Declaration

    Swift

    publicmutatingfuncsubtract(_other:BigUInt,shiftedByshift:Int=0)
  • Subtractb from this integer, and return the difference.b is shiftedshift digits to the left before being subtracted.

    Requires

    self >= b * 2^shift

    Complexity

    O(count)

    Declaration

    Swift

    publicfuncsubtracting(_other:BigUInt,shiftedByshift:Int=0)->BigUInt
  • Decrement this integer by one.

    Requires

    !isZero

    Complexity

    O(count)

    Declaration

    Swift

    publicmutatingfuncdecrement(shiftedByshift:Int=0)
  • Subtractb froma and return the result.

    Requires

    a >= b

    Complexity

    O(a.count)

    Declaration

    Swift

    publicstaticfunc-(a:BigUInt,b:BigUInt)->BigUInt
  • Subtractb froma and store the result ina.

    Requires

    a >= b

    Complexity

    O(a.count)

    Declaration

    Swift

    publicstaticfunc-=(a:inoutBigUInt,b:BigUInt)
  • Undocumented

    Declaration

    Swift

    publicsubscript(bitAtindex:Int)->Bool{getset}
  • The minimum number of bits required to represent this integer in binary.

    Complexity

    O(1)

    Declaration

    Swift

    publicvarbitWidth:Int{get}

    Return Value

    floor(log2(2 * self + 1))

  • The number of leading zero bits in the binary representation of this integer in base2^(Word.bitWidth).This is useful when you need to normalize aBigUInt such that the top bit of its most significant word is 1.

    Note

    0 is considered to have zero leading zero bits.

    See also

    width

    Complexity

    O(1)

    Declaration

    Swift

    publicvarleadingZeroBitCount:Int{get}

    Return Value

    A value in0...(Word.bitWidth - 1).

  • The number of trailing zero bits in the binary representation of this integer.

    Note

    0 is considered to have zero trailing zero bits.

    Complexity

    O(count)

    Declaration

    Swift

    publicvartrailingZeroBitCount:Int{get}

    Return Value

    A value in0...width.

  • Declaration

    Swift

    publicstructWords:RandomAccessCollection
  • Declaration

    Swift

    publicvarwords:Words{get}
  • Undocumented

    Declaration

    Swift

    publicinit<Words>(words:Words)whereWords:Sequence,Words.Element==BigUInt.Word

© 2021Károly Lőrentey. (Last updated: 2021-09-06)

Generated byjazzy ♪♫ v0.14.0, aRealm project.


[8]ページ先頭

©2009-2025 Movatter.jp