Here I plan to put some projects which may not be useful, per se, but implement some mathematical ideas. For now, I have two:
This is an implementation of the Euclidean algorithm for finding the greatest common denominator of two numbers, written in C. The code prints each step of the algorithm, but if desired, the function gcd
is available to just efficiently calculate the greatest common denominator. A second function will "unzip" the computation, expressing the greatest common denominator as a linear combination of the two inputs. The curious should check out more information on this algorithm from Wikipedia or Wolfram MathWorld.
The code can be compiled by an ANSI-compatible C compiler (C89 standard). On unix this might mean
> cc -o euclid euclid.c stack.c
Subsequently it can be run by passing in two positive integer arguments:
> ./euclid 97135 45293
97135 = 2 * 45293 + 6549
45293 = 6 * 6549 + 5999
6549 = 1 * 5999 + 550
5999 = 10 * 550 + 499
550 = 1 * 499 + 51
499 = 9 * 51 + 40
51 = 1 * 40 + 11
40 = 3 * 11 + 7
11 = 1 * 7 + 4
7 = 1 * 4 + 3
4 = 1 * 3 + 1
3 = 3 * 1 + 0
gcd(97135,45293) = 1
Expressing 1 as a linear combination of 97135 and 45293:
1 = 1 * 4 - 1 * 3
1 = 2 * 4 - 1 * 7
1 = 2 * 11 - 3 * 7
1 = 11 * 11 - 3 * 40
1 = 11 * 51 - 14 * 40
1 = 137 * 51 - 14 * 499
1 = 137 * 550 - 151 * 499
1 = 1647 * 550 - 151 * 5999
1 = 1647 * 6549 - 1798 * 5999
1 = 12435 * 6549 - 1798 * 45293
1 = 12435 * 97135 - 26668 * 45293
There's also a very simple array-based stack implementation there which may be of interest. It employs a very simplistic form of genericism, being parameterized by redefining the _TYPE_
macro.
This is a simple MT19937 random number generator written in C (1989 standard). The code is straightforward conversion of the Mersenne Twister pseudocode published on the Wikipedia page.
The generator is initialized by the ms_init
function. After this, 32-bit pseudorandom numbers can be generated by calling ms_rand()
. If the user doesn't call ms_init
prior to the first call to ms_rand()
, the generator is seeded to the system time (unless lines 6 and 7 are commented out, in which case, you'd better seed the generator with something or you'll get all zeroes). The main
function included in this code is a very simple demo that counts instances of generated pseudorandom numbers between 0 and 19 and prints the counts.