# Swift Package Index

## DiscreteMathematics

https://github.com/cszatmary/DiscreteMathematics.git

Discrete Mathematics algorithms.

• The latest stable release is 2.0.0. Released over 1 year ago.
• The last commit to master was over 1 year ago.

### Swift Version Compatibility

• 2.0.0 and master
5.3
5.2
5.1
5.0
4.2
Full build results

### Platform Compatibility

• 2.0.0 and master
iOS
macOS(Intel)
macOS(ARM)
Linux
tvOS
watchOS
Full build results

# Discrete Mathematics

DiscreteMathematics is a set of algorithm implementations from Discrete Mathematics.

## Examples

### Operators

#### Congruence Modulo n `==%`:

The equivalence relation a ≡ b(mod m) ↔ m | (b - a).

``````-8 ==% (7, 5) // true
2 ==% (8, 5) // false
``````

#### Divides `|%`:

Returns whether or not a|b ↔ ∃ q ∈ N, b = q · a.

``````3 |% 9 // true
2 |% 7 // false
``````

### Functions

#### Long Division

Performs division on two integers and returns the quotient and remainder.

a = q · b + r

``````longDivision(a: 8, b: 3) // (q: 2, r: 2)
``````

#### Greatest Common Divisor

`gcd()` returns the greatest common divisor using the Euclidean Algorithm.

``````gcd(5005, 4410) // 35
gcd(175, 155) // 5
``````

`egcd()` returns the GCD of two integers as an integer combination using the Extended Euclidean Algorithm.

a · x + b · y = d

``````egcd(5005, 4410) // (d: 35, x: -37, y: 42)
egcd(175, 155) // (d: 5, x: 8, y: -9)
``````

#### Coprime

Two elements, a, b, are coprime if gcd(a, b) = 1.

``````coprime(17, -60) // true
``````

#### Linear Diophantine Equation

`lde()` returns a solution to the given Linear Diophantine Equation or `nil` if it has no solutions.

``````lde(a: 175, b: 155, c: 50) // (x: 80, y: -90)
lde(a: 234, b: 182, c: 10) // nil
``````

`ldeSolutions()` returns a function that will compute all possible solutions to an LDE.

``````let solutions = ldeSolutions(a: 175, b: 155, c: 50)
solutions!(3) // (173, -195)
``````

## Installation

### Swift Package Manager

The recommended way to install `DiscreteMathematics` is by using the Swift Package Manager.

To install it, add the following to your `Package.swift`'s `dependencies` array:

``````.package(url: "https://github.com/cszatma/DiscreteMathematics.git", from: "2.0.0")
``````

### CocoaPods

DiscreteMathematics is also available through CocoaPods.

``````pod 'DiscreteMathematics', '~> 2.0'