extended euklidian algorithm
This commit is contained in:
48
main.c
48
main.c
@@ -1,4 +1,5 @@
|
|||||||
#include <inttypes.h>
|
#include <inttypes.h>
|
||||||
|
#include <math.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
@@ -28,7 +29,7 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
|
|||||||
powed[0] = a;
|
powed[0] = a;
|
||||||
for (int i = 1; i <= length; i++) {
|
for (int i = 1; i <= length; i++) {
|
||||||
powed[i] = (uint64_t)(((unsigned __int128)powed[i - 1] * powed[i - 1]) % n);
|
powed[i] = (uint64_t)(((unsigned __int128)powed[i - 1] * powed[i - 1]) % n);
|
||||||
printf("powed: %ju, index: %d; ", powed[i], (i));
|
// printf("powed: %ju, index: %d; ", powed[i], (i));
|
||||||
}
|
}
|
||||||
|
|
||||||
// check where in the binary are ones
|
// check where in the binary are ones
|
||||||
@@ -39,7 +40,7 @@ uint64_t quick_pow(uint64_t *d_binary, uint64_t a, uint64_t n, uint64_t length)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
printf("\nbm quick math: %ju; %ju ", multiplied, n);
|
// printf("\nbm quick math: %ju; %ju ", multiplied, n);
|
||||||
|
|
||||||
free(powed);
|
free(powed);
|
||||||
|
|
||||||
@@ -115,6 +116,46 @@ void *prime_thread_worker(void *arg) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint64_t lnko;
|
||||||
|
__int128 x;
|
||||||
|
__int128 y;
|
||||||
|
} euklidian_result_t;
|
||||||
|
|
||||||
|
euklidian_result_t euklidian_algorigthm_extended(unsigned __int128 a, unsigned __int128 b) {
|
||||||
|
__int128 r = a % b, q = a / b, k = 1, xk = 0, yk = 1, next_r;
|
||||||
|
__int128 prev_r = b, prev_q, prev_xk = 0, prev_yk = 1, prev_prev_xk = 1, prev_prev_yk = 0;
|
||||||
|
euklidian_result_t res = {0, 0, 0};
|
||||||
|
|
||||||
|
while (r != 0) {
|
||||||
|
k++;
|
||||||
|
|
||||||
|
prev_q = q;
|
||||||
|
q = prev_r / r;
|
||||||
|
|
||||||
|
next_r = prev_r % r;
|
||||||
|
prev_r = r;
|
||||||
|
r = next_r;
|
||||||
|
|
||||||
|
xk = xk * prev_q + prev_prev_xk;
|
||||||
|
prev_prev_xk = prev_xk;
|
||||||
|
prev_xk = xk;
|
||||||
|
|
||||||
|
yk = yk * prev_q + prev_prev_yk;
|
||||||
|
prev_prev_yk = prev_yk;
|
||||||
|
prev_yk = yk;
|
||||||
|
}
|
||||||
|
|
||||||
|
__int128 x = k % 2 == 0 ? prev_xk : -prev_xk;
|
||||||
|
__int128 y = k % 2 == 0 ? -prev_yk : prev_yk;
|
||||||
|
|
||||||
|
res.lnko = prev_r;
|
||||||
|
res.x = x;
|
||||||
|
res.y = y;
|
||||||
|
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
@@ -145,5 +186,8 @@ int main() {
|
|||||||
e = rand64();
|
e = rand64();
|
||||||
} while (e <= 1 && e >= fi_n && prime_test(e, base));
|
} while (e <= 1 && e >= fi_n && prime_test(e, base));
|
||||||
|
|
||||||
|
euklidian_result_t test = euklidian_algorigthm_extended(192, 11);
|
||||||
|
printf("test lnko: %ju\n", test.lnko);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user