An arbitary precision unsigned integer type, also known as a big integer.
-
Operations on big integers never overflow, but they might take a long time to execute.
+
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 wrapper
-around Array<UInt64>. In fact, BigUInt implements a mutable collection of its UInt64 digits, with the
-digit at index 0 being the least significant.
-
-
To make memory management simple, BigUInt allows you to subscript it with out-of-bounds indexes:
-the subscript getter zero-extends the digit sequence, while the subscript setter automatically extends the
-underlying storage when necessary:
-
-
var number = BigUInt(1)
-number[42] // Not an error, returns 0
-number[23] = 1 // Not an error, number is now 2^1472 + 1.
-
-
-
Note that it is rarely a good idea to use big integers as collections; in the vast majority of cases it is much
-easier to work with the provided high-level methods and operators rather than with raw big digits.
+around Array<UInt64>. (In fact, BigUInt only uses an array if there are more than two digits.)
See more
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:
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:
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.
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.
+ 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.
+
+
+
+
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)->BigInt
+
+
+
+
+
Return Value
+
1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
An arbitary precision unsigned integer type, also known as a big integer.
-
Operations on big integers never overflow, but they might take a long time to execute.
+
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 wrapper
-around Array<UInt64>. In fact, BigUInt implements a mutable collection of its UInt64 digits, with the
-digit at index 0 being the least significant.
-
-
To make memory management simple, BigUInt allows you to subscript it with out-of-bounds indexes:
-the subscript getter zero-extends the digit sequence, while the subscript setter automatically extends the
-underlying storage when necessary:
-
-
var number = BigUInt(1)
-number[42] // Not an error, returns 0
-number[23] = 1 // Not an error, number is now 2^1472 + 1.
-
-
-
Note that it is rarely a good idea to use big integers as collections; in the vast majority of cases it is much
-easier to work with the provided high-level methods and operators rather than with raw big digits.
+around Array<UInt64>. (In fact, BigUInt only uses an array if there are more than two digits.)
The number of leading zero bits in the binary representation of this integer in base 2^Digit.width.
-This is useful when you need to normalize a BigUInt such that the top bit of its most significant digit is 1.
-
+
Create a big integer consisting of width uniformly distributed random bits.
Note
- 0 is considered to have zero leading zero bits.
-
-
-
-
-
Returns
- A value in 0...(Digit.width - 1).
-
-
-
-
-
See also
- width
-
-
-
-
-
Complexity
- O(1)
+ This function uses arc4random_buf to generate random bits.
Calculate the bitwise AND of a and b and return the result.
+
Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.
-
-
Complexity
- O(max(a.count, b.count))
+
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.
Calculate the bitwise OR of a and b, and store the result in a.
+
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.
+
+
Note
+ 0 is considered to have zero leading zero bits.
+
+
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.
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.
+
+
Note
+ If the result indicates an overflow, then self becomes the twos’ complement of the absolute difference.
+
+
Subtract a digit d from this integer in place, returning a flag that is true if the operation
-caused an arithmetic overflow. d is shifted shift digits to the left before being subtracted.
-
+
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.
Note
- If the result is true, then self becomes the two’s complement of the absolute difference.
+ If overflow is true, then the result value is the twos’ complement of the absolute value of the difference.
Subtract a digit d from this integer, returning the difference and a flag that is true if the operation
-caused an arithmetic overflow. d is shifted shift digits to the left before being subtracted.
-
+
Subtracts other from self, returning the result and a flag indicating arithmetic overflow.
Note
- If overflow is true, then the returned value is the two’s complement of the absolute difference.
+ When the operation overflows, then partialValue is the twos’ complement of the absolute value of the difference.
Subtract b from this integer in place, and return true iff the operation caused an
-arithmetic overflow. b is shifted shift digits to the left before being subtracted.
-
-
-
Note
- If the result is true, then self becomes the twos’ complement of the absolute difference.
+
Subtract b from this integer, returning the difference and a flag that is true if the operation caused an
-arithmetic overflow. b is shifted shift digits to the left before being subtracted.
-
-
-
Note
- If overflow is true, then the result value is the twos’ complement of the absolute value of the difference.
+
Subtracts rhs from lhs, returning the result and a Bool that is true iff the operation caused an arithmetic overflow.
-Overflow is returned if and only if lhs is less than rhs, in which case the result is the twos’ complement of the absolute difference.
+
Divide x by y and store the remainder in x.
+
+
Note
+ Use divided(by:) if you also need the remainder.
+
+
Explicitly convert to IntMax, trapping on overflow.
+
Get or set a digit at a given index.
+
+
Note
+ Unlike a normal collection, it is OK for the index to be greater than or equal to endIndex.
+The subscripting getter returns zero for indexes beyond the most significant digit.
+Setting these extended digits automatically appends new elements to the underlying digit array.
-
- Unlike a normal collection, it is OK for the index to be greater than or equal to endIndex.
-The subscripting getter returns zero for indexes beyond the most significant digit.
-Setting these extended digits automatically appends new elements to the underlying digit array.
-
-
-
-
-
Requires
- index >= 0
-
-
-
-
-
Complexity
- The getter is O(1). The setter is O(1) if the conditions below are true; otherwise it’s O(count).
-
-
-
The integer’s storage is not shared with another integer
-
The integer wasn’t created as a slice of another integer
Multiply x by y, and add the result to this integer, optionally shifted shift digits to the left.
-
+
Multiply x by y, and add the result to this integer, optionally shifted shift words to the left.
Note
This is the fused multiply/shift/add operation; it is more efficient than doing the components
individually. (The fused operation doesn’t need to allocate space for temporary big integers.)
-
-
-
Returns
- self is set to self + (x * y) << (shift * 2^Digit.width)
-
-
- 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.
+
+
Requires
+ modulus > 1
-
Complexity
O(count^3)
@@ -3667,11 +2742,10 @@
Declaration
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.
-
+
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.
- 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.
-
-
-
-
-
Returns
- 1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
-
-
-
-
-
See also
- BigUInt.power(_:, modulus:)
-
-
-
-
-
Complexity
- O((exponent * self.count)^log2(3)) or somesuch. The result may require a large amount of memory, too.
-
-
+
Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than value.
Declaration
Swift
-
publicfuncpower(_exponent:Int)->BigUInt
+
publicfuncsquareRoot()->BigUInt
Return Value
-
1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
+ 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.
An arbitary precision unsigned integer type, also known as a big integer.
-
Operations on big integers never overflow, but they might take a long time to execute.
+
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 wrapper
-around Array<UInt64>. In fact, BigUInt implements a mutable collection of its UInt64 digits, with the
-digit at index 0 being the least significant.
-
-
To make memory management simple, BigUInt allows you to subscript it with out-of-bounds indexes:
-the subscript getter zero-extends the digit sequence, while the subscript setter automatically extends the
-underlying storage when necessary:
-
-
var number = BigUInt(1)
-number[42] // Not an error, returns 0
-number[23] = 1 // Not an error, number is now 2^1472 + 1.
-
-
-
Note that it is rarely a good idea to use big integers as collections; in the vast majority of cases it is much
-easier to work with the provided high-level methods and operators rather than with raw big digits.
+around Array<UInt64>. (In fact, BigUInt only uses an array if there are more than two digits.)
See more
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:
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:
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.
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.
+ 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.
+
+
+
+
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)->BigInt
+
+
+
+
+
Return Value
+
1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
An arbitary precision unsigned integer type, also known as a big integer.
-
Operations on big integers never overflow, but they might take a long time to execute.
+
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 wrapper
-around Array<UInt64>. In fact, BigUInt implements a mutable collection of its UInt64 digits, with the
-digit at index 0 being the least significant.
-
-
To make memory management simple, BigUInt allows you to subscript it with out-of-bounds indexes:
-the subscript getter zero-extends the digit sequence, while the subscript setter automatically extends the
-underlying storage when necessary:
-
-
var number = BigUInt(1)
-number[42] // Not an error, returns 0
-number[23] = 1 // Not an error, number is now 2^1472 + 1.
-
-
-
Note that it is rarely a good idea to use big integers as collections; in the vast majority of cases it is much
-easier to work with the provided high-level methods and operators rather than with raw big digits.
+around Array<UInt64>. (In fact, BigUInt only uses an array if there are more than two digits.)
The number of leading zero bits in the binary representation of this integer in base 2^Digit.width.
-This is useful when you need to normalize a BigUInt such that the top bit of its most significant digit is 1.
-
+
Create a big integer consisting of width uniformly distributed random bits.
Note
- 0 is considered to have zero leading zero bits.
-
-
-
-
-
Returns
- A value in 0...(Digit.width - 1).
-
-
-
-
-
See also
- width
-
-
-
-
-
Complexity
- O(1)
+ This function uses arc4random_buf to generate random bits.
Calculate the bitwise AND of a and b and return the result.
+
Returns true if this integer is probably prime. Returns false if this integer is definitely not prime.
-
-
Complexity
- O(max(a.count, b.count))
+
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.
Calculate the bitwise OR of a and b, and store the result in a.
+
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.
+
+
Note
+ 0 is considered to have zero leading zero bits.
+
+
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.
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.
+
+
Note
+ If the result indicates an overflow, then self becomes the twos’ complement of the absolute difference.
+
+
Subtract a digit d from this integer in place, returning a flag that is true if the operation
-caused an arithmetic overflow. d is shifted shift digits to the left before being subtracted.
-
+
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.
Note
- If the result is true, then self becomes the two’s complement of the absolute difference.
+ If overflow is true, then the result value is the twos’ complement of the absolute value of the difference.
Subtract a digit d from this integer, returning the difference and a flag that is true if the operation
-caused an arithmetic overflow. d is shifted shift digits to the left before being subtracted.
-
+
Subtracts other from self, returning the result and a flag indicating arithmetic overflow.
Note
- If overflow is true, then the returned value is the two’s complement of the absolute difference.
+ When the operation overflows, then partialValue is the twos’ complement of the absolute value of the difference.
Subtract b from this integer in place, and return true iff the operation caused an
-arithmetic overflow. b is shifted shift digits to the left before being subtracted.
-
-
-
Note
- If the result is true, then self becomes the twos’ complement of the absolute difference.
+
Subtract b from this integer, returning the difference and a flag that is true if the operation caused an
-arithmetic overflow. b is shifted shift digits to the left before being subtracted.
-
-
-
Note
- If overflow is true, then the result value is the twos’ complement of the absolute value of the difference.
+
Subtracts rhs from lhs, returning the result and a Bool that is true iff the operation caused an arithmetic overflow.
-Overflow is returned if and only if lhs is less than rhs, in which case the result is the twos’ complement of the absolute difference.
+
Divide x by y and store the remainder in x.
+
+
Note
+ Use divided(by:) if you also need the remainder.
+
+
Explicitly convert to IntMax, trapping on overflow.
+
Get or set a digit at a given index.
+
+
Note
+ Unlike a normal collection, it is OK for the index to be greater than or equal to endIndex.
+The subscripting getter returns zero for indexes beyond the most significant digit.
+Setting these extended digits automatically appends new elements to the underlying digit array.
-
- Unlike a normal collection, it is OK for the index to be greater than or equal to endIndex.
-The subscripting getter returns zero for indexes beyond the most significant digit.
-Setting these extended digits automatically appends new elements to the underlying digit array.
-
-
-
-
-
Requires
- index >= 0
-
-
-
-
-
Complexity
- The getter is O(1). The setter is O(1) if the conditions below are true; otherwise it’s O(count).
-
-
-
The integer’s storage is not shared with another integer
-
The integer wasn’t created as a slice of another integer
Multiply x by y, and add the result to this integer, optionally shifted shift digits to the left.
-
+
Multiply x by y, and add the result to this integer, optionally shifted shift words to the left.
Note
This is the fused multiply/shift/add operation; it is more efficient than doing the components
individually. (The fused operation doesn’t need to allocate space for temporary big integers.)
-
-
-
Returns
- self is set to self + (x * y) << (shift * 2^Digit.width)
-
-
- 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.
+
+
Requires
+ modulus > 1
-
Complexity
O(count^3)
@@ -3667,11 +2742,10 @@
Declaration
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.
-
+
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.
- 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.
-
-
-
-
-
Returns
- 1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
-
-
-
-
-
See also
- BigUInt.power(_:, modulus:)
-
-
-
-
-
Complexity
- O((exponent * self.count)^log2(3)) or somesuch. The result may require a large amount of memory, too.
-
-
+
Returns the integer square root of a big integer; i.e., the largest integer whose square isn’t greater than value.
Declaration
Swift
-
publicfuncpower(_exponent:Int)->BigUInt
+
publicfuncsquareRoot()->BigUInt
Return Value
-
1 if exponent == 0, otherwise self raised to exponent. (This implies that 0.power(0) == 1.)
+ 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.
This repository provides integer types of arbitrary width implemented
in 100% pure Swift. The underlying representation is in base 2^64, using Array<UInt64>.
Two big integer types are included: BigUInt and BigInt,
+
Two big integer types are included: BigUInt and BigInt,
the latter being the signed variant.
Both of these are Swift structs with copy-on-write value semantics, and they can be used much
like any other integer type.
Addition and subtraction have variants that allow for
shifting the digits of the second operand on the fly.
Unsigned subtraction will trap when the result would be negative.
-(There are variants that return an overflow flag.)
-
Multiplication uses brute force for numbers up to 1024 digits, then switches to Karatsuba’s recursive method.
-(This limit is configurable, see BigUInt.directMultiplicationLimit.)
BigInt 2.1.0 requires Swift 3.0.1. (The last version with support for Swift 3.0.0 was BigInt 2.0.1.
-The last version with support for Swift 2.2 was BigInt 1.3.0.)
+
BigInt 3.0.0 requires Swift 4. (The last version with support for Swift 3.x was BigInt 2.1.0.
+The last version with support for Swift 2 was BigInt 1.3.0.)
BigInt deploys to macOS 10.10, iOS 9, watchOS 2 and tvOS 9.
It has been tested on the latest OS releases only—however, as the module uses very few platform-provided APIs,
@@ -209,37 +210,37 @@
Swift Package Manager:
Although the Package Manager is still in its infancy, BigInt provides experimental support for it.
Add this to the dependency section of your Package.swift manifest:
BigUInt is a MutableCollectionType of its 64-bit digits, with the least significant
-digit at index 0. As a convenience, BigUInt allows you to subscript it with indexes at
-or above its count. The subscript operator returns 0 for out-of-bound gets and
+
BigUInt is a MutableCollectionType of its 64-bit digits, with the least significant
+digit at index 0. As a convenience, BigUInt allows you to subscript it with indexes at
+or above its count. The subscript operator returns 0 for out-of-bound gets and
automatically extends the array on out-of-bound sets. This makes memory management simpler.
-
BigInt is just a tiny wrapper around a BigUIntabsolute value and a
-sign bit, both of which are accessible as public read-write properties.
I haven’t found (64,64)->128 multiplication or (128,64)->64 division operations
-in Swift, so the module has generic implementations for them in terms of the standard
+in Swift, so the module has generic implementations for them in terms of the standard
single-width * and / operators. I suspect there are LLVM intrinsics for full-width
arithmetics that are probably accessible somehow, though. (Let me know if you know how!)
This sounds slow, but 64-bit digits are
still considerably faster than 32-bit, even though the latter can use direct 64-bit arithmetic to
implement these primitives.
The types provided by BigInt are not parametric—this is very much intentional, as
+
The types provided by BigInt are not parametric—this is very much intentional, as
Swift generics cost us dearly at runtime in this use case. In every approach I tried,
making arbitrary-precision arithmetic operations work with a generic Digit type parameter
resulted in code that was literally ten times slower. If you can make the algorithms generic
@@ -249,14 +250,14 @@
implementations for arbitrary-width arithmetic operations. (Polynomial division and decimal bases
are two examples.) The library already implements double-digit multiplication and division as
extension methods on a protocol with an associated type requirement; this has not measurably affected
-performance. Unfortunately, the same is not true for BigUInt’s methods.
+performance. Unfortunately, the same is not true for BigUInt‘s methods.
Of course, as a last resort, we could just duplicate the code to create a separate
generic variant that was slower but more flexible.
Let’s start with a simple function that generates a random n-bit prime. The module
@@ -376,7 +377,7 @@
Let’s try out our new keypair by converting a string into UTF-8, interpreting
the resulting binary representation as a big integer, and encrypting it with the
-public key. BigUInt has an initializer that takes an NSData, so this is pretty
+public key. BigUInt has an initializer that takes an NSData, so this is pretty
easy to do:
let secret: BigUInt = BigUInt("Arbitrary precision arithmetic is fun!".dataUsingEncoding(NSUTF8StringEncoding)!)
==> 83323446846105976078466731524728681905293067701804838925389198929123912971229457
@@ -407,9 +408,9 @@
Yay! This is truly terrific, but please don’t use this example code in an actual
cryptography system. RSA has lots of subtle (and some not so subtle) complications
that we ignored to keep this example short.
Another fun activity to try with BigInts is to generate the digits of π.
+
Another fun activity to try with BigInts is to generate the digits of π.
Let’s try implementing Jeremy Gibbon’s spigot algorithm.
This is a rather slow algorithm as π-generators go, but it makes up for it with its grooviness
factor: it’s remarkably short, it only uses (big) integer arithmetic, and every iteration
@@ -469,8 +470,8 @@
Initialize a big integer from an ASCII representation in a given radix. Numerals above 9 are represented by","parent_name":"BigInt"},"Structs/BigInt.html#/s:6BigIntAAVABs7UnicodeO6ScalarV07unicodeD7Literal_tcfc":{"name":"init(unicodeScalarLiteral:)","abstract":"
Initialize a new big integer from a Unicode scalar.","parent_name":"BigInt"},"Structs/BigInt.html#/s:6BigIntAAVABSS30extendedGraphemeClusterLiteral_tcfc":{"name":"init(extendedGraphemeClusterLiteral:)","abstract":"
Initialize a new big integer from an extended grapheme cluster.","parent_name":"BigInt"},"Structs/BigInt.html#/s:6BigIntAAVABSS13stringLiteral_tcfc":{"name":"init(stringLiteral:)","abstract":"
Initialize a new big integer from a decimal number represented by a string literal of arbitrary length.","parent_name":"BigInt"},"Structs/BigInt.html#/s:6BigIntAAV11descriptionSSv":{"name":"description","abstract":"
Return the decimal representation of this integer.
The number of leading zero bits in the binary representation of this integer in base 2^(Word.bitWidth).","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV20trailingZeroBitCountSiv":{"name":"trailingZeroBitCount","abstract":"
The number of trailing zero bits in the binary representation of this integer.
Subtract other from this integer in place, and return a flag indicating if the operation caused an","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV28subtractingReportingOverflowAC12partialValue_Sb8overflowtAC_Si9shiftedBytF":{"name":"subtractingReportingOverflow(_:shiftedBy:)","abstract":"
Subtract other from this integer, returning the difference and a flag indicating arithmetic overflow.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV28subtractingReportingOverflowAC12partialValue_Sb8overflowtACF":{"name":"subtractingReportingOverflow(_:)","abstract":"
Subtracts other from self, returning the result and a flag indicating arithmetic overflow.
Subtract other from this integer in place.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV11subtractingA2C_Si9shiftedBytF":{"name":"subtracting(_:shiftedBy:)","abstract":"
Subtract b from this integer, and return the difference.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV9decrementySi9shiftedBy_tF":{"name":"decrement(shiftedBy:)","abstract":"
Initialize a big integer from an ASCII representation in a given radix. Numerals above 9 are represented by","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntVACSgs9SubstringV_Si5radixtcfc":{"name":"init(_:radix:)","abstract":"
Initialize a new big integer from a Unicode scalar.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntVACSS30extendedGraphemeClusterLiteral_tcfc":{"name":"init(extendedGraphemeClusterLiteral:)","abstract":"
Initialize a new big integer from an extended grapheme cluster.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntVACSS13stringLiteral_tcfc":{"name":"init(stringLiteral:)","abstract":"
Initialize a new big integer from a decimal number represented by a string literal of arbitrary length.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV11descriptionSSv":{"name":"description","abstract":"
Return the decimal representation of this integer.
Initializes an integer from the bits stored inside a piece of Data.","parent_name":"BigUInt"},"Structs/BigUInt.html#/s:6BigInt0A4UIntV9serialize10Foundation4DataVyF":{"name":"serialize()","abstract":"
Return a Data value that contains the base-256 representation of this integer, in network (big-endian) byte order.