pierre.c (573B)
1 #include "pierre.h" 2 3 void 4 fermat_factor(ctx_t *ctx, fmpz_t r, fmpz_t n, size_t limit) 5 { 6 size_t i; 7 uint64_t inc = 1; 8 int found = 0; 9 fmpz_set(r, n); 10 11 assert(ctx->zs_alloc >= 1); 12 13 for (i = 0; i < limit; i++) { 14 if (fmpz_is_square(r)) { 15 found = 1; 16 break; 17 } 18 19 fmpz_add_ui(r, r, inc); 20 inc += 2; 21 } 22 23 if (found) { 24 fmpz_t tmp = {ctx->zs[0]}; 25 fmpz_set(tmp, n); 26 fmpz_sub(tmp, r, n); // tmp = b^2 27 fmpz_sqrt(tmp, tmp); // tmp = b 28 fmpz_sqrt(r, r); // r = a 29 fmpz_add(r, r, tmp); 30 } else { 31 fmpz_set_ui(r, 0); 32 } 33 }