This repository has been archived by the owner on Aug 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
60 lines (50 loc) · 1.51 KB
/
main.c
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
55
56
57
58
59
#include <stdlib.h>
#include <stdio.h>
#include <gmp.h>
#include <math.h>
char * luk (unsigned int number) //Тест Люка-Лемера
{
if (number==2) return "6"; //Тест Люка-Лемера не работает с двойкой.
mpz_t L,tmp,mersenne;
mpz_init_set_ui(L,4);
mpz_init (tmp);
//mersenne pow(2, q) - 1;
mpz_init_set_ui(mersenne,2);
mpz_pow_ui(mersenne,mersenne,number);//pow(2, q)
mpz_sub_ui(mersenne,mersenne,1);//pow(2, q) - 1
for (unsigned long long i = 1; i <= number-2; i++) //Проверка
{
mpz_mul (tmp,L,L);//L*L
mpz_sub_ui(tmp,tmp,2);//L*L-2
mpz_mod(L,tmp,mersenne);//(L*L-2) %mersenne
}
//char * mpz_get_str(char *str, intbase, mpztop)
//gmp_printf("Test: %Zd \n",L);
if (mpz_cmp_ui(L,0)!=0) return "0";
else
{
mpz_t resolte;// 1ull << (number-1))*mersenne
mpz_init_set_ui(resolte,2);
mpz_pow_ui(resolte,resolte,number-1);
mpz_mul(resolte,resolte,mersenne);
//gmp_printf("Число: %Zd \n",resolte);
//printf("str: %s",mpz_get_str(NULL,10,resolte));
return mpz_get_str(NULL,10,resolte) ; //Если тест прошёл, то вычисляем положительное совершеное число.
}
}
int prime(unsigned int n) //Проверка на простоту
{
for(unsigned int i=2;i<=sqrt(n);i++) if(n%i==0) return 0;
return 1;
}
int main(int argc , char **argv[])
{
char * response = "0";
unsigned int n=atoi(argv[1]);
if (prime(n) == 1) {
response = luk (n);
if (response != "0"){
printf("%s\n",response);
}
}
}