1 Star 0 Fork 0

wdxtub / cryptography-security-note

Create your Gitee Account
Explore and code with more than 6 million developers,Free private repositories !:)
Sign up
Clone or Download
prime.go 1.15 KB
Copy Edit Web IDE Raw Blame History
dawang authored 2021-04-29 16:14 . add math part
package math
import (
"fmt"
"sort"
)
// CheckPrime 检查是否是素数
func CheckPrime(num int) bool {
// 小于等于 1 直接返回 false
if num <= 1 {
return false
}
m := 0
for i := 2; i < num-1; i++ {
m = num % i
if m == 0 {
return false
}
}
return true
}
// CheckRelativelyPrime 检查是否互质
func CheckRelativelyPrime(num1, num2 int) bool {
result := EuclidGCD(num1, num2)
return result == 1
}
// EuclidGCD 欧几里得算范求最大公因子
func EuclidGCD(num1, num2 int) int {
if num1 < 0 {
num1 = -num1
}
if num2 < 0 {
num2 = -num2
}
if num1+num2 == 0 {
return -1
}
// 要保证后面的数更大
if num1 > num2 {
num1, num2 = num2, num1
}
a := num2
for num1 > 0 {
a = num1
num1 = num2 % num1
num2 = a
}
return a
}
// QuadraticResidue 求解平方余数
func QuadraticResidue(num int) []string {
result := []string{}
tmp := map[int]int{}
unsort := []int{}
for i := 1; i < num; i++ {
rest := i * i % num
tmp[rest] += 1
}
// 排序
for k := range tmp {
unsort = append(unsort, k)
}
sort.Ints(unsort)
for _, k := range unsort {
result = append(result, fmt.Sprintf("%d", k))
}
return result
}

Comment ( 0 )

Sign in to post a comment

Go
1
https://gitee.com/wdxtub/cryptography-security-note.git
git@gitee.com:wdxtub/cryptography-security-note.git
wdxtub
cryptography-security-note
cryptography-security-note
master

Search