BigInt Docs (85% documented)
BigInt Reference
BigUInt Structure Reference
publicstructBigUInt:UnsignedIntegerextensionBigUInt:CodableextensionBigUInt:ComparableextensionBigUInt:HashableextensionBigUInt:ExpressibleByIntegerLiteralextensionBigUInt:StrideableextensionBigUInt:ExpressibleByStringLiteralextensionBigUInt:CustomStringConvertibleextensionBigUInt:CustomPlaygroundDisplayConvertibleAn 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.)
The type representing a digit inBigUInt‘s underlying number system.
Swift
publictypealiasWord=UIntInitializes a new BigUInt with value 0.
Swift
publicinit()Initializes a new BigUInt with the specified digits. The digits are ordered from least to most significant.
Swift
publicinit(words:[Word])Adda andb together and return the result.
Complexity
O(max(a.count, b.count))Swift
publicstaticfunc+(a:BigUInt,b:BigUInt)->BigUIntAdda andb together, and store the sum ina.
Complexity
O(max(a.count, b.count))Swift
publicstaticfunc+=(a:inoutBigUInt,b:BigUInt)Swift
publicstaticvarisSigned:Bool{get}Return true iff this integer is zero.
Complexity
O(1)Swift
publicvarisZero:Bool{get}Returns1 if this value is, positive; otherwise,0.
Swift
publicfuncsignum()->BigUIntThe sign of this number, expressed as an integer of the same type.
Return the ones’ complement ofa.
Complexity
O(a.count)Swift
publicprefixstaticfunc~(a:BigUInt)->BigUIntCalculate the bitwise OR ofa andb, and store the result ina.
Complexity
O(max(a.count, b.count))Swift
publicstaticfunc|=(a:inoutBigUInt,b:BigUInt)Calculate the bitwise AND ofa andb and return the result.
Complexity
O(max(a.count, b.count))Swift
publicstaticfunc&=(a:inoutBigUInt,b:BigUInt)Calculate the bitwise XOR ofa andb and return the result.
Complexity
O(max(a.count, b.count))Swift
publicstaticfunc^=(a:inoutBigUInt,b:BigUInt)Swift
publicinit(fromdecoder:Decoder)throwsSwift
publicfuncencode(toencoder:Encoder)throwsComparea tob and return anNSComparisonResult indicating their order.
Complexity
O(count)Swift
publicstaticfunccompare(_a:BigUInt,_b:BigUInt)->ComparisonResultReturn true iffa is equal tob.
Complexity
O(count)Swift
publicstaticfunc==(a:BigUInt,b:BigUInt)->BoolReturn true iffa is less thanb.
Complexity
O(count)Swift
publicstaticfunc<(a:BigUInt,b:BigUInt)->BoolInitialize a BigInt from bytes accessed from an UnsafeRawBufferPointer
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.
Swift
publicinit(_data:Data)Return aData value that contains the base-256 representation of this integer, in network (big-endian) byte order.
Swift
publicfuncserialize()->DataDivide this integer byy and return the resulting quotient and remainder.
Requires
y > 0Complexity
O(count^2)Swift
publicfuncquotientAndRemainder(dividingByy:BigUInt)->(quotient:BigUInt,remainder:BigUInt)(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.Swift
publicstaticfunc/(x:BigUInt,y:BigUInt)->BigUIntDividex byy and return the remainder.
Note
Usedivided(by:) if you also need the remainder.Swift
publicstaticfunc%(x:BigUInt,y:BigUInt)->BigUIntDividex byy and store the quotient inx.
Note
Usedivided(by:) if you also need the remainder.Swift
publicstaticfunc/=(x:inoutBigUInt,y:BigUInt)Dividex byy and store the remainder inx.
Note
Usedivided(by:) if you also need the remainder.Swift
publicstaticfunc%=(x:inoutBigUInt,y:BigUInt)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.Swift
publicfuncpower(_exponent:Int)->BigUInt1 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 somesuchSwift
publicfuncpower(_exponent:BigUInt,modulus:BigUInt)->BigUIntSwift
publicinit?<T>(exactlysource:T)whereT:BinaryFloatingPointSwift
publicinit<T>(_source:T)whereT:BinaryFloatingPointReturns the greatest common divisor ofself andb.
Complexity
O(count^2) where count = max(self.count, b.count)Swift
publicfuncgreatestCommonDivisor(withb:BigUInt)->BigUIntReturns themultiplicative inverse of this integer in modulomodulus arithmetic,ornil if there is no such number.
Requires
modulus > 1Complexity
O(count^3)Swift
publicfuncinverse(_modulus:BigUInt)->BigUInt?Ifgcd(self, modulus) == 1, the value returned is an integera < modulus such that(a * self) % modulus == 1. Ifself andmodulus aren’t coprime, the return value isnil.
Append thisBigUInt to the specified hasher.
Swift
publicfunchash(intohasher:inoutHasher)Swift
publicinit?<T>(exactlysource:T)whereT:BinaryIntegerSwift
publicinit<T>(_source:T)whereT:BinaryIntegerSwift
publicinit<T>(truncatingIfNeededsource:T)whereT:BinaryIntegerSwift
publicinit<T>(clampingsource:T)whereT:BinaryIntegerInitialize a new big integer from an integer literal.
Swift
publicinit(integerLiteralvalue:UInt64)Multiply this big integer by a single word, and store the result in place of the original big integer.
Complexity
O(count)Swift
publicmutatingfuncmultiply(byWordy:Word)Multiply this big integer by a single Word, and return the result.
Complexity
O(count)Swift
publicfuncmultiplied(byWordy:Word)->BigUIntMultiplyx 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)Swift
publicmutatingfuncmultiplyAndAdd(_x:BigUInt,_y:Word,shiftedByshift:Int=0)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))Swift
publicfuncmultiplied(byy:BigUInt)->BigUIntMultiplication switches to an asymptotically better recursive algorithm when arguments have more words than this limit.
Swift
publicstaticvardirectMultiplicationLimit:IntMultiplya 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))Swift
publicstaticfunc*(x:BigUInt,y:BigUInt)->BigUIntMultiplya byb and store the result ina.
Swift
publicstaticfunc*=(a:inoutBigUInt,b:BigUInt)Returns true iff this integer passes thestrong probable prime test for the specified base.
Swift
publicfuncisStrongProbablePrime(_base:BigUInt)->BoolReturns 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.
Swift
publicfuncisPrime(rounds:Int=10)->BoolCreate a big unsigned integer consisting ofwidth uniformly distributed random bits.
Swift
publicstaticfuncrandomInteger<RNG>(withMaximumWidthwidth:Int,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGeneratorwidth | The maximum number of one bits in the result. |
generator | The source of randomness. |
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.
Swift
publicstaticfuncrandomInteger(withMaximumWidthwidth:Int)->BigUIntwidth | The maximum number of one bits in the result. |
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.
Swift
publicstaticfuncrandomInteger<RNG>(withExactWidthwidth:Int,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGeneratorwidth | The number of bits required to represent the answer. |
generator | The source of randomness. |
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.
Swift
publicstaticfuncrandomInteger(withExactWidthwidth:Int)->BigUIntA random big unsigned integer whose width iswidth.
Create a uniformly distributed random unsigned integer that’s less than the specified limit.
Precondition
limit > 0.
Swift
publicstaticfuncrandomInteger<RNG>(lessThanlimit:BigUInt,usinggenerator:inoutRNG)->BigUIntwhereRNG:RandomNumberGeneratorlimit | The upper bound on the result. |
generator | The source of randomness. |
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.
Swift
publicstaticfuncrandomInteger(lessThanlimit:BigUInt)->BigUIntlimit | The upper bound on the result. |
A random big unsigned integer that is less thanlimit.
Undocumented
Swift
publicstaticfunc>>=<Other>(lhs:inoutBigUInt,rhs:Other)whereOther:BinaryIntegerUndocumented
Swift
publicstaticfunc<<=<Other>(lhs:inoutBigUInt,rhs:Other)whereOther:BinaryIntegerUndocumented
Swift
publicstaticfunc>><Other>(lhs:BigUInt,rhs:Other)->BigUIntwhereOther:BinaryIntegerUndocumented
Swift
publicstaticfunc<<<Other>(lhs:BigUInt,rhs:Other)->BigUIntwhereOther:BinaryIntegerReturns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater thanvalue.
Swift
publicfuncsquareRoot()->BigUIntfloor(sqrt(self))
A type that can represent the distance between two values ofaBigUInt.
Swift
publictypealiasStride=BigIntAddsn toself and returns the result. Traps if the result would be less than zero.
Swift
publicfuncadvanced(byn:BigInt)->BigUIntInitialize a big integer from an ASCII representation in a given radix. Numerals above9 are represented byletters from the English alphabet.
Requires
radix > 1 && radix < 36Swift
publicinit?<S>(_text:S,radix:Int=10)whereS:StringProtocolThe 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.
Swift
publicinit(unicodeScalarLiteralvalue:UnicodeScalar)Initialize a new big integer from an extended grapheme cluster.The cluster must consist of a decimal digit.
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.
Swift
publicinit(stringLiteralvalue:StringLiteralType)Return the decimal representation of this integer.
Swift
publicvardescription:String{get}Return the playground quick look representation of this integer.
Swift
publicvarplaygroundDescription:Any{get}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)Swift
publicmutatingfuncsubtractReportingOverflow(_b:BigUInt,shiftedByshift:Int=0)->BoolSubtractother 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)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)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^shiftComplexity
O(count)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^shiftComplexity
O(count)Swift
publicfuncsubtracting(_other:BigUInt,shiftedByshift:Int=0)->BigUIntDecrement this integer by one.
Requires
!isZeroComplexity
O(count)Swift
publicmutatingfuncdecrement(shiftedByshift:Int=0)Subtractb froma and return the result.
Requires
a >= bComplexity
O(a.count)Swift
publicstaticfunc-(a:BigUInt,b:BigUInt)->BigUIntSubtractb froma and store the result ina.
Requires
a >= bComplexity
O(a.count)Swift
publicstaticfunc-=(a:inoutBigUInt,b:BigUInt)Undocumented
Swift
publicsubscript(bitAtindex:Int)->Bool{getset}The minimum number of bits required to represent this integer in binary.
Complexity
O(1)Swift
publicvarbitWidth:Int{get}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
widthComplexity
O(1)Swift
publicvarleadingZeroBitCount:Int{get}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)Swift
publicvartrailingZeroBitCount:Int{get}A value in0...width.
Swift
publicstructWords:RandomAccessCollectionSwift
publicvarwords:Words{get}© 2021Károly Lőrentey. (Last updated: 2021-09-06)
Generated byjazzy ♪♫ v0.14.0, aRealm project.