-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy path整数因子划分.cpp
More file actions
54 lines (52 loc) · 1.19 KB
/
整数因子划分.cpp
File metadata and controls
54 lines (52 loc) · 1.19 KB
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
#include<bits/stdc++.h>
using namespace std;
int Pollard(int n);
int Random(int a, int b);
int CommFactor(int m, int n);
int main()
{
int x, y;
cout<<"请输入一个整数:";
cin>>x;
y = Pollard(x);
if (y == 0)
cout<<"本次运行失败"<<endl;
else
cout<<x<<"的一个因子是:"<<y<<endl;
return 0;
}
int Pollard(int n)
{
int a, b, c, d;
a = Random(1, n - 1); // a为[1, n-1]区间的随机整数
b = (a * a) % n;
while (b < n - 1)
{
c = (int)sqrt(b);
if (c * c != b * b)
b = b + n; //求相应的b,满足b是一个完全平方数
}
if (b == n - 1)
return 0; //本次算法执行失败
else
d = CommFactor(a + b, n); //调用欧几里德算法求最大公约数
if (d > 1)
return d; //若d为n的非平凡因子
else
return 0; //本次算法执行失败
}
int Random(int a, int b)
{
return (rand( )%(b-a) + a); //rand( )产生[0, 32767]之间的随机整数
}
int CommFactor(int m, int n)
{
int r = m % n;
while (r != 0)
{
m = n;
n = r;
r = m % n;
}
return n;
}