-
Notifications
You must be signed in to change notification settings - Fork 0
/
primetest.c
40 lines (37 loc) · 1.09 KB
/
primetest.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
#include<stdio.h>
#include<stdbool.h>
#include<stdlib.h>
typedef unsigned int Uint;
void mod(Uint a, Uint p, Uint n, Uint *result, _Bool *composite);
_Bool isPrime(Uint n, Uint k);
int main(){
Uint n;
while (scanf("%d", &n) != EOF && n != 0){
if (isPrime(n, 100)) printf("YES!\n");
else printf("NO!\n");
}
return 0;
}
_Bool isPrime(Uint n, Uint k){
if (n == 3 || n == 2) return true;
Uint a;
Uint *result = (Uint *)malloc(sizeof(Uint));
_Bool *composite = (_Bool *)malloc(sizeof(_Bool));
*composite = false;
for (int i = 0; i < k; i++){
a = rand() % (n - 3) + 2;
mod(a, n - 1, n, result, composite);
if ((*composite) || (*result != 1)) return false;
}
return true;
}
void mod(Uint a, Uint p, Uint n, Uint *result, _Bool *composite){
Uint *x = (Uint *)malloc(sizeof(Uint));
if (p == 0) *result = 1;
else{
mod(a, p / 2, n, x, composite);
*result = ((*x) * (*x)) % n;
if ((*result == 1) && (*x != 1) && (*x != n - 1)) *composite = true;
if (p % 2 == 1) *result = (*result * a) % n;
}
}