本作业是一项实践操作。GMP是一种非常著名的多精度算术的Library,本作业包括以下内容:
1、阅读浏览GMP的主页,了解GMP的使用,https://gmplib.org/
2、安装GMP包
3、写一个C/C++程序,调用GMP包中的函数完成以下工作:a、生成两个随机的大素数p和q,分别有512比特长。
b、把p和q相乘,赋值为n。
c、求比n小且与n互素的整数的个数,记为euler,并输出:p、q、n和euler。4、提交代码和运行截图。
安装与使用GMP包
传送门:https://blog.csdn.net/shangsongwww/article/details/95623176
代码
最开始是逐个遍历,过了老半天觉着不对劲,查了下得用欧拉函数…
https://zhuanlan.zhihu.com/p/151756874
https://blog.csdn.net/liuzibujian/article/details/81086324
https://blog.csdn.net/qq_34446253/article/details/51839005
(先说结果,跑了一星期没运行完,跑那么久估计都是跑在求质数上了
(因为没用VS2019的调试,代码也没设置什么“进度条”之类的东西(当时想着应该不会那么久所以没去弄),所以跑了一星期到底运行到什么程度,完全没数
#pragma warning(disable:4146)//屏蔽C4146警告
#include "gmp.h"
#pragma comment(lib,"libgmp-10.lib")
#include<time.h>
class MyTimer {
//简易计时器
private:
clock_t clock_start;
clock_t clock_stop;
public:
void Start() {
clock_start = clock(); }
void Stop() {
clock_stop = clock(); }
MyTimer() :clock_start(0), clock_stop(0) {
}
public:
struct Time {
long day;
long hour;
long minute;
long second;
};
Time GetResult(unsigned Switch=0b0111) {
//second(1),minute(2),hour(4),day(8)。需要哪几个数据就对应置1
//例如传入Switch=0B0101(代表仅取”时“和”秒“的单位),若当前计时器计时4100秒,那返回的Time的结果为:day=0,hour=1,minute=0,second=500
Time rst;
rst.second = (clock_stop - clock_start)/1000;
rst.minute= rst.second / 60;
rst.hour= rst.minute / 60;
rst.day= rst.hour / 24;
if (Switch & 0b1000) {
//保留“天”
rst.hour -= rst.day * 24;
rst.minute -= rst.day * 24 * 60;
rst.second -= rst.day * 24 * 60 * 60;
}
else
rst.day = 0;
if (Switch & 0b0100) {
//保留“小时”
rst.minute -= rst.hour*60;
rst.second -= rst.hour*60*60;
}
else
rst.hour = 0;
if (Switch & 0b0010) {
//保留“分钟”
rst.second -= rst.minute * 60;
}
else
rst.minute = 0;
if ((Switch & 0b0001) == 0)
rst.second = 0;
return rst;
}
};
#include<vector>
class Vector_mpzT {
//制作极其简陋的mpz_t数组(能跑就行
private:
std::vector<mpz_ptr>lst;
public:
~Vector_mpzT() {
for (auto p = lst<