BigInt
public struct BigInt:
SignedNumeric, // Implies Numeric, Equatable, ExpressibleByIntegerLiteral
BinaryInteger, // Implies Hashable, CustomStringConvertible, Strideable, Comparable
ExpressibleByFloatLiteral,
Codable
BigNumber is an arbitrary precision integer value type. It stores a number in base 2^64 notation as an array.
Each element of the array is called a limb, which is of type UInt64, the whole array is called limbs and has the type [UInt64]. A boolean sign variable determines if the number is positive or negative. If sign == true, then the number is smaller than 0, otherwise it is greater or equal to 0. It stores the 64 bit digits in little endian, that is, the least significant digit is stored in the array index 0:
limbs == [] := undefined, should throw an error
limbs == [0], sign == false := 0, defined as positive
limbs == [0], sign == true := undefined, should throw an error
limbs == [n] := n if sign == false, otherwise -n, given 0 <= n < 2^64
limbs == [l0, l1, l2, ..., ln] :=
(l0 * 2^(0*64)) +
(11 * 2^(1*64)) +
(12 * 2^(2*64)) +
... +
(ln * 2^(n*64))
-
Declaration
Swift
public typealias Magnitude = UInt64
-
Declaration
Swift
public var magnitude: UInt64 { get }
-
Declaration
Swift
public typealias Words = [UInt]
-
Declaration
Swift
public var words: BigInt.Words { get }
-
Returns the size of the BigNumber in bits.
Declaration
Swift
public var size: Int { get }
-
Returns a formated human readable string that says how much space (in bytes, kilobytes, megabytes, or gigabytes) the BigNumber occupies.
Declaration
Swift
public var sizeDescription: String { get }
-
Create an instance initialized to an integer value.
Declaration
Swift
public init(_ z: Int)
-
Create an instance initialized to an unsigned integer value.
Declaration
Swift
public init(_ n: UInt)
-
Create an instance initialized to a string value.
Declaration
Swift
public init?(_ str: String)
-
Create an instance initialized to a string with the value of mathematical numerical system of the specified radix (base). So for example, to get the value of hexadecimal string radix value must be set to 16.
Declaration
Swift
public init?(_ number: String, radix: Int)
-
Create an instance initialized to a string with the value of mathematical numerical system of the specified radix (base). You have to specify the base as a prefix, so for example, “0b100101010101110” is a vaild input for a binary number. Currently, hexadecimal (0x), octal (0o) and binary (0b) are supported.
Declaration
Swift
public init?(prefixedNumber number: String)
-
Declaration
Swift
public init(floatLiteral value: Double)
-
Declaration
Swift
public init(integerLiteral value: Int)
-
Declaration
Swift
public init?<T>(exactly source: T) where T : BinaryInteger
-
Creates an integer from the given floating-point value, rounding toward zero.
Declaration
Swift
public init<T>(_ source: T) where T : BinaryFloatingPoint
-
Creates a new instance from the given integer.
Declaration
Swift
public init<T>(_ source: T) where T : BinaryInteger
-
Creates a new instance with the representable value that’s closest to the given integer.
Declaration
Swift
public init<T>(clamping source: T) where T : BinaryInteger
-
Creates an integer from the given floating-point value, if it can be represented exactly.
Declaration
Swift
public init?<T>(exactly source: T) where T : BinaryFloatingPoint
-
Creates a new instance from the bit pattern of the given instance by sign-extending or truncating to fit this type.
Declaration
Swift
public init<T>(truncatingIfNeeded source: T) where T : BinaryInteger
-
Declaration
Swift
public var description: String { get }
-
Undocumented
Declaration
Swift
public var locale: Locale
-
returns the current value in scientific notation
Declaration
Swift
public var scientificDescription: String { get }
-
Returns the BigNumber’s value in the given base (radix) as a string.
Declaration
Swift
public func asString(radix: Int) -> String
-
Declaration
Swift
public func hash(into hasher: inout Hasher)
-
Declaration
Swift
public static var isSigned: Bool { get }
-
Declaration
Swift
public var bitWidth: Int { get }
-
Returns -1 if this value is negative and 1 if it’s positive; otherwise, 0.
Declaration
Swift
public func signum() -> BigInt
-
The number of trailing zeros in this value’s binary representation.
Declaration
Swift
public var trailingZeroBitCount: Int { get }
-
Undocumented
Declaration
Swift
public static func << <T>(lhs: BigInt, rhs: T) -> BigInt where T : BinaryInteger
-
Undocumented
Declaration
Swift
public static func <<= <T>(lhs: inout BigInt, rhs: T) where T : BinaryInteger
-
Undocumented
Declaration
Swift
public static func >> <T>(lhs: BigInt, rhs: T) -> BigInt where T : BinaryInteger
-
Undocumented
Declaration
Swift
public static func >>= <T>(lhs: inout BigInt, rhs: T) where T : BinaryInteger
-
Returns the result of performing a bitwise AND operation on the two given values.
Declaration
Swift
public static func & (lhs: BigInt, rhs: BigInt) -> BigInt
-
Stores the result of performing a bitwise AND operation on the two given values in the left-hand-side variable.
Declaration
Swift
public static func &= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
public static func | (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
public static func |= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
public static func ^ (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
public static func ^= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
public prefix static func ~ (x: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
prefix static func + (x: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func += (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func + (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func + (lhs: Int, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func + (lhs: BigInt, rhs: Int) -> BigInt
-
Undocumented
Declaration
Swift
static func += (lhs: inout Int, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func += (lhs: inout BigInt, rhs: Int)
-
Declaration
Swift
mutating func negate()
-
Undocumented
Declaration
Swift
prefix static func - (n: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func - (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func - (lhs: Int, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func - (lhs: BigInt, rhs: Int) -> BigInt
-
Undocumented
Declaration
Swift
static func -= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func -= (lhs: inout Int, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func -= (lhs: inout BigInt, rhs: Int)
-
Undocumented
Declaration
Swift
static func * (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func * (lhs: Int, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func * (lhs: BigInt, rhs: Int) -> BigInt
-
Undocumented
Declaration
Swift
static func *= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func *= (lhs: inout Int, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func *= (lhs: inout BigInt, rhs: Int)
-
Undocumented
Declaration
Swift
static func ** (lhs: BigInt, rhs: Int) -> BigInt
-
Returns the quotient and remainder of this value divided by the given value.
Declaration
Swift
func quotientAndRemainder(dividingBy rhs: BigInt) -> (quotient: BigInt, remainder: BigInt)
-
Undocumented
Declaration
Swift
static func / (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func / (lhs: Int, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func / (lhs: BigInt, rhs: Int) -> BigInt
-
Undocumented
Declaration
Swift
static func /= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func /= (lhs: inout BigInt, rhs: Int)
-
Undocumented
Declaration
Swift
static func % (lhs: BigInt, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func % (lhs: Int, rhs: BigInt) -> BigInt
-
Undocumented
Declaration
Swift
static func % (lhs: BigInt, rhs: Int) -> BigInt
-
Undocumented
Declaration
Swift
static func %= (lhs: inout BigInt, rhs: BigInt)
-
Undocumented
Declaration
Swift
static func %= (lhs: inout BigInt, rhs: Int)
-
Undocumented
Declaration
Swift
static func == (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func == <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
-
Undocumented
Declaration
Swift
static func == <T>(lhs: T, rhs: BigInt) -> Bool where T : BinaryInteger
-
Undocumented
Declaration
Swift
static func != (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func != <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
-
Undocumented
Declaration
Swift
static func != <T>(lhs: T, rhs: BigInt) -> Bool where T : BinaryInteger
-
Undocumented
Declaration
Swift
static func < (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func < <T>(lhs: BigInt, rhs: T) -> Bool where T : BinaryInteger
-
Undocumented
Declaration
Swift
static func < (lhs: Int, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func < (lhs: BigInt, rhs: Int) -> Bool
-
Undocumented
Declaration
Swift
static func > (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func > (lhs: Int, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func > (lhs: BigInt, rhs: Int) -> Bool
-
Undocumented
Declaration
Swift
static func <= (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func <= (lhs: Int, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func <= (lhs: BigInt, rhs: Int) -> Bool
-
Undocumented
Declaration
Swift
static func >= (lhs: BigInt, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func >= (lhs: Int, rhs: BigInt) -> Bool
-
Undocumented
Declaration
Swift
static func >= (lhs: BigInt, rhs: Int) -> Bool
-
Returns the corresponding Fibonacci number, commonly denoted F_n, form a sequence, called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.
Declaration
Swift
var fibonacci: BigInt { get }
-
Check if the BigInt is a prime.
Uses the multiple of 6 method (which is fairly quick and 100% safe) for relatively small integers. For bigger ones, we switch to Miller-Rabin algorithm with 30 precision.
Declaration
Swift
var isPrime: Bool { get }
-
Returns true if (2 ** exp) - 1 is a mersenne prime.
Declaration
Swift
var isMersenne: Bool { get }
-
“Prime Factorization” is finding which prime numbers multiply together to make the original number.
Here are some examples:
BigInt(12).primeFactors //=> [2, 2, 3] because 12 = 2 * 2 * 3
Declaration
Swift
var primeFactors: [BigInt] { get }
-
The Miller–Rabin test relies on an equality or set of equalities that hold true for prime values, then checks whether or not they hold for a number that we want to test for primality.
Declaration
Swift
func millerRabin(accuracy k: Int = 30) -> Bool
Parameters
k
a parameter that determines the accuracy of the test
Return Value
composite if
self
is composite, otherwise probably prime -
Counts the numbr of prime number before itself
Declaration
Swift
var primePi: Int { get }
-
Generate a random BigInt
⚠️ This isn’t crypto secure
Declaration
Swift
static func randomBigNumber(bits n: Int) -> BigInt
Parameters
n
Length of random number (in terms of bits)
-
Alias of
randomBigNumber
Declaration
Swift
static let random: (Int) -> BigInt
-
RAND returns an evenly distributed random real number greater than or equal to 0 and less than 1.
A new random real number is returned every time the worksheet is calculated.
Declaration
Swift
static func rand() -> BigDouble
-
Returns a random integer number between the numbers you specify. A new random integer number is returned every time the worksheet is calculated.
Parameters
a
The smallest integer RANDBETWEEN will return.
b
The largest integer RANDBETWEEN will return.