Program to perform the extended Euclidean algorithm
More...
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
|
static void | single_test (int a, int b, int gcd, int x, int y) |
| perform one single check on the result of the algorithm with provided parameters and expected output
|
|
static void | test () |
| Perform tests on known results.
|
|
int | main () |
| Main Function.
|
|
typedef struct euclidean_result | euclidean_result_t |
| for tests
|
|
static void | xy_push (int arr[2], int newval) |
| gives queue-like behavior to an array of two ints, pushing an element onto the end and pushing one off the front
|
|
static void | calculate_next_xy (int quotient, int prev[2]) |
| calculates the value of x or y and push those into the small 'queues'
|
|
euclidean_result_t | extended_euclidean_algorithm (int a, int b) |
| performs the extended Euclidean algorithm on integer inputs a and b
|
|
Program to perform the extended Euclidean algorithm
The extended Euclidean algorithm, on top of finding the GCD (greatest common divisor) of two integers a and b, also finds the values x and y such that ax+by = gcd(a, b)
◆ euclidean_result_t
for tests
for IO for div function and corresponding div_t struct
a structure holding the values resulting from the extended Euclidean algorithm
◆ calculate_next_xy()
static void calculate_next_xy |
( |
int |
quotient, |
|
|
int |
prev[2] |
|
) |
| |
|
inlinestatic |
calculates the value of x or y and push those into the small 'queues'
Both x and y are found by taking their value from 2 iterations ago minus the product of their value from 1 iteration ago and the most recent quotient.
- Parameters
-
quotient | the quotient from the latest iteration of the Euclidean algorithm |
prev | the 'queue' holding the values of the two previous iterations |
- Returns
- void
55{
56 int next = prev[1] - (prev[0] * quotient);
58}
static void xy_push(int arr[2], int newval)
gives queue-like behavior to an array of two ints, pushing an element onto the end and pushing one of...
Definition euclidean_algorithm_extended.c:36
int next(Vector *vec)
This function gets the next item from the Vector each time it's called.
Definition vector.c:102
◆ extended_euclidean_algorithm()
performs the extended Euclidean algorithm on integer inputs a and b
- Parameters
-
a | first integer input |
b | second integer input |
- Returns
- euclidean_result_t containing the gcd, and values x and y such that ax + by = gcd
70{
71 int previous_remainder = 1;
72 int previous_x_values[2] = {0, 1};
73 int previous_y_values[2] = {1, 0};
74 div_t div_result;
76
77
78 if (abs(a) < abs(b))
79 {
80 a ^= b;
81 b ^= a;
82 a ^= b;
83 }
84
85 div_result.rem = b;
86
87 while (div_result.rem > 0)
88 {
89 div_result = div(a, b);
90
91 previous_remainder = b;
92
93 a = b;
94 b = div_result.rem;
95
98 }
99
100 result.
gcd = previous_remainder;
101 result.x = previous_x_values[1];
102 result.
y = previous_y_values[1];
103
104 return result;
105}
static void calculate_next_xy(int quotient, int prev[2])
calculates the value of x or y and push those into the small 'queues'
Definition euclidean_algorithm_extended.c:54
for tests
Definition euclidean_algorithm_extended.c:21
int y
the values x and y such that ax + by = gcd(a, b)
Definition euclidean_algorithm_extended.c:24
int gcd
the greatest common divisor calculated with the Euclidean algorithm
Definition euclidean_algorithm_extended.c:22
◆ main()
Main Function.
- Returns
- 0 upon successful program exit
151{
153 return 0;
154}
static void test()
Perform tests on known results.
Definition euclidean_algorithm_extended.c:135
◆ single_test()
static void single_test |
( |
int |
a, |
|
|
int |
b, |
|
|
int |
gcd, |
|
|
int |
x, |
|
|
int |
y |
|
) |
| |
|
inlinestatic |
perform one single check on the result of the algorithm with provided parameters and expected output
- Parameters
-
a | first paramater for Euclidean algorithm |
b | second parameter for Euclidean algorithm |
gcd | expected value of result.gcd |
x | expected value of result.x |
y | expected value of result.y |
- Returns
- void
122{
124
126 assert(result.
gcd == gcd);
127 assert(result.x == x);
128 assert(result.
y == y);
129}
euclidean_result_t extended_euclidean_algorithm(int a, int b)
performs the extended Euclidean algorithm on integer inputs a and b
Definition euclidean_algorithm_extended.c:69
◆ test()
static void test |
( |
void |
| ) |
|
|
static |
Perform tests on known results.
- Returns
- void
136{
142
143 printf("All tests have successfully passed!\n");
144}
static void single_test(int a, int b, int gcd, int x, int y)
perform one single check on the result of the algorithm with provided parameters and expected output
Definition euclidean_algorithm_extended.c:121
◆ xy_push()
static void xy_push |
( |
int |
arr[2], |
|
|
int |
newval |
|
) |
| |
|
inlinestatic |
gives queue-like behavior to an array of two ints, pushing an element onto the end and pushing one off the front
- Parameters
-
arr | an array of ints acting as a queue |
newval | the value being pushed into arr |
- Returns
- void
37{
38 arr[1] = arr[0];
39 arr[0] = newval;
40}