This code below works for me on Windows MSYS2 but fails on my sons Unix with Segfault around rrandomb.c Directory not empty. Can you see why? #include 
#include 
#include 
#include 
#include "numtheory.h"
#include "randstate.h"
#include 
#include 
void pow\_mod(mpz\_t o, const mpz\_t a, const mpz\_t d, const mpz\_t n) {
 mpz\_t result, base, exp;
 mpz\_inits(result, base, exp, NULL);

 mpz\_mod(base, a, n);
 mpz\_set(exp, d);
 mpz\_set\_ui(o, 1);

 while (mpz\_cmp\_ui(exp, 0) > 0) {
 if (mpz\_odd\_p(exp)) {
 mpz\_mul(o, o, base);
 mpz\_mod(o, o, n);
 }

 mpz\_divexact\_ui(exp, exp, 2);
 mpz\_mul(base, base, base);
 mpz\_mod(base, base, n);
 }

 mpz\_clears(result, base, exp, NULL);
}

#include 
#include 
#include 

bool is\_prime(const mpz\_t n, uint64\_t iters) {
 // Check base cases
 if (mpz\_cmp\_ui(n, 2) < 0) {
 return false;
 } else if (mpz\_cmp\_ui(n, 2) == 0) {
 return true;
 } else if (mpz\_even\_p(n)) {
 return false;
 }

 // Write n-1 as 2^s \* r
 mpz\_t r, d;
 mpz\_init(r);
 mpz\_init(d);
 mpz\_sub\_ui(d, n, 1);
 mpz\_set(r, d);
 size\_t s = 0;
 while (mpz\_even\_p(r)) {
 mpz\_divexact\_ui(r, r, 2);
 s++;
 }

 // Perform iterations
 for (uint64\_t i = 0; i < iters; i++) {
 // Choose random base a
 
 mpz\_t a;
 mpz\_init(a);
 mpz\_set\_ui(a, 2); // TODO change 2 to random number
 if (mpz\_cmp\_ui(a, 1) <= 0 || mpz\_cmp(a, d) >= 0) {
 mpz\_clear(a);
 continue;
 }

 // Compute y = a^r mod n
 mpz\_t y;
 mpz\_init(y);
 mpz\_powm(y, a, r, n);

 // Test primality conditions
 bool is\_probably\_prime = false;
 if (mpz\_cmp\_ui(y, 1) == 0) {
 is\_probably\_prime = true;
 }
 for (size\_t j = 0; j < s; j++) {
 if (mpz\_cmp(y, d) == 0) {
 is\_probably\_prime = true;
 break;
 }
 mpz\_powm\_ui(y, y, 2, n);
 }
 if (!is\_probably\_prime) {
 mpz\_clear(a);
 mpz\_clear(y);
 return false;
 }

 // Clean up
 mpz\_clear(a);
 mpz\_clear(y);
 }

 // Clean up
 mpz\_clear(r);
 mpz\_clear(d);

 return true;
}

void make\_prime(mpz\_t p, uint64\_t bits, uint64\_t iters) {
 gmp\_randstate\_t state;

 // Initialize the random state variable
 gmp\_randinit\_mt(state);
 gmp\_randseed\_ui(state, time(NULL));

 while (true) {
 // Generate a random number of the desired size
 mpz\_rrandomb(p, state, bits);

 // Ensure that the number has the correct number of bits
 mpz\_setbit(p, bits - 1); // set the leftmost bit to 1

 // Test for primality
 if (is\_prime(p, iters)) {
 printf("Generated prime %s\n", mpz\_get\_str(NULL, 10, p)); 
 break;
 }
 }
}

