Estimating Pi in JavaScript

Pi is an irrational number starting off 3.14159 and then carrying on for an infinite number of digits with no pattern which anybody has ever discovered. Therefore it cannot be calculated as such, just estimated to (in principle) any number of digits, and in this post I will implement a few of the many methods for doing so in JavaScript.

Continue reading

Finding Prime Numbers with the Sieve of Eratosthenes in JavaScript

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 JavaScript.

Continue reading

The Soundex Algorithm in JavaScript

Soundex is a phonetic algorithm, assigning values to words or names so that they can be compared for similarity of pronounciation. For this post I will write an implementation in JavaScript.

It doesn't take much thought to realise that the whole area of phonetic algorithms is a minefield, and Soundex itself is rather restricted in its usefulness. In fact, after writing this implementation I came to the conclusion that it is rather mediocre but at least coding it up does give a better understanding of how it works and therefore its usefulness and limitations.

Wikipedia has a surprisingly brief article on the topic Soundex on Wikipedia which you might like to read.

Continue reading

Great Circle Distances in JavaScript

The shortest distance between two locations on the surface of Earth (or any planet) is known as the Great Circle Distance. Except for relatively short distances these cannot be measured on a map due to the distortion and flattening necessary to represent a sphere on a flat plane. However, the calculation of the Great Circle Distance from two coordinates is simple, although I suspect generations of midshipmen might not have agreed. In this post I will write a short JavaScript application to calculate the distance from London to various other cities round the world.

Continue reading

Should You Learn Data Structures and Algorithms?

You could reasonably argue that data structures and algorithms are at the heart of computer science. This leads to the widespread belief that they are also at the heart of software development and that a detailed or even profound knowledge of them is necessary to be a decent programmer. I believe that while it doesn't do any harm to learn them in depth it is not the best use of your time.

Continue reading

HSL GUI in Python

I recently wrote a post on RGB/HSL Conversions in Python and as a follow-up I decided to put together a simple Tkinter application which enables users to display a colour by selecting any combination of hue, saturation and lightness.

This application is only a toy but playing with it for a few minutes does give an intuitive understanding of the HSL system, and reinforces my conviction that it is easier to comprehend than RGB. And if you have never used Tkinter this project also provides a simple introduction to the topic.

Continue reading

RGB/HSL Conversions in Python

The most common ways of encoding colour values are RGB (red, green and blue) and HSL (hue, saturation and lightness). I will show how you can convert between the two in Python.

A while ago I wrote a post called Colour Converter in C about converting RGB (red/green/blue) values to HSL (hue/saturation/lightness) and back again. As part of a project which I am planning I need to be able to do the same in Python and was intending to re-write the C in Python, or probably Cython. However, I then found a module called colorsys in an obscure corner of the Python Standard Library. This module includes functions to do the conversions I will need, and in this post I will demonstrate using them to create an array of HSL values from a Pillow image, and then convert these back to RGB to create a new identical image.

Continue reading

Graph Theory in Python

Graph Theory is a vast area of study based around the simple idea of individual points - known as vertices - connected by lines known as edges, each of which may have an associated numeric values called a weight and perhaps also a direction.

These simple sets of vertices, edges, weights and directions are known as graphs (not to be confused with the more familiar graph of data or a mathematical function) and can be used to represent many real-world situations or abstract concepts. Once you have a graph stored in some kind of data structure there are many algorithms which can be applied to solve problems, especially those of optimisation.

This post is the first of potentially many, and in it I will develop a data structure to store a graph. In future posts I will implement some of the algorithms which operate on graphs.

Continue reading