A Journey Through Algorithms
2023-06-27
I picked up a small reference-style book this last weekend that looks to be very interesting to my field of study, “Algorithms” by Panos Louridas. This slightly larger-than-pocket-sized text is part of the MIT Press ‘Essential Knowledge Series’, so its goal is to be highly approachable and general while getting into the weeds the further you go. I believe the book goes on to explain several popular algorithms, some of which are even the basis for neural networks. That being said, I wanted to catalog all of them and show an example in whatever language seems appropriate.
The first problem of the book becomes creating patterns with the greatest common divisor. The Euclidean algorithm, although simple gives us a context for what comes next. I have really enjoyed videos from The Coding Train on Youtube, and he achieves some really amazing results with p5.js and p3 for Java. So I might dip my toes in that later in this series, but for now, this example is just a function in C++ since I did a little of that last semester and I had a bit of an idea to do programming language drag racing with these simple algorithms. Greatly inspired by the prime sieve from Dave Plumber of Dave’s Garage. The sample function has the most simple implementation, making a drag race might include a for loop to find the GCD for a range of inputs 1-500 for example by 1-500.
#include <iostream>
int euclidsAl(int a, int b){
if(b > a){
int tempA = b;
b = a;
a = tempA;
}
while(a % b != 0){
int tempA = b;
b = a % b;
a = tempA;
}
return b;
}
int main(int argc, const char * argv[]) {
// This is the first of a collection of projects that just show algorithms working in limited implimentation
int first;
int second;
int output;
std::cout << "Hello, welcome The Euclidean Algorithm\n Please enter first number\n";
std::cin >> first;
std::cout << "Now the second.\n ";
std::cin >> second;
output = euclidsAl(first, second);
std::cout << "result: "+std::to_string(output);
return 0;
}
Comments