- 在openEuler(推荐)或Ubuntu或Windows(不推荐)中完成下面任务
- 用自己8位学号建两个文件夹xxxxxxxxsrc,xxxxxxxx,到GMP官网https://gmplib.org/下载最新代码到201x1xxxsrc,编译代码并把GMP库安装到201x1xxx文件夹。(5')
- 基于GMP的大数库编写测试代码测试大数运算,计算10000内的素数的乘积(5‘)
- 基于GMP的大数库计算你以及前面5位同学和后面5位同学的8位学号(数字)的乘积(5‘)
1. 用自己8位学号建两个文件夹xxxxxxxxsrc,xxxxxxxx,到GMP官网https://gmplib.org/下载最新代码到201x1xxxsrc,编译代码并把GMP库安装到201x1xxx文件夹。(5')
之前已经下载好gmp
2. 基于GMP的大数库编写测试代码测试大数运算,计算10000内的素数的乘积(5‘)
代码:
include <stdio.h>
include <stdlib.h>
include <gmp.h>
int main() {
mpz_t product, num;
mpz_init(product);
mpz_init(num);
int i, j, prime;
mpz_set_ui(product, 1); // 初始化 product 为 1
for (i = 2; i <= 10000; i++) {
prime = 1;
for (j = 2; j < i; j++) {
if (i % j == 0) {
prime = 0;
break;
}
}
if (prime) {
mpz_set_ui(num, i);
mpz_mul(product, product, num);
}
}
gmp_printf("The product of primes under 10000 is %Zd\n", product);
mpz_clear(product);
mpz_clear(num);
return 0;
}
python测试:
3. 基于GMP的大数库计算你以及前面5位同学和后面5位同学的8位学号(数字)的乘积(5‘)
代码:
标签:product,大数,d%,init,测试,GMP,mpz From: https://www.cnblogs.com/weihehahaha/p/17386948.htmlinclude<gmp.h>
define N 20201329
int main()
{
mpz_t a[11], b;
mpz_init(b);
mpz_init_set_str(b, "1", 10);const char s[11][9]={"20201324","20201325","20201326","20201327","20201328","20201329", "20201330", "20201331", "20201332", "20201201","20201202"};
int i;
for(i=0;i<11;i++)
{
mpz_init(a[i]);
mpz_init_set_str(a[i],s[i], 10);
mpz_mul(b, b, a[i]);
}gmp_printf("%d%d%d%d%d%d%d%d%d%d%d = \n%Zd\n", N-5, N-4, N-3, N-2, N-1, N, N+1, N+2, N+3, N+4, N+5, b);
for(i=0;i<11;i++){
mpz_clear(a[i]);
}mpz_clear(b);
return 0;}