int main(void) {
 mpz\_t n;
 mpz\_init(n);

 // Test with a composite number
 mpz\_set\_ui(n, 1001);
 if (is\_prime(n, 20)) {
 printf("%s is prime\n", mpz\_get\_str(NULL, 10, n));
 } else {
 printf("%s is composite\n", mpz\_get\_str(NULL, 10, n));
 }

 mpz\_t p;
 int num\_bits = 40;
 int num\_primes = 10;

 int iters =20;
 for (int i = 0; i < num\_primes; i ++) {
 make\_prime(p, num\_bits, iters);
 sleep (1);
 }
 
 mpz\_clear(n);
 return 0;
}
아래 코드는 Windows MSYS2에서는 작동하지만 내 아들 유닉스에서는 rrandomb.c 디렉토리가 비어 있지 않은 세그 오류로 실패합니다. 이유를 알 수 있을까요? #포함 #포함 #포함 #include #include "numtheory.h"#include "randstate.h"#포함 #포함 void pow\_mod(mpz\_t o, const mpz\_t a, const mpz\_t d, const mpz\_t n) { MPZ\_t RESULT, BASE, EXP; mpz\_inits(result, base, exp, NULL); MPZ\_MOD(BASE, A, N); MPZ\_SET(EXP, D); MPZ\_SET\_UI(O, 1); while (mpz\_cmp\_ui(exp, 0) > 0) { IF (MPZ\_ODD\_P(EXP)) { MPZ\_MUL(O, O, BASE); mpz\_mod(o, o, n); } MPZ\_DIVEXACT\_UI(EXP, EXP, 2); MPZ\_MUL(베이스, 베이스, 베이스); mpz\_mod(base, base, n); } mpz\_clears(result, base, exp, NULL);}#포함 #포함 #include bool is\_prime(const mpz\_t n, uint64\_t iters) { // 기본 사례 확인 if (mpz\_cmp\_ui(n, 2) < 0) { return false; } else if (mpz\_cmp\_ui(n, 2) == 0) { return true; } else if (mpz\_even\_p(n)) { return false; } // n-1을 2^s \* r로 쓰기 mpz\_t r, d; MPZ\_INIT(R); MPZ\_INIT(D); MPZ\_SUB\_UI(D, N, 1); MPZ\_SET(R, D); size\_t s = 0; while (mpz\_even\_p(r)) { MPZ\_DIVEXACT\_UI(R, R, 2); s++; } // 반복 수행 for (uint64\_t i = 0; i < iters; i++) { // 랜덤 베이스 선택  mpz\_t a; MPZ\_INIT(A); mpz\_set\_ui(a, 2); // 2를 난수로 변경하기 if (mpz\_cmp\_ui(a, 1) <= 0 || mpz\_cmp(a, d) >= 0) { MPZ\_CLEAR(A); 계속 } // 계산 y = a^r mod n mpz\_t y; mpz\_init(y); mpz\_powm(y, a, r, n); // 일차성 조건 테스트 부울은\_아마도\_프라임 = 거짓입니다; if (mpz\_cmp\_ui(y, 1) == 0) { is\_probably\_prime = true; } for (size\_t j = 0; j < s; j++) { if (mpz\_cmp(y, d) == 0) { is\_probably\_prime = true; break; } MPZ\_POWM\_UI(Y, Y, 2, N); } if (!is\_probably\_prime) { MPZ\_CLEAR(A); MPZ\_CLEAR(Y); return false; } // 정리 mpz\_clear(a); mpz\_clear(y); } // 정리 MPZ\_CLEAR(R); MPZ\_CLEAR(D); 참을 반환합니다;}void make\_prime(mpz\_t p, uint64\_t bits, uint64\_t iters) { GMP\_RANDSTATE\_STATE; // 랜덤 상태 변수를 초기화합니다 gmp\_randinit\_mt(state); gmp\_randseed\_ui(state, time(NULL)); while (true) { // 원하는 크기의 난수를 생성합니다. mpz\_randomb(p, state, bits); // 숫자의 비트 수가 올바른지 확인합니다. mpz\_setbit(p, bits - 1); // 가장 왼쪽 비트를 1로 설정합니다. // 1등성 테스트 if (is\_prime(p, iters)) { printf("생성된 소인수 %s\n", mpz\_get\_str(NULL, 10, p));  break; } }}int main(void) { mpz\_t n; MPZ\_INIT(N); // 복합 숫자로 테스트 MPZ\_SET\_UI(N, 1001); if (is\_prime(n, 20)) { printf("%s는 소수\n입니다", mpz\_get\_str(NULL, 10, n)); } else { printf("%s는 복합\n", mpz\_get\_str(NULL, 10, n)); } mpz\_t p; int num\_bits = 40; int num\_프라임 = 10; int iters = 20; for (int i = 0; i < num\_primes; i ++) { make\_prime(p, num\_bits, iters); sleep (1); }  MPZ\_CLEAR(N); 반환 0;}