
The site hasn’t been updated in ages and is statically generated using Jekyll. Jekyll is a Ruby Gem, so runs on Ruby. My laptop is Windows 10, whose biggest failing IMHO is the lack of a centralised software management.
Much simpler when all that’s required is
sudo aptget update
orpacman Syu
. Why? it leads to problems with dependencies and out of date versions installed. 
I like Pimoroni’s products, they’re generally well made with reasonable documentation. The first run of their new Grow Hat was discounted because of a slight flaw  the screens don’t dim.

Coronavirus has seen many of us stuck indoors for potentially months so I’ve been taking advantage of the time trying out a course in Machine Learning.
I just finished reading Hannah Fry’s Hello World and realised that although machine learning affects a vast amount of my life, I don’t know a great deal about how it is actually implemented. Combined with my employer’s desire for me to take more courses, I enrolled in a couple with Udemy.

I’ve been spending time with Python recently and am beginning to really like some of the language’s features. List comprehension (listcomp) creates a list by evaluating an expression on each item in a given list, from left to right.

Pimoroni’s Rainbow Hat offers a nice selection of inputs and outputs using a variety of protocols. This makes it a great way to experiment with Raspberry Pi, especially as Hardware Attached on Top (HAT) avoids the messiness of breadboards and shorting links!

Having not developed anything in Java since finishing university, opening Android Studio seemed like looking at an alien language. While I’d love to say it all came flooding back, that wouldn’t be true. Downloading the source to my last app from GitHub really didn’t help either.

In an earlier post, I used Threads but the thread module in Python when I should be using threading. The documentation for threading says it builds upon the thread module (renamed _thread):
This module provides lowlevel primitives for working with multiple threads (also called lightweight processes or tasks) — multiple threads of control sharing their global data space. For synchronization, simple locks (also called mutexes or binary semaphores) are provided. The threading module provides an easier to use and higherlevel threading API built on top of this module.

Playing with Rainbow Hat I learned a few things about Python as a result I found out what a decorator is, the difference between args and kwargs and threads. I also learned that a lot of guides don’t understand either.
If you can’t explain it simply, you don’t understand it well enough.^{1}

Albert Einstein ↩


Passwordless SSH access is convenient, especially as everything is on my local network. I only really access the Pi remotely and you can configure it to use RSA keys. I’m on Ubuntu Linux so open a terminal and create an RSA key (if you don’t have one):
sshkeygen t rsa

Project Euler again, problem 19. How many Sundays fell on the first of the month in the twentieth century?
Brute force solution in R but it let me play around with the way R handles dates. Seems more straight forward than in many languages (looking at you Java).
a.date < as.Date("1901/01/01") end.date < as.Date("2000/12/31") count < 0 while (a.date <= end.date) { if (format(a.date, "%d") == "01") { if (format(a.date, "%A") == "Sunday") { count = count + 1 } } a.date = a.date + 1 } print(count)
Used a for loop initially but found that it wouldn’t iterate Date types. Could probably look into that in the future.

Project Euler problem 25 is to find the index of the first Fibonacci number of 1000 characters. I used the gmp library – I couldn’t find a way to get the length of a bigz data type so converted it to a string:
library(gmp) digits < 0 i < 0 while (digits < 1000) { i = i + 1 number.string = as.character(fibnum(i)) digits = nchar(number.string) } print(i)
The gmp fibnum function is quick.

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total. If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
thousands = { '0': '', '1': 'one thousand ', '2': 'two thousand ', '3': 'three thousand ', '4': 'four thousand ', '5': 'five thousand ', '6': 'six thousand ', '7': 'seven thousand ', '8': 'eight thousand ', '9': 'nine thousand ', } hundreds = { '0': '', '1': 'one hundred ', '2': 'two hundred ', '3': 'three hundred ', '4': 'four hundred ', '5': 'five hundred ', '6': 'six hundred ', '7': 'seven hundred ', '8': 'eight hundred ', '9': 'nine hundred ', } units = { '0': '', '1': 'one', '2': 'two', '3': 'three', '4': 'four', '5': 'five', '6': 'six', '7': 'seven', '8': 'eight', '9': 'nine', } tens = { '10': 'ten', '11': 'eleven', '12': 'twelve', '13': 'thirteen', '14': 'fourteen', '15': 'fifteen', '16': 'sixteen', '17': 'seventeen', '18': 'eighteen', '19': 'nineteen', } twenties = { '0': '', '2': 'twenty', '3': 'thirty', '4': 'forty', '5': 'fifty', '6': 'sixty', '7': 'seventy', '8': 'eighty', '9': 'ninety' } total = 0 for i in range(1, 1001): sentance = '' numberString = str(i) if len(numberString) == 4: sentance = thousands[numberString[0]] + hundreds[numberString[1]] if not numberString[1:4] == '000': sentance = sentance + 'and ' if numberString[2] == '1': sentance = sentance + tens[numberString[2]+numberString[3]] else: sentance = sentance + twenties[numberString[2]] + units[numberString[3]] if len(numberString) == 3: sentance = hundreds[numberString[0]] if not numberString[1:3] == '00': sentance = sentance + 'and ' if numberString[1] == '1': sentance = sentance + tens[numberString[1]+numberString[2]] else: sentance = sentance + twenties[numberString[1]] + units[numberString[2]] if len(numberString) == 2: if numberString[0] == '1': sentance = tens[numberString] else: sentance = twenties[numberString[0]] + units[numberString[1]] if len(numberString) == 1: sentance = units[numberString[0]] total = total + len(sentance.replace(' ','')) print(sentance + str(total) + '\n')

