-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathRA1.swift
105 lines (95 loc) · 3.28 KB
/
RA1.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//
// RA1.swift
// AlgorithmsSwift
//
// Created by Michael Ho on 12/12/20.
//
class RA1 {
/**
Modular exponentiation, the power in Modular Arithmetic. The following function computes (x^y) % N.
Reference: https://www.geeksforgeeks.org/modular-exponentiation-power-in-modular-arithmetic/
- Parameter x: The number as a base to be calculated.
- Parameter y: The exponential number.
- Parameter N: N The mod number.
- Returns: The computation result.
*/
static func modularExp(_ x: Int, _ y: Int, _ N: Int) -> Int {
var result = 1
var x = x
var y = y
while y > 0 {
/**
If the exponential is not the muliply of 2,
we multiply x once to the result
*/
if y % 2 != 0 {
result = (result * x) % N
}
/**
Now we can directly round y down by 2. We caclulate the modular on
sqaure of x, this reduces the runtime to half.
*/
y /= 2
x = (x * x)%N
}
return result
}
/**
Multiplicative inverse is another name for reciprocal, which tries to find the number to multiply
in order to get 1. For example, if x = 3, N = 11, the result will be 4 since 3*4%11 = 1.
Given two integers x and N, find modular multiplicative inverse of x under modulo N.
- Parameter x: The integer as a base number.
- Parameter N: The modulo N.
- Returns: The computation result.
*/
static func multiplicativeInverse(_ x: Int, _ N: Int) -> Int {
var x = x
x = x%N
// We only need to try N - 1 times so that all remainders
// from modular of N is considered.
for i in 1..<N {
if (x*i)%N == 1 {
return i
}
}
return -1
}
/**
The class covers greatest common divisor methods.
*/
class GreatestCommonDivisor {
/**
The euclid algorithm used to calculate greatest common divisor.
The runtime is O(N^3), where N is the number of bits.
- Parameter x: The larger number to calculate GCD.
- Parameter y: The smaller number to calculate GCD.
- Returns: The greatest common divisor derived by the algorithm.
*/
static func euclidAlgorithm(_ x: Int, _ y: Int) -> Int {
if x == 0 {
return y
}
// x%y takes O(N^2) runtime.
return euclidAlgorithm(y % x, x)
}
/**
Extended euclid algorithm where xα + yβ = gcd(x, y) to compute inverses.
The runtime is O(N^3).
- Parameter x: The larger number.
- Parameter y: The smaller number.
- Returns: The computation result.
*/
func extendedEuclid(_ x: Int, _ y: Int) -> [Int] {
if y == 0 {
return [x, 1, 0]
}
let values = extendedEuclid(y, x % y)
let d = values[0]
let α = values[2]
let β = values[1] - (x / y) * values[2]
// The following means d = xα + yβ, where
// x and y are the original input.
return [d, α, β]
}
}
}