Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
euclidean_algorithm_extended.c File Reference

Program to perform the extended Euclidean algorithm More...

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
Include dependency graph for euclidean_algorithm_extended.c:

Data Structures

struct  euclidean_result
 for tests More...
 

Functions

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
 

Detailed Description

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)

Typedef Documentation

◆ 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

Function Documentation

◆ 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
quotientthe quotient from the latest iteration of the Euclidean algorithm
prevthe 'queue' holding the values of the two previous iterations
Returns
void
55{
56 int next = prev[1] - (prev[0] * quotient);
57 xy_push(prev, next);
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
Here is the call graph for this function:

◆ extended_euclidean_algorithm()

euclidean_result_t extended_euclidean_algorithm ( int  a,
int  b 
)

performs the extended Euclidean algorithm on integer inputs a and b

Parameters
afirst integer input
bsecond 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;
75 euclidean_result_t result;
76
77 /* swap values of a and b */
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
96 calculate_next_xy(div_result.quot, previous_x_values);
97 calculate_next_xy(div_result.quot, previous_y_values);
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
Here is the call graph for this function:

◆ main()

int main ( void  )

Main Function.

Returns
0 upon successful program exit
151{
152 test(); // run self-test implementations
153 return 0;
154}
static void test()
Perform tests on known results.
Definition euclidean_algorithm_extended.c:135
Here is the call graph for this function:

◆ 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
afirst paramater for Euclidean algorithm
bsecond parameter for Euclidean algorithm
gcdexpected value of result.gcd
xexpected value of result.x
yexpected value of result.y
Returns
void
122{
123 euclidean_result_t result;
124
125 result = extended_euclidean_algorithm(a, b);
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
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Perform tests on known results.

Returns
void
136{
137 single_test(40, 27, 1, -2, 3);
138 single_test(71, 41, 1, -15, 26);
139 single_test(48, 18, 6, -1, 3);
140 single_test(99, 303, 3, -16, 49);
141 single_test(14005, 3507, 1, -305, 1218);
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
Here is the call graph for this function:

◆ 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
arran array of ints acting as a queue
newvalthe value being pushed into arr
Returns
void
37{
38 arr[1] = arr[0];
39 arr[0] = newval;
40}