Sines and Cosines with Taylor Polynomials

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.

Continue reading

Posted in Uncategorized | Leave a comment

Memoization of Factorials

I am currently working on an article about calculating sines and cosines using Taylor Polynomials. These make heavy use of factorials so I started thinking about ways to streamline the process.

This post consists of a simple project using memoization with a lookup table to pre-calculate factorials and store them for future use.

Continue reading

Posted in Uncategorized | Leave a comment

Using the C Library’s qsort Function

I recently wrote an article on Bubble Sort, more as an academic exercise than a piece of practical and usable code. At the bottom of the post I suggested that the C library’s built-in qsort (Quicksort) function was the best option for sorting arrays in most situations.

However, there is more to using qsort than just throwing an array at a function: you need to provide your own comparator function, the implementation of which can be slightly fiddly if you are sorting anything other than primitive data types. In this article I’ll start off with a simple int-sorting example, and then go on to sort an array of structs, firstly by an int and then by a string.

Continue reading

Posted in Uncategorized | Leave a comment

Linear Regression: Fitting a Line to Data

In a previous post I implemented the Pearson Correlation Coefficient, a measure of how much one variable depends on another. The three sets of bivariate data I used for testing and demonstration are shown again below, along with their corresponding scatterplots. As you can see these scatterplots now have lines of best fit added, their gradients and heights being calculated using least-squares regression which is the subject of this article.

Continue reading

Posted in Uncategorized | Leave a comment

Finding Prime Numbers – Sieve of Eratosthenes

Prime numbers have been understood at least since the Ancient Greeks, and possibly since the Ancient Egyptians. In modern times their study has intensified greatly due to their usefulness, notably in encryption, and because computers enable them to be calculated to a massively higher level than could be done by hand.

The best know (and according to Wikipedia still the most widely used) method for identifying them is the Sieve of Eratosthenes, which I will implement here in C.

Continue reading
Posted in Uncategorized | 1 Comment

Pearson Correlation Coefficient

Correlation is the process of quantifying the relationship between two sets of values, and in this post I will be writing code in C to calculate possibly the best-known type of correlation - the Pearson Correlation Coefficient.

Continue reading
Posted in Uncategorized | Leave a comment

The Notorious Bubble Sort

I am sticking my neck out here by implementing the notorious Bubble Sort in C. There are many sorting algorithms and even more variations on a theme, but the bubble sort is probably the best known. It’s simple to understand and the process can be illustrated with some cute animations and even Hungarian folk dancing!

Continue reading

Posted in Uncategorized | 5 Comments

Calculating Any Term of the Fibonacci Sequence Using Binet’s Formula

You can calculate the Fibonacci Sequence by starting with 0 and 1 and adding the previous two numbers, but Binet’s Formula can be used to calculate directly any term of the sequence. This short project is an implementation of the formula in C.

Continue reading

Posted in Uncategorized | Leave a comment

Logarithms: a Practical Use

This is a simple little C project demonstrating a useful application of logarithms, with the bonus of a few lines demonstrating localization.

Continue reading

Posted in Uncategorized | 2 Comments

Zeller’s Congruence – Calculating the Day of the Week

Today I’ll write an implementation in C of Zeller’s Congruence, a simple and elegant little formula to carry out the seemingly complex task of calculating the day of the week (Monday, Tuesday etc.) from a date.

Continue reading

Posted in Uncategorized | Leave a comment