The Euclidean Algorithm is a simple method for finding the highest common factor or HCF (also known as greatest common divisor or GCD) of two positive integers. This is an implementation of the algorithm in C.Continue reading
There are many sorting algorithms, often with variations and optimizations, and every now and again I will be coding some of them for this site.
A while ago I wrote a post on bubble sort and here is a follow up on another sorting algorithm called selection sort. As with bubble sort it’s not particularly efficient but it is simple to understand and implement.
All the posts on this blog contain C source code which has been reformatted as HTML. This would be incredibly tedious to do by hand so I put together a quick and dirty utility to do it for me. It's not the greatest piece of software ever written but it does the job, so I thought I might as well share it.Continue reading
The C language has a reputation for being difficult to learn and to code in. I think this is unfair as it is actually a very small and simple language, but most of the perceived difficulty with using C comes from its memory management, or rather lack of. Only the smallest and simplest programs can get away with using auto variables: sooner or later you are going to have to use dynamic memory, opening yourself up to an extensive range of tricky bugs. There's no foolproof way to get round this, but you can catch most bugs before they wreak havoc in production with a brilliant little program called Valgrind.Continue reading
When creating a dynamic data structure it is tempting to allocate just the amount of extra memory you need to add each item. However, malloc, realloc or calloc are quite expensive in terms of resources so for situations where many items are likely to be added it is more efficient to allocate a large block of memory in one go. This can be gradually used up, and then another block allocated when necessary. In this post I will write a simple demonstration of this principle, complete with monitoring code to verify that we are in fact improving efficiency.Continue reading
Previous posts have included an SVG library, memoization of factorials and Taylor Polynomials. In this post I will bring these all together to plot various sine waves created using Taylor Polynomials.
Very briefly, Taylor Polynomials are used to approximate functions, in this case sine and cosine, to any level of accuracy. We can plot these levels to show how they become increasingly accurate, which is the subject of this post.
In this post I will write code in C to convert colour values between decimal red, green and blue, hexadecimal RGB, and hue, saturation and lightness or HSL.
This is a relatively short and simple project which will calculate a few simple statistics from an array of numbers. It covers the most basic areas of classical statistics which might seem a bit old-fashioned in an era of big data and machine learning algorithms, but even the most complex of data science investigations are likely to start out with a few simple statistics.Continue reading
I recently posted articles on memoization of factorials and calculating sines and cosines with Taylor polynomials. It seems logical to combine the two principles and write a library to memoize the trigonometric values sine, cosine and tangent.
If you just need a few values then this approach is overkill, but some applications require very heavy use of trigonometry and so need all the optimization they can get. In particular 3D graphics consist of large numbers of triangles, the positioning and rendering of which requires some serious use of trigonometry.
In this post I will write a C library to calculate sines and cosines using Taylor polynomials. It is impossible to calculate these directly but they can be approximated to any accuracy using this method, and I will show how a Taylor series converges to ever-increasing accuracy.