# Operations / Functions

### BigDouble Operators

• Returns the absolute value of the given number.

#### Declaration

Swift

public func abs(_ x: BigDouble) -> BigDouble

#### Parameters

 x a big double
• round to smallest BigNumber value not less than base

#### Declaration

Swift

public func ceil(_ base: BigDouble) -> BigInt
• Combinations: \frac{n!}{k! * (n - k)!}.

See combinationsWithRepitition for the other method.

Order matters, repetition allowed.

#### Declaration

Swift

public func combinations(_ n: Int, _ k: Int) throws -> BigInt
• Combinations with repetitions: \frac{(n + k - 1)!}{k! * (n - 1)!}.

Order matters, repetition allowed.

#### Declaration

Swift

public func combinationsWithRepetitions(_ n: Int, _ k: Int) throws -> BigInt
• round to largest BigNumber value not greater than base

#### Declaration

Swift

public func floor(_ base: BigDouble) -> BigInt
• greatest common divisor.

#### Declaration

Swift

public func gcd(_ a: BigInt, _ b: BigInt) -> BigInt

#### Parameters

 a BigInt b BigInt
• Do not use this, extremely slow. Only for testing purposes.

#### Declaration

Swift

public func gcdEuclid(_ a: BigInt, _ b: BigInt) -> BigInt

#### Parameters

 a Any BigInt b Any BigInt

#### Return Value

GCD of a and b as a BigInt

• lowest (or least) common multiple.

#### Declaration

Swift

public func lcm(_ a: BigInt, _ b: BigInt) -> BigInt

#### Parameters

 a BigInt b BigInt
• Returns the BigDouble that is largest

#### Declaration

Swift

public func max(_ lhs: BigDouble, _ rhs: BigDouble) -> BigDouble
• Returns the BigDouble that is the smallest

#### Declaration

Swift

public func min(_ lhs: BigDouble, _ rhs: BigDouble) -> BigDouble
• Convenience function combinding addition and non-negative modulo operations

#### Declaration

Swift

public func mod_add(_ a: BigInt, _ b: BigInt, _ m: BigInt) -> BigInt

#### Parameters

 a left hand side of the modulo addition b right hand side of the modulo addition m modulus

#### Return Value

nnmod(a + b, m)

• Quick exponentiation/modulo algorithm FIXME: for security, this should use the constant-time Montgomery algorithm to thwart timing attacks

#### Declaration

Swift

public func mod_exp(_ b: BigInt, _ p: BigInt, _ m: BigInt) -> BigInt

#### Parameters

 b base p power m modulus

#### Return Value

pow(b, p) % m

• Non-negative modulo operation

#### Declaration

Swift

public func nnmod(_ a: BigInt, _ m: BigInt) -> BigInt

#### Parameters

 a left hand side of the module operation m modulus

#### Return Value

r := a % b such that 0 <= r < abs(m)

• Permutations: \$\frac{n!}{(n-k)!}

Order matters, repetition allowed.

#### Declaration

Swift

public func permutations(_ n: Int, _ k: Int) throws -> BigInt
• Permutations with repetition: n^k

Order matters, repetition allowed.

#### Declaration

Swift

public func permutationsWithRepitition(_ n: Int, _ k: Int) -> BigInt
• Returns a BigDouble number raised to a given power.

Warning

This may take a while

#### Declaration

Swift

public func pow(_ base: BigDouble, _ exp: Int) -> BigDouble
• Returns a BigDouble number raised to a given power.

Warning

This may take a while

#### Declaration

Swift

public func pow(_ base: BigDouble, _ exp: BigInt) -> BigDouble
• Warning

This may take a while. This is only precise up until precision. When comparing results after pow or ** usenearlyEqual

#### Declaration

Swift

public func pow(_ base: BigDouble, _ exp: BigDouble) -> BigDouble
• The binary GCD algorithm, also known as Stein’s algorithm, is an algorithm that computes the greatest common divisor of two nonnegative integers. Stein’s algorithm uses simpler arithmetic operations than the conventional Euclidean algorithm; it replaces division with arithmetic shifts, comparisons, and subtraction.

#### Declaration

Swift

public func steinGcd(_ a: Limbs, _ b: Limbs) -> Limbs

#### Parameters

 a Any limbs Number b Any limbs Numbers

#### Return Value

GCD of a and b as a Limbs

• Undocumented

#### Declaration

Swift

public func exp(_ r: BigNumber) -> BigNumber

### Encoding a MD5 digest of bytes to a string

• Encodes array of Byte to MD5

#### Declaration

Swift

public func encodeMD5(digest: [Byte]) -> String

#### Parameters

 digest Array of Byte as utf8 array

### BigNumber Utility Functions

• Returns the factorial of a number. The factorial of a number is equal to 1*2*3*…* number.

This relies on Lanczos approximation, provided by gamma

Let’s say you have six bells, each with a different tone, and you want to find the number of unique sequences in which each bell can be rung once. In this example, you are calculating the factorial of six. In general, use a factorial to count the number of ways in which a group of distinct items can be arranged (also called permutations). To calculate the factorial of a number, use this function.

#### Declaration

Swift

public func factorial(_ number: BigInt) throws -> BigInt

#### Parameters

 number The nonnegative number for which you want the factorial. If number is not an integer, it is truncated.
• The gamma function (represented by \Gamma, the capital letter gamma from the Greek alphabet) is one commonly used extension of the factorial function to complex numbers. The gamma function is defined for all complex numbers except the non-positive integers. For any positive integer n: \Gamma (n)=(n-1)!

#### Declaration

Swift

public func gamma(_ float: BigNumber) throws -> BigNumber

#### Parameters

 float A BigNumber
• Returns the multiplicative inverse of this integer in modulo modulus arithmetic, or nil if there is no such number.

Requires

modulus > 1

Complexity

O(count^3)

#### Declaration

Swift

public func inverse(_ base: BigInt, _ modulus: BigInt) -> BigInt?

#### 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.

• Gives you the least (smallest) prime factor of any integer by trial division

#### Declaration

Swift

public func leastFactor(_ number: BigInt) -> BigInt

#### Parameters

 number The integer you want to use
• Gives you the least (smallest) prime factor of any integer by trial division

#### Declaration

Swift

public func leastFactor(_ number: Int) -> Int

#### Parameters

 number The integer you want to use
• Very fast natural logarigthm for very large numbers

#### Declaration

Swift

public func ln(_ n: BigInt) -> BigDouble

#### Parameters

 n Any BigInt

#### Return Value

\ln(n)

• Returns the natural logarithm of a number. Natural logarithms are based on the constant e (2.71828182845904)

LN is the inverse of the EXP function.

#### Declaration

Swift

public func ln(_ n: BigDouble, precision: Int = 15) -> BigDouble

#### Parameters

 n The positive real number for which you want the natural logarithm. precision The precision you want to use (number of cycles). Higher precision means better result, but slower compute time
• The sum function adds values. These values are passed as an argument

#### Declaration

Swift

public func sum(_ numbers: BigNumber...) -> BigNumber

#### Parameters

 numbers The values you want to sum
• The sum function adds values. These values are passed as an argument

#### Declaration

Swift

public func sum(_ numbers: [BigNumber]) -> BigNumber

#### Parameters

 numbers The values you want to sum