FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Go/math/modular/inverse.go at master · gitgitcode/Go · GitHub
gitgitcode
/
Go
Public
forked from
TheAlgorithms/Go
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Go
/
math
/
modular
/
inverse.go
Copy path
More file actions
More file actions
Latest commit
History
History
History
27 lines (21 loc) · 789 Bytes
Breadcrumbs
Go
/
math
/
modular
/
inverse.go
Copy path
File metadata and controls
27 lines (21 loc) · 789 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
// inverse.go
// description: Implementation of Modular Inverse Algorithm
// details:
// A simple implementation of Modular Inverse - [Modular Inverse wiki](https://en.wikipedia.org/wiki/Modular_multiplicative_inverse)
// author(s) [Taj](https://github.com/tjgurwara99)
// see inverse_test.go
package
modular
import
(
"errors"
"github.com/TheAlgorithms/Go/math/gcd"
)
// ErrorIntOverflow For asserting that the values do not overflow in Int64
var
ErrorInverse
=
errors
.
New
(
"no Modular Inverse exists"
)
// Inverse Modular function
func
Inverse
(
a
,
b
int64
) (
int64
,
error
) {
gcd
,
x
,
_
:=
gcd
.
Extended
(
a
,
b
)
if
gcd
!=
1
{
return
0
,
ErrorInverse
}
return
((
b
+
(
x
%
b
))
%
b
),
nil
// this is necessary because of Go's use of architecture specific instruction for the % operator.
}
Back
|
FazBrowse Home
|
New Git URL