Skip to main content

Why Doesn’t The Simple Formula Work?

Problem: Write a program to find sum of all multiples of 3 and 5 upto input n. If a number is multiple of both 3 and 5 add it only once. We were given this problem at work. I tried to “solve” it using math. Clearly, we can add all the multiples of 3 and 5 upto n and subtract multiples of 15 once to account for the overcounting. We could derive formula for this as follows:

Using Complex Numbers to Move Mars Rover

Recently I was given Mars Rover Problem and told that my solution was pretty cool. Before that I assumed this is how everybody else was doing it ;). Here is golang implementation. Enjoy! Design Position and orientation are represented as complex numbers implemented as type vect. Builtin complex type is not used because it uses floats which they can’t be compared easily. In calculations below $j = \sqrt{-1}$. Multiplication of complex numbers allow us to rotate.

Iterating Over A Trie With Channels And Goroutines

We have a trie that stores set of integer slices. We want an iterator over its items. What we do here is have one goroutine to walk over the trie and send the items over channel. Other goroutine pulls the items from the channel and prints them out. My mental image of this is one gopher jumping around a tree and throwing mangoes back at his friend on the ground.

Embedding Golang Interfaces Into Structs To Simplify Testing

You can embed an interface inside a struct in golang. I discovered it by accident not by reading the manual. Here is a problem that it solves. There are many more in Embedding in Go: Part 3 - interfaces in structs . You have a dependency on an external library. The library exports a large interface. You want to mock it for testing but then you will have to implement all the methods on it.

A Generic Red-Black Tree Implementation In Golang

To balance the tree CLRS implementation rotates the subtrees. The functional implementations rewrites the tree [1,2]. Thinking in terms of rewriting instead of rotating feels like looking at the problem in a proper coordinate system. My implementation here is translation of Matt Might’s Haskell code in go. The generics syntax is easy on eyes. I like it. References The missing method: Deleting from Okasaki’s red-black trees by Matt Might Red-Black Trees in a Functional Setting by Okasaki The Next Step for Generics by Ian Lance Taylor and Robert Griesemer Code Playground link