这一部分是关于质数的算法,包括最基本的判定质数、分解质因数,以及线性质数筛,欧拉函数等等。
IsPrime
判断传入的整数是不是质数,并返回一个布尔值。
参数
Name |
Type |
Description |
p |
int |
待判断是否为质数的整数 |
返回值
Name |
Type |
Description |
- |
bool |
如果传入的参数是质数返回true,不是的话返回false |
使用示例
x := algorithm.IsPrime(7)
y := algorithm.IsPrime(9)
log.Println(x, y)
//代码输出
true false
DevidePrime
任何整数都可以分解成形如
的标准形式。其中
是整数N的质因子,
表示N包含质因子
的个数。
DevidePrime可以对传入的整数N进行标准化,返回一个int-int的map类型,key代表
,value代表
。
参数
Name |
Type |
Description |
p |
int |
待分解的整数 |
返回值
Name |
Type |
Description |
res |
map[int]int |
key代表质因子,value代表对应的幂数。 |
使用示例
x := algorithm.DevidePrime(435)
log.Println(x)
//代码输出
map[3:1 5:1 29:1]
GetPrime

使用示例
x := algorithm.GetPrime(100)
log.Println(x)
//代码输出
[2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73
79 83 89 97]
Phi
使用示例
x := algorithm.Phi(9)
log.Println(x)
//代码输出
6
BatchPhi



该函数结合线性质数筛进行运算,时间复杂度为O(n)
使用示例
x := algorithm.BatchPhi(100)
log.Println(x)
//代码输出
[114514 1 1 2 2 4 2 6 4 6 4 10 4 12 6 8 8 16 6 18 8 12 10 22
8 20 12 18 12 28 8 30 16 20 16 24 12 36 18 24 16 40 12 42 20 24 22 46 16 42 20
32 24 52 18 40 24 36 28 58 16 60 30 36 32 48 20 66 32 44 24 70 24 72 36 40 36 60
24 78 32 54 40 82 24 64 42 56 40 88 24 72 44 60 46 72 32 96 42 60 40]