Project Euler again, this time Python. The problem is to sort a list of 5000 names alphabetically then give them a value. For example “COLIN” is 3 + 15 + 12 + 9 + 14 = 53 and is the 938th item – so its value is 49714 (53*938).
The biggest hurdle here is reading the file. It’s in one big blob of values wrapped in quotes and seperated by a comma.
def readFile(): with open("data.txt", "r") as file: # The data file is a list of names in quotes # seperated by a comma. The replace method # strips the quotes and the split seperates # by comma. names = file.read().replace('"','').split(",") return names def alphaValue(name): # The unicode value for lowercase a is 97 # but we want 1, hence the magic number! value = 0 for letter in name.lower(): value += (ord(letter)  96) return value # Read the data file and sort it. names = sorted(readFile()) count = 0 position = 1 for name in names: count+=(alphaValue(name) * position) position+=1 print(count)

Project Euler problem 21 is to find the sum of all amicable numbers under 10000. An amicable number is:
Let d(n) be the sum of proper divisors of n then d(a)=b and d(b)=a if a!=b then a and b are amicable numbers.
Brute force approach first in Python:
amicables = set() # LIMIT is the value tested up to LIMIT = 10000 def factorize(number): factors = [] for i in range(1, number): if number % i == 0: factors.append(i) return(factors) def amicable(a): # Let d(n) is the sum of proper divisors of n (less than n that # divide into n) d(a) = b and d(b)=a if a!=b then a and b are # amicable numbers. Da = sum(factorize(a)) Db = sum(factorize(Da)) if Db == Da: return None if Db == a: if Db > Da: return (Da, Db) return (Db, Da) for value in range(1, LIMIT): test = amicable(value) if test != None: amicables.add(test) print(amicables) total=0 for amicable in amicables: total = total + sum(amicable) print(total)

Problem 13 is summing a list of fifty digit numbers. I used the gmp library and R to solve in 2 lines:
library(gmp) print(substring((sum(as.bigz(readLines("euler13.txt")))), 0, 10))
Not much more to say really. I forgot the problem asks for the first 10 digits, so my first answer was wrong.

Today I’m trying problem 12  find the first triangular number with over 500 divisors. This is the first Project Euler problem I’ve really struggled to find a solution in a reasonable amount of time.
What’s a triangular number? It is the sequence found by summing all the natural numbers, for example the third number is $1+2+3=6$. Interestingly, it counts objects arranged as a triangle.
This also has closed form $T_n=\sum_{i=1}^{n}i=\frac{n(n+1)}{2}$.
I started with a brute force approach  iterate through the triangular numbers and test if the number of divisors is greater than 500. I’m using the “numbers” package’s Sigma function to implement divisor function $\sigma x(n)=\sum{d n}d^x$ where $\sigma _0(n)$ gives the total number of factors for a given number. That requires a loop, which is going to dominate for large $n$, so $O(n^2)$. library(numbers) triangular.number < function(number) { number = (number * (number + 1)) / 2 } num.factors < 0 i < 1 while (num.factors < 500) { num.factors < (Sigma((triangular.number(i)), k = 0)) print(triangular.number(i)) i < i + 1 }
Not terribly efficient but it gets the correct answer. So how can we improve the algorithm? Reducing the number of times we repeat the loop would be a good place to start.
Now, $a(n)$ and $b(n+1)$ are coprime  the only positive integer that divides them both is 1. This has a useful property, that $lcm(a,b)=ab$. Thing is, I can’t see how to incorporate it…

I’m beginning to like R. You can capture table data from a connection (a generalised file), one of which is the clipboard, to a data frame.
problem.matrix < as.matrix(read.table("clipboard",sep=" "))
This gives a 20×20 matrix. Awesome. Although this all goes askew once Visual Studio is involved – I added a text file containing the data to the solution explorer and discovered it gets encoded in some fantastic way introducing a Byte Order Mark (BOM) so I needed to specify the encoding scheme.
problem.matrix < as.matrix(read.csv("euler.txt", header = FALSE, sep = " ", fileEncoding = "UTF8BOM"))
The problem is to find the greatest product of four adjacent numbers (horizontally, vertically or diagonally) in a 20×20 grid. I’m approaching this by considering the products of multiple matrices.
Consider that the solution cannot lie, for example, in the last 3 columns because we need 4 numbers. So let’s consider a 17×17 matrix being moved along and down the data.
Remember to repeat this from right to left as there are two diagonal directions. The same approach can be taken for the rows, using a 17×20 matrix and for the columns using a 20×17 matrix.
problem.matrix < as.matrix(read.csv("euler.txt", header = FALSE, sep = " ", fileEncoding = "UTF8BOM")) diagonal.ltor < problem.matrix[1:17, 1:17] * problem.matrix[2:18, 2:18] * problem.matrix[3:19, 3:19] * problem.matrix[4:20, 4:20] diagonal.rto1 < problem.matrix[4:20, 1:17] * problem.matrix[3:19, 2:18] * problem.matrix[2:18, 3:19] * problem.matrix[1:17, 4:20] horizontal < problem.matrix[, 1:17] * problem.matrix[, 2:18] * problem.matrix[, 3:19] * problem.matrix[, 4:20] vertical < problem.matrix[1:17,] * problem.matrix[2:18,] * problem.matrix[3:19,] * problem.matrix[4:20,] print(max(diagonal.ltor, diagonal.rto1, horizontal, vertical))