forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added a few algorithms that will be used for rewriting RSA algo…
…rithm that currently fails the tests (TheAlgorithms#288) * feat: added a few algorithms that will be used for rsa algorithm * fix: renamed the directory for better import string organisation * fix: rename of a file * updating DIRECTORY.md * fix: corrected the mistakes in the documetation comments of some code * fix: missplelling * fix: spelling errors * fix: reduced cyclomatic complexity Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
- Loading branch information
1 parent
e701407
commit 8bc4e86
Showing
14 changed files
with
175 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,9 @@ | ||
package gcd | ||
|
||
// Gcd finds and returns the greatest common divisor of a given integer. | ||
func Gcd(a, b int) int { | ||
// Recursive finds and returns the greatest common divisor of a given integer. | ||
func Recursive(a, b int64) int64 { | ||
if b == 0 { | ||
return a | ||
} | ||
return Gcd(b, a%b) | ||
return Recursive(b, a%b) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package gcd | ||
|
||
// Iterative Faster iterative version of GcdRecursive without holding up too much of the stack | ||
func Iterative(a, b int64) int64 { | ||
for b != 0 { | ||
a, b = b, a%b | ||
} | ||
return a | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package lcm | ||
|
||
import ( | ||
"math" | ||
|
||
"github.com/TheAlgorithms/Go/math/gcd" | ||
) | ||
|
||
// Lcm returns the lcm of two numbers using the fact that lcm(a,b) * gcd(a,b) = | a * b | | ||
func Lcm(a, b int64) int64 { | ||
return int64(math.Abs(float64(a*b)) / float64(gcd.Iterative(a, b))) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package prime | ||
|
||
import ( | ||
"math/rand" | ||
|
||
"github.com/TheAlgorithms/Go/math/modulararithmetic" | ||
) | ||
|
||
// findD accepts a number and returns the | ||
// odd number d such that num = 2^r * d - 1 | ||
func findRD(num int64) (int64, int64) { | ||
r := int64(0) | ||
d := num - 1 | ||
for num%2 == 0 { | ||
d /= 2 | ||
r++ | ||
} | ||
return d, r | ||
} | ||
|
||
// MillerTest This is the intermediate step that repeats within the | ||
// miller rabin primality test for better probabilitic chances of | ||
// receiving the correct result. | ||
func MillerTest(d, num int64) (bool, error) { | ||
random := rand.Int63n(num-1) + 2 | ||
|
||
res, err := modulararithmetic.ModularExponentiation(random, d, num) | ||
|
||
if err != nil { | ||
return false, err | ||
} | ||
// miller conditions checks | ||
if res == 1 || res == num-1 { | ||
return true, nil | ||
} | ||
|
||
for d != num-1 { | ||
res = (res * res) % num | ||
d *= 2 | ||
if res == 1 { | ||
return false, nil | ||
} | ||
if res == num-1 { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
// MillerRabinTest Probabilistic test for primality of an integer based of the algorithm devised by Miller and Rabin. | ||
func MillerRabinTest(num, rounds int64) (bool, error) { | ||
if num <= 4 { | ||
if num == 2 || num == 3 { | ||
return true, nil | ||
} | ||
return false, nil | ||
} | ||
if num%2 == 0 { | ||
return false, nil | ||
} | ||
d, _ := findRD(num) | ||
|
||
for i := int64(0); i < rounds; i++ { | ||
val, err := MillerTest(d, num) | ||
if err != nil { | ||
return false, err | ||
} | ||
if !val { | ||
return false, nil | ||
} | ||
} | ||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.